Class: Lookbook::Formatter

Inherits:
Rouge::Formatters::HTML
  • Object
show all
Defined in:
lib/lookbook/code_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Formatter

Returns a new instance of Formatter.



25
26
27
28
29
# File 'lib/lookbook/code_formatter.rb', line 25

def initialize(opts = {})
  @opts = opts
  @highlight_lines = opts[:highlight_lines].to_a || []
  @start_line = opts[:start_line] || 0
end

Instance Method Details

#stream(tokens, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lookbook/code_formatter.rb', line 31

def stream(tokens, &block)
  token_lines(tokens).each_with_index do |line_tokens, i|
    yield "<div class='line #{"highlighted-line" if @highlight_lines.include?(i + 1)}'>"
    yield "<span class='line-number'>#{@start_line + i}</span>" if @opts[:line_numbers]
    yield "<span class='line-content'>"
    line_tokens.each do |token, value|
      yield span(token, value)
    end
    yield "</span>"
    yield "</div>"
  end
end