class Rouge::Formatters::HTML

Transforms a token stream into HTML output.

Constants

TABLE_FOR_ESCAPE_HTML

Public Instance Methods

safe_span(tok, safe_val) click to toggle source
# File lib/rouge/formatters/html.rb, line 20
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
span(tok, val) click to toggle source
# File lib/rouge/formatters/html.rb, line 14
def span(tok, val)
  return val if escape?(tok)

  safe_span(tok, escape_special_html_chars(val))
end
stream(tokens) { |span(tok, val)| ... } click to toggle source

@yield the html output.

# File lib/rouge/formatters/html.rb, line 10
def stream(tokens, &b)
  tokens.each { |tok, val| yield span(tok, val) }
end

Private Instance Methods

escape_special_html_chars(value) click to toggle source

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.

# File lib/rouge/formatters/html.rb, line 47
def escape_special_html_chars(value)
  escape_regex = /[&<>]/
  return value unless value =~ escape_regex

  value.gsub(escape_regex, TABLE_FOR_ESCAPE_HTML)
end