html_gitlab.rb 785 Bytes
Newer Older
1 2
# frozen_string_literal: true

3 4
module Rouge
  module Formatters
5
    class HTMLGitlab < Rouge::Formatters::HTML
6 7 8 9
      tag 'html_gitlab'

      # Creates a new <tt>Rouge::Formatter::HTMLGitlab</tt> instance.
      #
10
      # [+tag+]     The tag (language) of the lexer used to generate the formatted tokens
11 12
      def initialize(tag: nil)
        @line_number = 1
13
        @tag = tag
14 15
      end

16
      def stream(tokens)
17
        is_first = true
18
        token_lines(tokens) do |line|
19 20 21
          yield "\n" unless is_first
          is_first = false

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

26
          @line_number += 1
27 28 29 30 31
        end
      end
    end
  end
end