debian-mirror-gitlab/lib/gitlab/asciidoc.rb

79 lines
2.4 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
require 'asciidoctor'
2019-09-04 21:01:54 +05:30
require 'asciidoctor-plantuml'
require 'asciidoctor/extensions'
require 'gitlab/asciidoc/html5_converter'
2020-10-24 23:57:45 +05:30
require 'gitlab/asciidoc/mermaid_block_processor'
2019-09-30 21:07:59 +05:30
require 'gitlab/asciidoc/syntax_highlighter/html_pipeline_adapter'
2015-09-11 14:41:01 +05:30
module Gitlab
# Parser/renderer for the AsciiDoc format that uses Asciidoctor and filters
# the resulting HTML through HTML pipeline filters.
module Asciidoc
2019-09-04 21:01:54 +05:30
MAX_INCLUDE_DEPTH = 5
2020-02-01 01:16:34 +05:30
MAX_INCLUDES = 32
2019-09-04 21:01:54 +05:30
DEFAULT_ADOC_ATTRS = {
'showtitle' => true,
2019-09-30 21:07:59 +05:30
'sectanchors' => true,
2019-09-04 21:01:54 +05:30
'idprefix' => 'user-content-',
'idseparator' => '-',
'env' => 'gitlab',
'env-gitlab' => '',
2019-09-30 21:07:59 +05:30
'source-highlighter' => 'gitlab-html-pipeline',
2019-09-04 21:01:54 +05:30
'icons' => 'font',
'outfilesuffix' => '.adoc',
'max-include-depth' => MAX_INCLUDE_DEPTH
}.freeze
2015-09-11 14:41:01 +05:30
2020-04-22 19:07:51 +05:30
def self.path_attrs(path)
return {} unless path
{
# Set an empty docname if the path is a directory
'docname' => if path[-1] == ::File::SEPARATOR
''
else
::File.basename(path, '.*')
end
}
end
2015-09-11 14:41:01 +05:30
# Public: Converts the provided Asciidoc markup into HTML.
#
# input - the source text in Asciidoc format
2019-09-04 21:01:54 +05:30
# context - :commit, :project, :ref, :requested_path
2015-09-11 14:41:01 +05:30
#
2017-08-17 22:00:37 +05:30
def self.render(input, context)
2019-09-04 21:01:54 +05:30
extensions = proc do
include_processor ::Gitlab::Asciidoc::IncludeProcessor.new(context)
2020-10-24 23:57:45 +05:30
block ::Gitlab::Asciidoc::MermaidBlockProcessor
2019-09-04 21:01:54 +05:30
end
2020-04-22 19:07:51 +05:30
extra_attrs = path_attrs(context[:requested_path])
2017-08-17 22:00:37 +05:30
asciidoc_opts = { safe: :secure,
backend: :gitlab_html5,
2020-04-22 19:07:51 +05:30
attributes: DEFAULT_ADOC_ATTRS.merge(extra_attrs),
2019-09-04 21:01:54 +05:30
extensions: extensions }
2015-09-11 14:41:01 +05:30
2017-09-10 17:25:29 +05:30
context[:pipeline] = :ascii_doc
2020-02-01 01:16:34 +05:30
context[:max_includes] = [MAX_INCLUDES, context[:max_includes]].compact.min
2017-08-17 22:00:37 +05:30
plantuml_setup
2015-09-11 14:41:01 +05:30
2017-08-17 22:00:37 +05:30
html = ::Asciidoctor.convert(input, asciidoc_opts)
html = Banzai.render(html, context)
2015-09-11 14:41:01 +05:30
html.html_safe
end
2017-08-17 22:00:37 +05:30
def self.plantuml_setup
Asciidoctor::PlantUml.configure do |conf|
2018-03-17 18:26:18 +05:30
conf.url = Gitlab::CurrentSettings.plantuml_url
conf.svg_enable = Gitlab::CurrentSettings.plantuml_enabled
conf.png_enable = Gitlab::CurrentSettings.plantuml_enabled
2017-08-17 22:00:37 +05:30
conf.txt_enable = false
end
end
2015-09-11 14:41:01 +05:30
end
end