2018-10-08 10:50:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-06-28 14:42:41 -04:00
|
|
|
module Rouge
|
|
|
|
module Formatters
|
2016-06-15 15:32:55 -04:00
|
|
|
class HTMLGitlab < Rouge::Formatters::HTML
|
2015-06-28 14:42:41 -04:00
|
|
|
tag 'html_gitlab'
|
|
|
|
|
|
|
|
# Creates a new <tt>Rouge::Formatter::HTMLGitlab</tt> instance.
|
|
|
|
#
|
2021-03-18 02:11:52 -04:00
|
|
|
# [+tag+] The tag (language) of the lexer used to generate the formatted tokens
|
|
|
|
# [+line_number+] The line number used to populate line IDs
|
2021-02-01 13:09:17 -05:00
|
|
|
def initialize(options = {})
|
|
|
|
@tag = options[:tag]
|
2021-03-18 02:11:52 -04:00
|
|
|
@line_number = options[:line_number] || 1
|
2015-06-28 14:42:41 -04:00
|
|
|
end
|
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
def stream(tokens)
|
2016-06-21 14:23:10 -04:00
|
|
|
is_first = true
|
2016-06-15 15:32:55 -04:00
|
|
|
token_lines(tokens) do |line|
|
2016-06-21 14:23:10 -04:00
|
|
|
yield "\n" unless is_first
|
|
|
|
is_first = false
|
|
|
|
|
2017-03-10 16:34:29 -05:00
|
|
|
yield %(<span id="LC#{@line_number}" class="line" lang="#{@tag}">)
|
2021-10-28 14:14:18 -04:00
|
|
|
|
|
|
|
line.each do |token, value|
|
|
|
|
yield highlight_unicode_control_characters(span(token, value.chomp! || value))
|
|
|
|
end
|
|
|
|
|
2016-07-15 01:43:49 -04:00
|
|
|
yield %(</span>)
|
2015-06-28 14:42:41 -04:00
|
|
|
|
2016-06-15 15:32:55 -04:00
|
|
|
@line_number += 1
|
2015-06-28 14:42:41 -04:00
|
|
|
end
|
|
|
|
end
|
2021-10-28 14:14:18 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def highlight_unicode_control_characters(text)
|
|
|
|
text.gsub(Gitlab::Unicode::BIDI_REGEXP) do |char|
|
|
|
|
%(<span class="unicode-bidi has-tooltip" data-toggle="tooltip" title="#{Gitlab::Unicode.bidi_warning}">#{char}</span>)
|
|
|
|
end
|
|
|
|
end
|
2015-06-28 14:42:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|