gitlab_html.rb 1.52 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
    @options = options.dup
10 11 12
    super options
  end

13 14 15 16 17
  def normal_text(text)
    return text unless text.present?
    text.gsub("'", "&rsquo;")
  end

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

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

34
    HTML
35
  end
36

37 38 39 40
  def link(link, title, content)
    h.link_to_gfm(content, link, title: title)
  end

41 42 43 44 45 46 47 48 49 50
  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

51
  def postprocess(full_document)
Marin Jankovski's avatar
Marin Jankovski committed
52 53 54
    unless @template.instance_variable_get("@project_wiki") || @project.nil?
      full_document = h.create_relative_links(full_document)
    end
55 56 57 58 59
    if @options[:parse_tasks]
      h.gfm_with_tasks(full_document)
    else
      h.gfm(full_document)
    end
60
  end
61
end