gitlab_html.rb 1.65 KB
Newer Older
1
class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
2 3 4 5

  attr_reader :template
  alias_method :h, :template

6
  def initialize(template, color_scheme, options = {})
7
    @template = template
8
    @color_scheme = color_scheme
9
    @project = @template.instance_variable_get("@project")
10
    @options = options.dup
11

12
    super(options)
13 14
  end

15 16 17 18 19 20
  # If project has issue number 39, apostrophe will be linked in
  # regular text to the issue as Redcarpet will convert apostrophe to
  # #39;
  # We replace apostrophe with right single quote before Redcarpet
  # does the processing and put the apostrophe back in postprocessing.
  # This only influences regular text, code blocks are untouched.
21 22
  def normal_text(text)
    return text unless text.present?
23

24 25 26
    text.gsub("'", "&rsquo;")
  end

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

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

39
    formatter = Rugments::Formatters::HTML.new(
40
      cssclass: "code highlight #{@color_scheme} #{lexer.tag}"
41 42
    )
    formatter.format(lexer.lex(code))
43
  end
44 45

  def postprocess(full_document)
46
    full_document.gsub!("&rsquo;", "'")
47

Marin Jankovski's avatar
Marin Jankovski committed
48 49 50
    unless @template.instance_variable_get("@project_wiki") || @project.nil?
      full_document = h.create_relative_links(full_document)
    end
51

52
    h.gfm_with_options(full_document, @options)
53
  end
54
end