class Rouge::Formatters::HTML

Transforms a token stream into HTML output.

def initialize(opts={})

Options Hash: (**opts)
  • :wrap (true/false) --
  • :inline_theme (Rouge::CSSTheme) --
  • :line_numbers (true/false) --
  • :css_class (String) --
def initialize(opts={})
  @css_class = opts.fetch(:css_class, 'highlight')
  @line_numbers = opts.fetch(:line_numbers, false)
  @inline_theme = opts.fetch(:inline_theme, nil)
  @wrap = opts.fetch(:wrap, true)
end

def span(tok, val)

def span(tok, val)
  val = val.gsub(/[&<>]/, TABLE_FOR_ESCAPE_HTML)
  shortname = tok.shortname or raise "unknown token: #{tok.inspect} for #{val.inspect}"
  if shortname.empty?
    yield val
  else
    if @inline_theme
      rules = @inline_theme.style_for(tok).rendered_rules
      yield "<span style=\"#{rules.to_a.join(';')}\">#{val}</span>"
    else
      yield "<span class=\"#{shortname}\">#{val}</span>"
    end
  end
end

def stream(tokens, &b)

Other tags:
    Yield: - the html output.
def stream(tokens, &b)
  if @line_numbers
    stream_tableized(tokens, &b)
  else
    stream_untableized(tokens, &b)
  end
end

def stream_tableized(tokens)

def stream_tableized(tokens)
  num_lines = 0
  last_val = ''
  formatted = ''
  tokens.each do |tok, val|
    last_val = val
    num_lines += val.scan(/\n/).size
    span(tok, val) { |str| formatted << str }
  end
  # add an extra line for non-newline-terminated strings
  if last_val[-1] != "\n"
    num_lines += 1
    span(Token::Tokens::Text::Whitespace, "\n") { |str| formatted << str }
  end
  # generate a string of newline-separated line numbers for the gutter
  numbers = num_lines.times.map do |x|
    %<<pre class="lineno">#{x+1}</pre>>
  end.join
  yield "<div class=#{@css_class.inspect}>" if @wrap
  yield '<table style="border-spacing: 0"><tbody><tr>'
  # the "gl" class applies the style for Generic.Lineno
  yield '<td class="gutter gl" style="text-align: right">'
  yield numbers
  yield '</td>'
  yield '<td class="code">'
  yield '<pre>'
  yield formatted
  yield '</pre>'
  yield '</td>'
  yield '</tr></tbody></table>'
  yield '</div>' if @wrap
end

def stream_untableized(tokens, &b)

def stream_untableized(tokens, &b)
  yield "<pre class=#{@css_class.inspect}>" if @wrap
  tokens.each do |tok, val|
    span(tok, val, &b)
  end
  yield '</pre>' if @wrap
end