2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-05-12 19:07:48 -04:00
|
|
|
require 'asciidoctor'
|
2016-12-08 19:15:08 -05:00
|
|
|
require 'asciidoctor/converter/html5'
|
2016-11-28 12:41:29 -05:00
|
|
|
require "asciidoctor-plantuml"
|
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
|
|
|
|
DEFAULT_ADOC_ATTRS = [
|
|
|
|
'showtitle', 'idprefix=user-content-', 'idseparator=-', 'env=gitlab',
|
2018-02-14 14:41:13 -05:00
|
|
|
'env-gitlab', 'source-highlighter=html-pipeline', 'icons=font',
|
|
|
|
'outfilesuffix=.adoc'
|
2015-05-12 19:07:48 -04:00
|
|
|
].freeze
|
|
|
|
|
|
|
|
# Public: Converts the provided Asciidoc markup into HTML.
|
|
|
|
#
|
|
|
|
# input - the source text in Asciidoc format
|
|
|
|
#
|
2017-05-02 10:52:19 -04:00
|
|
|
def self.render(input, context)
|
2017-04-24 06:21:10 -04:00
|
|
|
asciidoc_opts = { safe: :secure,
|
|
|
|
backend: :gitlab_html5,
|
|
|
|
attributes: DEFAULT_ADOC_ATTRS }
|
2015-05-12 19:07:48 -04:00
|
|
|
|
2017-05-09 08:14:24 -04:00
|
|
|
context[:pipeline] = :ascii_doc
|
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
|
|
|
class Html5Converter < Asciidoctor::Converter::Html5Converter
|
|
|
|
extend Asciidoctor::Converter::Config
|
|
|
|
|
|
|
|
register_for 'gitlab_html5'
|
|
|
|
|
|
|
|
def stem(node)
|
|
|
|
return super unless node.style.to_sym == :latexmath
|
|
|
|
|
2017-05-09 08:14:24 -04:00
|
|
|
%(<pre#{id_attribute(node)} data-math-style="display"><code>#{node.content}</code></pre>)
|
2016-12-08 19:15:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def inline_quoted(node)
|
|
|
|
return super unless node.type.to_sym == :latexmath
|
|
|
|
|
2017-05-09 08:14:24 -04:00
|
|
|
%(<code#{id_attribute(node)} data-math-style="inline">#{node.text}</code>)
|
2016-12-08 19:15:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def id_attribute(node)
|
|
|
|
node.id ? %( id="#{node.id}") : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|