gitlab_html.rb 1.55 KB
Newer Older
1
class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
2 3 4 5 6 7 8

  attr_reader :template
  alias_method :h, :template

  def initialize(template, options = {})
    @template = template
    @project = @template.instance_variable_get("@project")
9
    @ref = @template.instance_variable_get("@ref")
Marin Jankovski's avatar
Marin Jankovski committed
10
    @request_path = @template.instance_variable_get("@path")
11
    @options = options.dup
12 13 14
    super options
  end

15
  def block_code(code, language)
16 17 18 19 20 21 22
    # New lines are placed to fix an rendering issue
    # with code wrapped inside <h1> tag for next case:
    #
    # # Title kinda h1
    #
    #     ruby code here
    #
23
    <<-HTML
24

25 26
<div class="highlighted-data #{h.user_color_scheme_class}">
  <div class="highlight">
27
    <pre><code class="#{language}">#{h.send(:html_escape, code)}</code></pre>
28 29
  </div>
</div>
30

31
    HTML
32
  end
33

34 35 36 37
  def link(link, title, content)
    h.link_to_gfm(content, link, title: title)
  end

38 39 40 41 42 43 44 45 46 47
  def header(text, level)
    if @options[:no_header_anchors]
      "<h#{level}>#{text}</h#{level}>"
    else
      id = ActionController::Base.helpers.strip_tags(h.gfm(text)).downcase() \
          .gsub(/[^a-z0-9_-]/, '-').gsub(/-+/, '-').gsub(/^-/, '').gsub(/-$/, '')
      "<h#{level} id=\"#{id}\">#{text}<a href=\"\##{id}\"></a></h#{level}>"
    end
  end

48
  def preprocess(full_document)
49
    if @project
Marin Jankovski's avatar
Marin Jankovski committed
50
      h.create_relative_links(full_document, @project, @ref, @request_path, is_wiki?)
51 52 53
    else
      full_document
    end
54 55
  end

56 57 58
  def postprocess(full_document)
    h.gfm(full_document)
  end
Marin Jankovski's avatar
Marin Jankovski committed
59 60 61 62

  def is_wiki?
    @template.instance_variable_get("@wiki")
  end
63
end