Class: Rouge::Formatters::HTMLTable

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

Constant Summary

Constants inherited from Rouge::Formatter

Rouge::Formatter::REGISTRY

Class Method Summary collapse

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 = {}) ⇒ HTMLTable

Returns a new instance of HTMLTable.



23
24
25
26
27
28
29
30
# File 'lib/rouge/formatters/html_table.rb', line 23

def initialize(inner, opts={})
  @inner = HTML.assert_html_formatter!(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

Class Method Details

.new(inner, opts = {}) ⇒ Object



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

def self.new(inner, opts={})
  if !inner.is_a?(HTML) && inner.is_a?(Formatter)
    require_relative 'html_legacy_table'
    return HTMLLegacyTable.new(inner, opts)
  end

  super(inner, opts)
end

Instance Method Details

#stream(tokens) {|%(<table class="#@table_class"><tbody><tr>)| ... } ⇒ Object

Yields:

  • (%(<table class="#@table_class"><tbody><tr>))


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rouge/formatters/html_table.rb', line 37

def stream(tokens, &b)
  num_lines = 0
  last_val = ''
  formatted = String.new('')

  tokens.each do |tok, val|
    last_val = val
    num_lines += val.scan(/\n/).size
    formatted << @inner.span(tok, val)
  end

  # add an extra line for non-newline-terminated strings
  if last_val[-1] != "\n"
    num_lines += 1
    yield @inner.span(Token::Tokens::Text::Whitespace, "\n")
  end

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

  numbers = %(<pre class="lineno">#{formatted_line_numbers}</pre>)

  yield %(<table class="#@table_class"><tbody><tr>)

  # the "gl" class applies the style for Generic.Lineno
  yield %(<td class="#@gutter_class gl" aria-hidden="true">)
  yield numbers
  yield '</td>'

  yield %(<td class="#@code_class"><pre>)
  yield formatted
  yield '</pre></td>'

  yield "</tr></tbody></table>"
end

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

Yields:

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


32
33
34
35
# File 'lib/rouge/formatters/html_table.rb', line 32

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