Class: Rouge::Formatters::HTML

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

Overview

Transforms a token stream into HTML output.

Constant Summary

Constants inherited from Rouge::Formatter

Rouge::Formatter::REGISTRY

Instance Method Summary collapse

Methods inherited from Rouge::Formatter

find, format, #format, #render, tag

Constructor Details

#initialize(opts = {}) ⇒ HTML

A css class to be used for the generated <pre> tag.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :css_class (Object)


12
13
14
# File 'lib/rouge/formatters/html.rb', line 12

def initialize(opts={})
  @css_class = opts[:css_class] || 'highlight'
end

Instance Method Details

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

Yields:

  • the html output.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rouge/formatters/html.rb', line 17

def stream(tokens, &b)
  yield "<pre class=#{@css_class.inspect}>"
  tokens.each do |tok, val|
    # TODO: properly html-encode val
    val = CGI.escape_html(val)

    case tok.shortname
    when ''
      yield val
    when nil
      raise "unknown token: #{tok.inspect}"
    else
      yield '<span class='
      yield tok.shortname.inspect
      yield '>'
      yield val
      yield '</span>'
    end
  end
  yield '</pre>'
end