gitlab_html.rb 1.28 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, color_scheme, options = {})
8
    @template = template
9
    @color_scheme = color_scheme
10
    @options = options.dup
11

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

18
    super(options)
19 20
  end

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

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

30 31 32 33
    # 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'
34
      code.gsub!(/^    /, "\t")
35
    end
36

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

  def postprocess(full_document)
44
    h.gfm_with_options(full_document, @options)
45
  end
46
end