Class: Rouge::Formatters::HTMLLegacyTable

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

Constant Summary

Constants inherited from Rouge::Formatter

Rouge::Formatter::REGISTRY

Instance Method Summary collapse

Methods inherited from Rouge::Formatter

disable_escape!, enable_escape!, #escape?, escape_enabled?, #filter_escapes, find, format, #format, #render, tag, with_escape

Constructor Details

#initialize(inner, opts = {}) ⇒ HTMLLegacyTable

Returns a new instance of HTMLLegacyTable.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rouge/formatters/html_legacy_table.rb', line 12

def initialize(inner, opts={})
  warn '[DEPRECATED] using a wrapped or line-managed delegator for HTMLTable is deprecated, and will be removed soon. Please use HTML, HTMLInline, or HTMLDebug, or write your own formatter.'

  @inner = inner
  @start_line = opts.fetch(:start_line, 1)
  @line_format = opts.fetch(:line_format, '%i')
  @table_class = opts.fetch(:table_class, 'rouge-table')
  @gutter_class = opts.fetch(:gutter_class, 'rouge-gutter')
  @code_class = opts.fetch(:code_class, 'rouge-code')
end

Instance Method Details

#stream(tokens) {|buffer.join| ... } ⇒ Object

Yields:

  • (buffer.join)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rouge/formatters/html_legacy_table.rb', line 28

def stream(tokens, &b)
  last_val = nil
  num_lines = tokens.reduce(0) {|count, (_, val)| count + (last_val = val).count("\n") }
  formatted = @inner.format(tokens)

  unless last_val && last_val.end_with?("\n")
    num_lines += 1
    formatted << "\n" unless formatted.end_with?("\n")
  end

  # generate a string of newline-separated line numbers for the gutter
  formatted_line_numbers = (@start_line..(@start_line + num_lines - 1)).map do |i|
    sprintf(@line_format, i)
  end.join("\n") << "\n"

  buffer = [%(<table class="#@table_class"><tbody><tr>)]
  # the "gl" class applies the style for Generic.Lineno
  buffer << %(<td class="#@gutter_class gl" aria-hidden="true">)
  buffer << %(<pre class="lineno">#{formatted_line_numbers}</pre>)
  buffer << '</td>'
  buffer << %(<td class="#@code_class"><pre><code>)
  buffer << formatted
  buffer << '</code></pre></td>'
  buffer << '</tr></tbody></table>'

  yield buffer.join
end

#style(scope) {|"#{scope} .#{@table_class} { border-spacing: 0 }"| ... } ⇒ Object

Yields:

  • ("#{scope} .#{@table_class} { border-spacing: 0 }")


23
24
25
26
# File 'lib/rouge/formatters/html_legacy_table.rb', line 23

def style(scope)
  yield "#{scope} .#{@table_class} { border-spacing: 0 }"
  yield "#{scope} .#{@gutter_class} { text-align: right }"
end