summaryrefslogtreecommitdiff
path: root/lib/rouge/formatters/html_gitlab.rb
blob: ec95ddf03eaf0c8f5d6978749579e3fb589511d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Rouge
  module Formatters
    class HTMLGitlab < Rouge::Formatters::HTML
      tag 'html_gitlab'

      # Creates a new <tt>Rouge::Formatter::HTMLGitlab</tt> instance.
      #
      # [+linenostart+]     The line number for the first line (default: 1).
      def initialize(linenostart: 1, tag: nil)
        @linenostart = linenostart
        @line_number = linenostart
        @tag = tag
      end

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

          yield %(<span id="LC#{@line_number}" class="line" lang="#{@tag}">)
          line.each { |token, value| yield span(token, value.chomp) }
          yield %(</span>)

          @line_number += 1
        end
      end
    end
  end
end