class CodeRay::Encoders::HTML
Default: false
or :debug (via inspect).
Can be :info (show token kind on mouse-over), :info_long (with full path)
Include some information into the output using the title attribute.
=== :hint
Default: nil
in combination with :line_numbers.
Bolding is deactivated when :highlight_lines is set. It only makes sense
Can be any Enumerable, typically just an Array or Range, of numbers.
Highlights certain line numbers.
=== :highlight_lines
Default: 10
Make every n
-th number appear bold.
=== :bold_every
Default: 1
Where to start with line number counting.
=== :line_number_start
Default: true, default prefix name: “line”
The prefix must consist only of letters, digits, and underscores.
or a prefix string that will be prepended to the anchor name.
Adds anchors and links to the line numbers. Can be false (off), true (on),
=== :line_number_anchors
Default: nil
Include line numbers in :table, :inline, or nil (no line numbers)
=== :line_numbers
Default: false
Forced to true if :line_numbers option is set to :inline.
Split multiline blocks at line breaks.
=== :break_lines
Default: ‘CodeRay output’
The title of the HTML page (works only when :wrap is set to :page.)
=== :title
Default: nil
You can also use Encoders::Div and Encoders::Span.
Wrap in :page, :div, :span or nil.
=== :wrap
Default: :class
How to include the styles; can be :class or :style.
=== :css
Default: 8
false will keep tab characters untouched.
Convert t characters to n
spaces (a number or false.)
=== :tab_width
== Options
)
:css => :style
:line_numbers => :inline,
:wrap => nil,
puts CodeRay.scan(‘Some code’, :ruby).html(
puts CodeRay.scan(‘Some /code/’, :ruby).span #-> the same
#-> <span class=“CodeRay”><span class=“co”>Some</span> /code/</span>
puts CodeRay.scan(‘Some /code/’, :ruby).html(:wrap => :span)
puts CodeRay.scan(‘Some /code/’, :ruby).html #-> a HTML page
require ‘coderay’
== Usage
It provides save, fast XHTML generation and CSS support.
This is CodeRay’s most important highlighter:
= HTML Encoder
def self.make_html_escape_hash
def self.make_html_escape_hash { '&' => '&', '"' => '"', '>' => '>', '<' => '<', # "\t" => will be set to ' ' * options[:tab_width] during setup }.tap do |hash| # Escape ASCII control codes except \x9 == \t and \xA == \n. (Array(0x00..0x8) + Array(0xB..0x1F)).each { |invalid| hash[invalid.chr] = ' ' } end end
def self.token_path_to_hint hint, kinds
Generate a hint about the given +kinds+ in a +hint+ style.
def self.token_path_to_hint hint, kinds kinds = Array kinds title = case hint when :info kinds = kinds[1..-1] if TRANSPARENT_TOKEN_KINDS.include? kinds.first TOKEN_KIND_TO_INFO[kinds.first] when :info_long kinds.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/') when :debug kinds.inspect end title ? " title=\"#{title}\"" : '' end
def begin_group kind
def begin_group kind @out << (@span_for_kinds[@last_opened ? [kind, *@opened] : kind] || '<span>') @opened << kind @last_opened = kind if @set_last_opened end
def begin_line kind
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
def break_lines text, style
def break_lines text, style reopen = ''.dup @opened.each_with_index do |kind, index| reopen << (@span_for_kinds[index > 0 ? [kind, *@opened[0...index]] : kind] || '<span>') end text.gsub("\n", "#{'</span>' * @opened.size}#{'</span>' if style}\n#{reopen}#{style}") end
def check_group_nesting name, kind
def check_group_nesting name, kind if @opened.empty? || @opened.last != kind warn "Malformed token stream: Trying to close a #{name} (%p) that is not open. Open are: %p." % [kind, @opened[1..-1]] end end
def check_options! options
def check_options! options unless [false, nil, :debug, :info, :info_long].include? options[:hint] raise ArgumentError, "Unknown value %p for :hint; expected :info, :info_long, :debug, false, or nil." % [options[:hint]] end unless [:class, :style].include? options[:css] raise ArgumentError, 'Unknown value %p for :css.' % [options[:css]] end options[:break_lines] = true if options[:line_numbers] == :inline end
def close_span
def close_span if @opened.pop @out << '</span>' @last_opened = @opened.last if @last_opened end end
def css_class_for_kinds kinds
def css_class_for_kinds kinds TokenKinds[kinds.is_a?(Symbol) ? kinds : kinds.first] end
def end_group kind
def end_group kind check_group_nesting 'token group', kind if $CODERAY_DEBUG close_span end
def end_line kind
def end_line kind check_group_nesting 'line', kind if $CODERAY_DEBUG close_span end
def finish options
def finish options unless @opened.empty? @out << '</span>' while @opened.pop @last_opened = nil end if @out.respond_to? :to_str @out.extend Output @out.css = @css if options[:line_numbers] Numbering.number! @out, options[:line_numbers], options end @out.wrap! options[:wrap] @out.apply_title! options[:title] end if defined?(@real_out) && @real_out @real_out << @out @out = @real_out end super end
def make_span_for_kinds method, hint
def make_span_for_kinds method, hint Hash.new do |h, kinds| begin css_class = css_class_for_kinds(kinds) title = HTML.token_path_to_hint hint, kinds if hint if css_class || title if method == :style style = style_for_kinds(kinds) "<span#{title}#{" style=\"#{style}\"" if style}>" else "<span#{title}#{" class=\"#{css_class}\"" if css_class}>" end end end.tap do |span| h.clear if h.size >= 100 h[kinds] = span end end end
def setup options
def setup options super check_options! options if options[:wrap] || options[:line_numbers] @real_out = @out @out = ''.dup end @break_lines = (options[:break_lines] == true) @HTML_ESCAPE = HTML_ESCAPE.merge("\t" => options[:tab_width] ? ' ' * options[:tab_width] : "\t") @opened = [] @last_opened = nil @css = CSS.new options[:style] @span_for_kinds = make_span_for_kinds(options[:css], options[:hint]) @set_last_opened = options[:hint] || options[:css] == :style end
def style_for_kinds kinds
def style_for_kinds kinds css_classes = kinds.is_a?(Array) ? kinds.map { |c| TokenKinds[c] } : [TokenKinds[kinds]] @css.get_style_for_css_classes css_classes end
def text_token text, kind
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