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

67 lines
1.9 KiB
Ruby
Raw Normal View History

2015-09-11 14:41:01 +05:30
require 'asciidoctor'
2017-08-17 22:00:37 +05:30
require 'asciidoctor/converter/html5'
require "asciidoctor-plantuml"
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
DEFAULT_ADOC_ATTRS = [
'showtitle', 'idprefix=user-content-', 'idseparator=-', 'env=gitlab',
2018-03-27 19:54:05 +05:30
'env-gitlab', 'source-highlighter=html-pipeline', 'icons=font',
'outfilesuffix=.adoc'
2015-09-11 14:41:01 +05:30
].freeze
# Public: Converts the provided Asciidoc markup into HTML.
#
# input - the source text in Asciidoc format
#
2017-08-17 22:00:37 +05:30
def self.render(input, context)
asciidoc_opts = { safe: :secure,
backend: :gitlab_html5,
attributes: DEFAULT_ADOC_ATTRS }
2015-09-11 14:41:01 +05:30
2017-09-10 17:25:29 +05:30
context[:pipeline] = :ascii_doc
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
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-09-10 17:25:29 +05:30
%(<pre#{id_attribute(node)} data-math-style="display"><code>#{node.content}</code></pre>)
2017-08-17 22:00:37 +05:30
end
def inline_quoted(node)
return super unless node.type.to_sym == :latexmath
2017-09-10 17:25:29 +05:30
%(<code#{id_attribute(node)} data-math-style="inline">#{node.text}</code>)
2017-08-17 22:00:37 +05:30
end
private
def id_attribute(node)
node.id ? %( id="#{node.id}") : nil
end
end
2015-09-11 14:41:01 +05:30
end
end