Class: Ro::Template::RougeFormatter

Inherits:
Rouge::Formatters::HTMLInline
  • Object
show all
Defined in:
lib/ro/template/rouge_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ RougeFormatter

Returns a new instance of RougeFormatter.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ro/template/rouge_formatter.rb', line 6

def initialize(opts)
  theme = if opts.is_a?(Hash)
            opts[:theme] || opts['theme']
          elsif opts.is_a?(Symbol)
            opts.to_s
          else
            opts
          end

  super(theme)
end

Instance Method Details

#safe_span(tok, safe_val) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ro/template/rouge_formatter.rb', line 33

def safe_span(tok, safe_val)
  # return safe_val if tok == ::Rouge::Token::Tokens::Text
  return safe_val.gsub(/\s/, '&nbsp;').gsub(/\n/, '<br />') if tok == ::Rouge::Token::Tokens::Text

  rules = @theme.style_for(tok).rendered_rules
  "<span style=\"#{rules.to_a.join(';')}\">#{safe_val}</span>"
end

#stream(tokens) {|"<code class='code' style='white-space:pre;'>"| ... } ⇒ Object

Yields:

  • ("<code class='code' style='white-space:pre;'>")


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ro/template/rouge_formatter.rb', line 18

def stream(tokens)
  lineno = 1
  yield "<code class='code' style='white-space:pre;'>"
  token_lines(tokens).each do |line_tokens|
    yield "<div class='code-line code-line-#{lineno}'>"
    line_tokens.each do |token, value|
      yield span(token, value)
    end
    yield "\n"
    yield '</div>'
    lineno += 1
  end
  yield '</code>'
end