Class: Rouge::Formatters::HTML

Inherits:
Rouge::Formatter show all
Defined in:
lib/rouge/formatters/html.rb

Overview

Transforms a token stream into HTML output.

Direct Known Subclasses

HTMLDebug, HTMLInline

Constant Summary collapse

TABLE_FOR_ESCAPE_HTML =
{
  '&' => '&',
  '<' => '&lt;',
  '>' => '&gt;',
  "\r" => "",
}.freeze
ESCAPE_REGEX =
/[&<>\r]/.freeze

Constants inherited from Rouge::Formatter

Rouge::Formatter::REGISTRY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rouge::Formatter

disable_escape!, enable_escape!, #escape?, escape_enabled?, #filter_escapes, find, format, #format, #initialize, #render, tag, with_escape

Constructor Details

This class inherits a constructor from Rouge::Formatter

Class Method Details

.assert_html_formatter!(formatter) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/rouge/formatters/html.rb', line 17

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

Instance Method Details

#safe_span(tok, safe_val) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rouge/formatters/html.rb', line 36

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) ⇒ Object



30
31
32
33
34
# File 'lib/rouge/formatters/html.rb', line 30

def span(tok, val)
  return val if escape?(tok)

  safe_span(tok, escape_special_html_chars(val))
end

#stream(tokens) { ... } ⇒ Object

Yields:

  • the html output.



26
27
28
# File 'lib/rouge/formatters/html.rb', line 26

def stream(tokens, &b)
  tokens.each { |tok, val| yield span(tok, val) }
end