class Rugments::Formatters::HTML
Public Class Methods
new( nowrap: false, cssclass: 'highlight', linenos: nil, linenostart: 1, lineanchors: false, lineanchorsid: 'L', anchorlinenos: false, inline_theme: nil )
click to toggle source
# File lib/rugments/formatters/html.rb, line 8 def initialize( nowrap: false, cssclass: 'highlight', linenos: nil, linenostart: 1, lineanchors: false, lineanchorsid: 'L', anchorlinenos: false, inline_theme: nil ) @nowrap = nowrap @cssclass = cssclass @linenos = linenos @linenostart = linenostart @lineanchors = lineanchors @lineanchorsid = lineanchorsid @anchorlinenos = anchorlinenos @inline_theme = inline_theme end
Public Instance Methods
format(tokens)
click to toggle source
# File lib/rugments/formatters/html.rb, line 28 def format(tokens) case when @linenos == 'table' format_tableized(tokens) when @linenos == 'inline' format_untableized(tokens) else format_untableized(tokens) end end
Private Instance Methods
create_linenos(numbers)
click to toggle source
# File lib/rugments/formatters/html.rb, line 83 def create_linenos(numbers) if @anchorlinenos numbers.map! do |number| "<a href=\"##{@lineanchorsid}#{number}\">#{number}</a>" end end numbers.join("\n") end
create_lines(formatted)
click to toggle source
# File lib/rugments/formatters/html.rb, line 92 def create_lines(formatted) if @lineanchors lines = formatted.split("\n") lines = lines.each_with_index.map do |line, index| number = index + @linenostart if @linenos == 'inline' "<a name=\"L#{number}\"></a>" "<span class=\"linenos\">#{number}</span><span id=\"#{@lineanchorsid}#{number}\" class=\"line\">#{line}</span>" else "<span id=\"#{@lineanchorsid}#{number}\" class=\"line\">#{line}</span>" end end lines.join("\n") else if @linenos == 'inline' lines = formatted.split("\n") lines = lines.each_with_index.map do |line, index| number = index + @linenostart "<span class=\"linenos\">#{number}</span>#{line}" end lines.join("\n") else formatted end end end
format_tableized(tokens)
click to toggle source
# File lib/rugments/formatters/html.rb, line 51 def format_tableized(tokens) data = process_tokens(tokens) html = '' html << "<div class=\"#{@cssclass}\">\n" unless @nowrap html << "<table><tbody>\n" html << "<td class=\"linenos\"><pre>" html << create_linenos(data[:numbers]) html << "</pre></td>\n" html << "<td class=\"lines\"><pre><code>" html << create_lines(data[:code]) html << "</code></pre></td>\n" html << "</tbody></table>\n" html << "</div>\n" unless @nowrap end
format_untableized(tokens)
click to toggle source
# File lib/rugments/formatters/html.rb, line 41 def format_untableized(tokens) data = process_tokens(tokens) html = '' html << "<pre class=\"#{@cssclass}\"><code>" unless @nowrap html << create_lines(data[:code]) html << "</code></pre>\n" unless @nowrap html end
process_tokens(tokens)
click to toggle source
# File lib/rugments/formatters/html.rb, line 67 def process_tokens(tokens) num_lines = 0 last_val = '' formatted = '' tokens.each do |tok, val| last_val = val num_lines += val.scan(/\n/).size formatted << span(tok, val) end numbers = (@linenostart..num_lines + @linenostart - 1).to_a { numbers: numbers, code: formatted } end
span(tok, val)
click to toggle source
# File lib/rugments/formatters/html.rb, line 120 def span(tok, val) # http://stackoverflow.com/a/1600584/2587286 val = CGI.escapeHTML(val) if tok.shortname.empty? val else if @inline_theme theme = Theme.find(@inline_theme).new rules = theme.style_for(tok).rendered_rules "<span style=\"#{rules.to_a.join(';')}\">#{val}</span>" else "<span class=\"#{tok.shortname}\">#{val}</span>" end end end