2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-05-12 19:07:48 -04:00
|
|
|
require 'asciidoctor'
|
2019-06-14 03:53:08 -04:00
|
|
|
require 'asciidoctor-plantuml'
|
|
|
|
require 'asciidoctor/extensions'
|
|
|
|
require 'gitlab/asciidoc/html5_converter'
|
2019-06-19 02:37:48 -04:00
|
|
|
require 'gitlab/asciidoc/syntax_highlighter/html_pipeline_adapter'
|
2015-05-12 19:07:48 -04:00
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
# Parser/renderer for the AsciiDoc format that uses Asciidoctor and filters
|
|
|
|
# the resulting HTML through HTML pipeline filters.
|
|
|
|
module Asciidoc
|
2019-06-14 03:53:08 -04:00
|
|
|
MAX_INCLUDE_DEPTH = 5
|
2020-01-30 16:08:47 -05:00
|
|
|
MAX_INCLUDES = 32
|
2019-06-14 03:53:08 -04:00
|
|
|
DEFAULT_ADOC_ATTRS = {
|
|
|
|
'showtitle' => true,
|
2019-07-10 04:30:10 -04:00
|
|
|
'sectanchors' => true,
|
2019-06-14 03:53:08 -04:00
|
|
|
'idprefix' => 'user-content-',
|
|
|
|
'idseparator' => '-',
|
|
|
|
'env' => 'gitlab',
|
|
|
|
'env-gitlab' => '',
|
2019-06-19 02:37:48 -04:00
|
|
|
'source-highlighter' => 'gitlab-html-pipeline',
|
2019-06-14 03:53:08 -04:00
|
|
|
'icons' => 'font',
|
|
|
|
'outfilesuffix' => '.adoc',
|
|
|
|
'max-include-depth' => MAX_INCLUDE_DEPTH
|
|
|
|
}.freeze
|
2015-05-12 19:07:48 -04:00
|
|
|
|
|
|
|
# Public: Converts the provided Asciidoc markup into HTML.
|
|
|
|
#
|
|
|
|
# input - the source text in Asciidoc format
|
2019-06-14 03:53:08 -04:00
|
|
|
# context - :commit, :project, :ref, :requested_path
|
2015-05-12 19:07:48 -04:00
|
|
|
#
|
2017-05-02 10:52:19 -04:00
|
|
|
def self.render(input, context)
|
2019-06-14 03:53:08 -04:00
|
|
|
extensions = proc do
|
|
|
|
include_processor ::Gitlab::Asciidoc::IncludeProcessor.new(context)
|
|
|
|
end
|
|
|
|
|
2017-04-24 06:21:10 -04:00
|
|
|
asciidoc_opts = { safe: :secure,
|
|
|
|
backend: :gitlab_html5,
|
2019-06-14 03:53:08 -04:00
|
|
|
attributes: DEFAULT_ADOC_ATTRS,
|
|
|
|
extensions: extensions }
|
2015-05-12 19:07:48 -04:00
|
|
|
|
2017-05-09 08:14:24 -04:00
|
|
|
context[:pipeline] = :ascii_doc
|
2020-01-30 16:08:47 -05:00
|
|
|
context[:max_includes] = [MAX_INCLUDES, context[:max_includes]].compact.min
|
2017-05-02 10:52:19 -04:00
|
|
|
|
2016-11-28 12:41:29 -05:00
|
|
|
plantuml_setup
|
|
|
|
|
2015-05-12 19:07:48 -04:00
|
|
|
html = ::Asciidoctor.convert(input, asciidoc_opts)
|
2017-05-02 10:52:19 -04:00
|
|
|
html = Banzai.render(html, context)
|
2015-05-12 19:07:48 -04:00
|
|
|
html.html_safe
|
|
|
|
end
|
2016-12-08 19:15:08 -05:00
|
|
|
|
2016-11-28 12:41:29 -05:00
|
|
|
def self.plantuml_setup
|
|
|
|
Asciidoctor::PlantUml.configure do |conf|
|
2018-02-02 13:39:55 -05:00
|
|
|
conf.url = Gitlab::CurrentSettings.plantuml_url
|
|
|
|
conf.svg_enable = Gitlab::CurrentSettings.plantuml_enabled
|
|
|
|
conf.png_enable = Gitlab::CurrentSettings.plantuml_enabled
|
2016-11-28 12:41:29 -05:00
|
|
|
conf.txt_enable = false
|
|
|
|
end
|
|
|
|
end
|
2016-12-08 19:15:08 -05:00
|
|
|
end
|
|
|
|
end
|