2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
module Rouge
|
|
|
|
module Formatters
|
2016-08-24 12:49:21 +05:30
|
|
|
class HTMLGitlab < Rouge::Formatters::HTML
|
2015-09-11 14:41:01 +05:30
|
|
|
tag 'html_gitlab'
|
|
|
|
|
|
|
|
# Creates a new <tt>Rouge::Formatter::HTMLGitlab</tt> instance.
|
|
|
|
#
|
2021-04-29 21:17:54 +05:30
|
|
|
# [+tag+] The tag (language) of the lexer used to generate the formatted tokens
|
|
|
|
# [+line_number+] The line number used to populate line IDs
|
2021-03-11 19:13:27 +05:30
|
|
|
def initialize(options = {})
|
|
|
|
@tag = options[:tag]
|
2021-04-29 21:17:54 +05:30
|
|
|
@line_number = options[:line_number] || 1
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def stream(tokens)
|
2016-08-24 12:49:21 +05:30
|
|
|
is_first = true
|
|
|
|
token_lines(tokens) do |line|
|
|
|
|
yield "\n" unless is_first
|
|
|
|
is_first = false
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
yield %(<span id="LC#{@line_number}" class="line" lang="#{@tag}">)
|
2021-10-29 20:43:33 +05:30
|
|
|
|
|
|
|
line.each do |token, value|
|
|
|
|
yield highlight_unicode_control_characters(span(token, value.chomp! || value))
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
yield %(</span>)
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
@line_number += 1
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
2021-10-29 20:43:33 +05:30
|
|
|
|
|
|
|
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-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|