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 or false.) false will keep tab characters untouched.
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.
126 127 128 |
# File 'lib/coderay/encoders/html.rb', line 126 def css @css end |
Instance Method Details
#begin_group(kind) ⇒ Object
token groups, eg. strings
235 236 237 238 239 |
# File 'lib/coderay/encoders/html.rb', line 235 def begin_group kind @out << (@span_for_kinds[@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
247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/coderay/encoders/html.rb', line 247 def begin_line kind if style = @span_for_kinds[@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
241 242 243 244 |
# File 'lib/coderay/encoders/html.rb', line 241 def end_group kind check_group_nesting 'token group', kind if $CODERAY_DEBUG close_span end |
#end_line(kind) ⇒ Object
261 262 263 264 |
# File 'lib/coderay/encoders/html.rb', line 261 def end_line kind check_group_nesting 'line', kind if $CODERAY_DEBUG close_span end |
#text_token(text, kind) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/coderay/encoders/html.rb', line 221 def text_token text, kind style = @span_for_kinds[@last_opened ? [kind, *@opened] : kind] text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] } if text =~ /#{HTML_ESCAPE_PATTERN}/o text = break_lines(text, style) if @break_lines && (style || @opened.size > 0) && text.index("\n") if style @out << style << text << '</span>' else @out << text end end |