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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
3.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'
2023-01-13 00:05:48 +05:30
require 'asciidoctor/extensions/asciidoctor_kroki/version'
2021-02-22 17:27:13 +05:30
require 'asciidoctor/extensions/asciidoctor_kroki/extension'
2019-09-04 21:01:54 +05:30
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',
2021-02-22 17:27:13 +05:30
'max-include-depth' => MAX_INCLUDE_DEPTH,
# This feature is disabled because it relies on File#read to read the file.
# If we want to enable this feature we will need to provide a "GitLab compatible" implementation.
# This attribute is typically used to share common config (skinparam...) across all PlantUML diagrams.
# The value can be a path or a URL.
'kroki-plantuml-include!' => '',
# This feature is disabled because it relies on the local file system to save diagrams retrieved from the Kroki server.
'kroki-fetch-diagram!' => ''
2019-09-04 21:01:54 +05:30
}.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
2021-02-22 17:27:13 +05:30
::Gitlab::Kroki.formats(Gitlab::CurrentSettings).each do |name|
block ::AsciidoctorExtensions::KrokiBlockProcessor, name
end
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,
2021-02-22 17:27:13 +05:30
attributes: DEFAULT_ADOC_ATTRS
.merge(extra_attrs)
.merge({
# Define the Kroki server URL from the settings.
# This attribute cannot be overridden from the AsciiDoc document.
'kroki-server-url' => Gitlab::CurrentSettings.kroki_url
}),
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
2023-09-09 17:08:58 +05:30
Gitlab::Plantuml.configure
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
end
end