class Rouge::Formatters::HTML
Transforms a token stream into HTML output.
Constants
- ESCAPE_REGEX
- TABLE_FOR_ESCAPE_HTML
Public Class Methods
Source
# File lib/rouge/formatters/html.rb, line 16 def self.assert_html_formatter!(formatter) return formatter if formatter.respond_to?(:span) raise ArgumentError.new("Expected an instance of Rouge::Formatters::HTML, got #{formatter.class}. Try HTML, HTMLDebug, or HTMLInline.") end
Public Instance Methods
Source
# File lib/rouge/formatters/html.rb, line 35 def safe_span(tok, safe_val) if tok == Token::Tokens::Text safe_val else shortname = tok.shortname or raise "unknown token: #{tok.inspect} for #{safe_val.inspect}" "<span class=\"#{shortname}\">#{safe_val}</span>" end end
Source
# File lib/rouge/formatters/html.rb, line 29 def span(tok, val) return val if escape?(tok) safe_span(tok, escape_special_html_chars(val)) end
Source
# File lib/rouge/formatters/html.rb, line 25 def stream(tokens, &b) tokens.each { |tok, val| yield span(tok, val) } end
@yield the html output.
Private Instance Methods
Source
# File lib/rouge/formatters/html.rb, line 56 def escape_special_html_chars(value) return value unless value =~ ESCAPE_REGEX value.gsub(ESCAPE_REGEX, TABLE_FOR_ESCAPE_HTML) end
A performance-oriented helper method to escape โ&`, `<` and `>` for the rendered HTML from this formatter.
โString#gsub` will always return a new string instance irrespective of whether a substitution occurs. This method however invokes `String#gsub` only if a substitution is imminent.
Returns either the given โvalue` argument string as is or a new string with the special characters replaced with their escaped counterparts.