Class: Rouge::Formatters::HTMLGitlab

Inherits:
HTML
  • Object
show all
Defined in:
lib/rouge/formatters/html_gitlab.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HTMLGitlab

Creates a new Rouge::Formatter::HTMLGitlab instance.

tag

The tag (language) of the lexer used to generate the formatted tokens

line_number

The line number used to populate line IDs



12
13
14
15
16
17
18
# File 'lib/rouge/formatters/html_gitlab.rb', line 12

def initialize(options = {})
  @tag = options[:tag]
  @line_number = options[:line_number] || 1
  @fix_attributes = options[:fix_attributes]
  @ellipsis_indexes = options[:ellipsis_indexes] || []
  @ellipsis_svg = options[:ellipsis_svg]
end

Instance Method Details

#stream(tokens) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rouge/formatters/html_gitlab.rb', line 20

def stream(tokens)
  is_first = true
  token_lines(tokens) do |line|
    yield "\n" unless is_first
    is_first = false

    id = " id=\"LC#{@line_number}\"" unless @fix_attributes
    lang = " lang=\"#{@tag}\"" unless @fix_attributes
    data_lang = " data-lang=\"#{@tag}\"" if @fix_attributes && @tag

    yield %(<span#{id} class="line"#{lang}#{data_lang}>)

    line.each do |token, value|
      value = value.chomp! || value
      value = replace_space_characters(value)

      yield highlight_unicode_control_characters(span(token, value))
    end

    yield ellipsis if @ellipsis_indexes.include?(@line_number - 1) && @ellipsis_svg.present?

    yield %(</span>)

    @line_number += 1
  end
end