Class: CodeRay::Encoders::HTML
- Defined in:
- lib/coderay/encoders/html.rb,
lib/coderay/encoders/html/css.rb,
lib/coderay/encoders/html/output.rb,
lib/coderay/encoders/html/numbering.rb
Overview
HTML Encoder
This is CodeRay’s most important highlighter: It provides save, fast XHTML generation and CSS support.
Usage
require 'coderay'
puts CodeRay.scan('Some /code/', :ruby).html #-> a HTML page
puts CodeRay.scan('Some /code/', :ruby).html(:wrap => :span)
#-> <span class="CodeRay"><span class="co">Some</span> /code/</span>
puts CodeRay.scan('Some /code/', :ruby).span #-> the same
puts CodeRay.scan('Some code', :ruby).html(
:wrap => nil,
:line_numbers => :inline,
:css => :style
)
Options
:tab_width
Convert t characters to n spaces (a number.)
Default: 8
:css
How to include the styles; can be :class or :style.
Default: :class
:wrap
Wrap in :page, :div, :span or nil.
You can also use Encoders::Div and Encoders::Span.
Default: nil
:title
The title of the HTML page (works only when :wrap is set to :page.)
Default: ‘CodeRay output’
:break_lines
Split multiline blocks at line breaks. Forced to true if :line_numbers option is set to :inline.
Default: false
:line_numbers
Include line numbers in :table, :inline, or nil (no line numbers)
Default: nil
:line_number_anchors
Adds anchors and links to the line numbers. Can be false (off), true (on), or a prefix string that will be prepended to the anchor name.
The prefix must consist only of letters, digits, and underscores.
Default: true, default prefix name: “line”
:line_number_start
Where to start with line number counting.
Default: 1
:bold_every
Make every n-th number appear bold.
Default: 10
:highlight_lines
Highlights certain line numbers. Can be any Enumerable, typically just an Array or Range, of numbers.
Bolding is deactivated when :highlight_lines is set. It only makes sense in combination with :line_numbers.
Default: nil
:hint
Include some information into the output using the title attribute. Can be :info (show token kind on mouse-over), :info_long (with full path) or :debug (via inspect).
Default: false
Defined Under Namespace
Modules: Numbering, Output Classes: CSS
Constant Summary collapse
- FILE_EXTENSION =
'snippet.html'- DEFAULT_OPTIONS =
{ :tab_width => 8, :css => :class, :style => :alpha, :wrap => nil, :title => 'CodeRay output', :break_lines => false, :line_numbers => nil, :line_number_anchors => 'n', :line_number_start => 1, :bold_every => 10, :highlight_lines => nil, :hint => false, }
Instance Attribute Summary collapse
-
#css ⇒ Object
readonly
Returns the value of attribute css.
Attributes inherited from Encoder
Attributes included from Plugin
Instance Method Summary collapse
-
#begin_group(kind) ⇒ Object
token groups, eg.
-
#begin_line(kind) ⇒ Object
whole lines to be highlighted, eg.
- #end_group(kind) ⇒ Object
- #end_line(kind) ⇒ Object
- #text_token(text, kind) ⇒ Object
Methods inherited from Encoder
#<<, const_missing, #encode, #encode_tokens, file_extension, #file_extension, #initialize, #token
Methods included from Plugin
#aliases, #plugin_host, #register_for, #title
Constructor Details
This class inherits a constructor from CodeRay::Encoders::Encoder
Instance Attribute Details
#css ⇒ Object (readonly)
Returns the value of attribute css.
125 126 127 |
# File 'lib/coderay/encoders/html.rb', line 125 def css @css end |
Instance Method Details
#begin_group(kind) ⇒ Object
token groups, eg. strings
281 282 283 284 285 |
# File 'lib/coderay/encoders/html.rb', line 281 def begin_group kind @out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>') @opened << kind @last_opened = kind if @set_last_opened end |
#begin_line(kind) ⇒ Object
whole lines to be highlighted, eg. a deleted line in a diff
299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/coderay/encoders/html.rb', line 299 def begin_line kind if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] if style['class="'] @out << style.sub('class="', 'class="line ') else @out << style.sub('>', ' class="line">') end else @out << '<span class="line">' end @opened << kind @last_opened = kind if @options[:css] == :style end |
#end_group(kind) ⇒ Object
287 288 289 290 291 292 293 294 295 296 |
# File 'lib/coderay/encoders/html.rb', line 287 def end_group kind if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind) warn 'Malformed token stream: Trying to close a token (%p) ' \ 'that is not open. Open are: %p.' % [kind, @opened[1..-1]] end if @opened.pop @out << '</span>' @last_opened = @opened.last if @last_opened end end |
#end_line(kind) ⇒ Object
313 314 315 316 317 318 319 320 321 322 |
# File 'lib/coderay/encoders/html.rb', line 313 def end_line kind if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind) warn 'Malformed token stream: Trying to close a line (%p) ' \ 'that is not open. Open are: %p.' % [kind, @opened[1..-1]] end if @opened.pop @out << '</span>' @last_opened = @opened.last if @last_opened end end |
#text_token(text, kind) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/coderay/encoders/html.rb', line 257 def text_token text, kind if text =~ /#{HTML_ESCAPE_PATTERN}/o text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } end style = @span_for_kind[@last_opened ? [kind, *@opened] : kind] if @break_lines && (i = text.index("\n")) && (c = @opened.size + (style ? 1 : 0)) > 0 close = '</span>' * c reopen = '' @opened.each_with_index do |k, index| reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '<span>') end text[i .. -1] = text[i .. -1].gsub("\n", "#{close}\n#{reopen}#{style}") end if style @out << style << text << '</span>' else @out << text end end |