asciidoc.rb 1.92 KB
Newer Older
1
2
# frozen_string_literal: true

3
require 'asciidoctor'
4
5
6
require 'asciidoctor-plantuml'
require 'asciidoctor/extensions'
require 'gitlab/asciidoc/html5_converter'
7
require 'gitlab/asciidoc/syntax_highlighter/html_pipeline_adapter'
8
9
10
11
12

module Gitlab
  # Parser/renderer for the AsciiDoc format that uses Asciidoctor and filters
  # the resulting HTML through HTML pipeline filters.
  module Asciidoc
13
    MAX_INCLUDE_DEPTH = 5
14
    MAX_INCLUDES = 32
15
16
    DEFAULT_ADOC_ATTRS = {
        'showtitle' => true,
Guillaume Grossetie's avatar
Guillaume Grossetie committed
17
        'sectanchors' => true,
18
19
20
21
        'idprefix' => 'user-content-',
        'idseparator' => '-',
        'env' => 'gitlab',
        'env-gitlab' => '',
22
        'source-highlighter' => 'gitlab-html-pipeline',
23
24
25
26
        'icons' => 'font',
        'outfilesuffix' => '.adoc',
        'max-include-depth' => MAX_INCLUDE_DEPTH
    }.freeze
27
28
29
30

    # Public: Converts the provided Asciidoc markup into HTML.
    #
    # input         - the source text in Asciidoc format
31
    # context       - :commit, :project, :ref, :requested_path
32
    #
33
    def self.render(input, context)
34
35
36
37
      extensions = proc do
        include_processor ::Gitlab::Asciidoc::IncludeProcessor.new(context)
      end

38
39
      asciidoc_opts = { safe: :secure,
                        backend: :gitlab_html5,
40
41
                        attributes: DEFAULT_ADOC_ATTRS,
                        extensions: extensions }
42

43
      context[:pipeline] = :ascii_doc
44
      context[:max_includes] = [MAX_INCLUDES, context[:max_includes]].compact.min
45

46
47
      plantuml_setup

48
      html = ::Asciidoctor.convert(input, asciidoc_opts)
49
      html = Banzai.render(html, context)
50
51
      html.html_safe
    end
52

53
54
    def self.plantuml_setup
      Asciidoctor::PlantUml.configure do |conf|
55
56
57
        conf.url = Gitlab::CurrentSettings.plantuml_url
        conf.svg_enable = Gitlab::CurrentSettings.plantuml_enabled
        conf.png_enable = Gitlab::CurrentSettings.plantuml_enabled
58
59
60
        conf.txt_enable = false
      end
    end
61
62
  end
end