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',
|
2016-11-28 23:18:13 -05:00
|
|
|
'env-gitlab', 'source-highlighter=html-pipeline', 'icons=font'
|
2015-05-12 19:07:48 -04:00
|
|
|
].freeze
|
|
|
|
|
|
|
|
# Public: Converts the provided Asciidoc markup into HTML.
|
|
|
|
#
|
|
|
|
# input - the source text in Asciidoc format
|
|
|
|
# context - a Hash with the template context:
|
|
|
|
# :commit
|
|
|
|
# :project
|
|
|
|
# :project_wiki
|
|
|
|
# :requested_path
|
|
|
|
# :ref
|
|
|
|
# asciidoc_opts - a Hash of options to pass to the Asciidoctor converter
|
|
|
|
#
|
2015-10-14 14:18:49 -04:00
|
|
|
def self.render(input, context, asciidoc_opts = {})
|
|
|
|
asciidoc_opts.reverse_merge!(
|
2015-05-12 19:07:48 -04:00
|
|
|
safe: :secure,
|
2016-12-08 19:15:08 -05:00
|
|
|
backend: :gitlab_html5,
|
2015-05-12 19:07:48 -04:00
|
|
|
attributes: []
|
|
|
|
)
|
|
|
|
asciidoc_opts[:attributes].unshift(*DEFAULT_ADOC_ATTRS)
|
|
|
|
|
2016-11-28 12:41:29 -05:00
|
|
|
plantuml_setup
|
|
|
|
|
2015-05-12 19:07:48 -04:00
|
|
|
html = ::Asciidoctor.convert(input, asciidoc_opts)
|
|
|
|
|
2016-02-01 11:07:59 -05:00
|
|
|
html = Banzai.post_process(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|
|
|
|
|
conf.url = ApplicationSetting.current.plantuml_url
|
|
|
|
conf.svg_enable = ApplicationSetting.current.plantuml_enabled
|
|
|
|
conf.png_enable = ApplicationSetting.current.plantuml_enabled
|
|
|
|
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
|
|
|
|
|
|
|
|
%(<pre#{id_attribute(node)} class="code math js-render-math #{node.role}" data-math-style="display"><code>#{node.content}</code></pre>)
|
|
|
|
end
|
|
|
|
|
|
|
|
def inline_quoted(node)
|
|
|
|
return super unless node.type.to_sym == :latexmath
|
|
|
|
|
|
|
|
%(<code#{id_attribute(node)} class="code math js-render-math #{node.role}" data-math-style="inline">#{node.text}</code>)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def id_attribute(node)
|
|
|
|
node.id ? %( id="#{node.id}") : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|