gitlab_html.rb 1.22 KB
Newer Older
1
require 'active_support/core_ext/string/output_safety'
2

3
class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
4 5 6
  attr_reader :template
  alias_method :h, :template

7
  def initialize(template, options = {})
8
    @template = template
9
    @options = options.dup
10

11
    @options.reverse_merge!(
12
      # Handled further down the line by Gitlab::Markdown::SanitizationFilter
Douwe Maan's avatar
Douwe Maan committed
13
      escape_html: false,
14 15 16
      project: @template.instance_variable_get("@project")
    )

17
    super(options)
18 19
  end

20
  def normal_text(text)
21
    ERB::Util.html_escape_once(text)
22 23
  end

24 25
  # Stolen from Rouge::Plugins::Redcarpet as this module is not required
  # from Rouge's gem root.
26
  def block_code(code, language)
27
    lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::PlainText
28

29 30 31 32
    # XXX HACK: Redcarpet strips hard tabs out of code blocks,
    # so we assume you're not using leading spaces that aren't tabs,
    # and just replace them here.
    if lexer.tag == 'make'
33
      code.gsub!(/^    /, "\t")
34
    end
35

36
    formatter = Rouge::Formatters::HTMLGitlab.new(
37
      cssclass: "code highlight js-syntax-highlight #{lexer.tag}"
38 39
    )
    formatter.format(lexer.lex(code))
40
  end
41 42

  def postprocess(full_document)
43
    h.gfm(full_document, @options)
44
  end
45
end