summaryrefslogtreecommitdiff
path: root/lib/gitlab/asciidoc.rb
blob: bf33e5b1b1e4526051dce64ad584c67bf2e43203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'asciidoctor'
require 'html/pipeline'

module Gitlab
  # Parser/renderer for the AsciiDoc format that uses Asciidoctor and filters
  # the resulting HTML through HTML pipeline filters.
  module Asciidoc

    # Provide autoload paths for filters to prevent a circular dependency error
    autoload :RelativeLinkFilter, 'gitlab/markdown/relative_link_filter'

    DEFAULT_ADOC_ATTRS = [
      'showtitle', 'idprefix=user-content-', 'idseparator=-', 'env=gitlab',
      'env-gitlab', 'source-highlighter=html-pipeline'
    ].freeze

    # Public: Converts the provided Asciidoc markup into HTML.
    #
    # input         - the source text in Asciidoc format
    # context       - a Hash with the template context:
    #                 :commit
    #                 :project
    #                 :project_wiki
    #                 :requested_path
    #                 :ref
    # asciidoc_opts - a Hash of options to pass to the Asciidoctor converter
    # html_opts     - a Hash of options for HTML output:
    #                 :xhtml - output XHTML instead of HTML
    #
    def self.render(input, context, asciidoc_opts = {}, html_opts = {})
      asciidoc_opts = asciidoc_opts.reverse_merge(
        safe: :secure,
        backend: html_opts[:xhtml] ? :xhtml5 : :html5,
        attributes: []
      )
      asciidoc_opts[:attributes].unshift(*DEFAULT_ADOC_ATTRS)

      html = ::Asciidoctor.convert(input, asciidoc_opts)

      if context[:project]
        result = HTML::Pipeline.new(filters).call(html, context)

        save_opts = html_opts[:xhtml] ?
          Nokogiri::XML::Node::SaveOptions::AS_XHTML : 0

        html = result[:output].to_html(save_with: save_opts)
      end

      html.html_safe
    end

    private

    def self.filters
      [
        Gitlab::Markdown::RelativeLinkFilter
      ]
    end
  end
end