Class: Asciidoctor::Rouge::HtmlFormatter

Inherits:
Rouge::Formatter
  • Object
show all
Defined in:
lib/asciidoctor/rouge/html_formatter.rb

Overview

An HTML Rouge formatter for Asciidoctor with support for callouts and highlighted lines.

Instance Method Summary collapse

Constructor Details

#initialize(callout_markers: nil, highlighted_class: 'highlighted', highlighted_lines: [], inline_theme: nil, line_class: 'line', line_id: 'L%i', inner: nil) ⇒ HtmlFormatter

Returns a new instance of HtmlFormatter.

Parameters:

  • callout_markers (#[], nil) (defaults to: nil)

    callout markers indexed by line numbers to be inserted between the line's content and the line's closing tag.

  • highlighted_class (String) (defaults to: 'highlighted')

    CSS class to use on a line wrapper element for highlighted lines (see above). Defaults to "highlighted".

  • highlighted_lines (Array<Integer>) (defaults to: [])

    a list of line numbers (1-based) to be highlighted (i.e. added highlighted_class to a line wrapper element). Defaults to empty array.

  • inline_theme (String, Rouge::Theme, Class<Rouge::Theme>, nil) (defaults to: nil)

    the theme to use for inline styles, or nil to not set inline styles (i.e. use classes). This is ignored if inner is not nil.

  • line_class (String, nil) (defaults to: 'line')

    CSS class to set on a line wrapper element, or nil to not set a class. Defaults to "line".

  • line_id (String, nil) (defaults to: 'L%i')

    format string specifying id for each line, or nil to omit id. Defaults to "L%i".

  • inner (Rouge::Formatter::HTML, #span, nil) (defaults to: nil)

    the inner HTML formatter to delegate formatting of tokens to, or nil to get html or html_inline formatter from the Rouge::Formatter's registry.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/asciidoctor/rouge/html_formatter.rb', line 38

def initialize(callout_markers: nil,
               highlighted_class: 'highlighted',
               highlighted_lines: [],
               inline_theme: nil,
               line_class: 'line',
               line_id: 'L%i',
               inner: nil, **)

  inner ||= if inline_theme
    ::Rouge::Formatter.find('html_inline').new(inline_theme)
  else
    ::Rouge::Formatter.find('html').new
  end

  @callout_markers = callout_markers || {}
  @inner = inner
  @highlighted_lines = highlighted_lines || []
  @highlighted_class = highlighted_class
  @line_id = line_id
  @line_class = line_class
end

Instance Method Details

#stream(tokens, &block) ⇒ void



60
61
62
63
64
# File 'lib/asciidoctor/rouge/html_formatter.rb', line 60

def stream(tokens, &block)
  token_lines(tokens).with_index(1) do |line_tokens, lno|
    stream_lines(line_tokens, lno, &block)
  end
end

#stream_lines(tokens, line_num) {|String| ... } ⇒ void

Formats tokens on the specified line into HTML.

Parameters:

  • tokens (Array<Rouge::Token>)

    tokens on the line.

  • line_nums (Integer)

    the line number (1-based).

Yields:

  • (String)

    gives formatted content.



71
72
73
74
75
76
77
78
79
# File 'lib/asciidoctor/rouge/html_formatter.rb', line 71

def stream_lines(tokens, line_num)
  yield line_start(line_num)

  tokens.each do |token, value|
    yield @inner.span(token, value)
  end

  yield line_end(line_num)
end