Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2015-05-13 02:07:48 +0300
committerJakub Jirutka <jakub@jirutka.cz>2015-05-18 21:48:03 +0300
commit8dbc4746fe7c723b67f3c90cbf40fd7bf6c29cb7 (patch)
tree268aba3884c8e2e09fdefda9cad7a4c569154cc2 /lib/gitlab/asciidoc.rb
parentdc348baf18240dc05e209ec781daada2cbcfe16f (diff)
Handle AsciiDoc better, reuse HTML pipeline filters (fixes #9263)
Diffstat (limited to 'lib/gitlab/asciidoc.rb')
-rw-r--r--lib/gitlab/asciidoc.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/gitlab/asciidoc.rb b/lib/gitlab/asciidoc.rb
new file mode 100644
index 00000000000..bf33e5b1b1e
--- /dev/null
+++ b/lib/gitlab/asciidoc.rb
@@ -0,0 +1,60 @@
+require 'asciidoctor'
+require 'html/pipeline'
+
+module Gitlab
+ # Parser/renderer for the AsciiDoc format that uses Asciidoctor and filters
+ # the resulting HTML through HTML pipeline filters.
+ module Asciidoc
+
+ # Provide autoload paths for filters to prevent a circular dependency error
+ autoload :RelativeLinkFilter, 'gitlab/markdown/relative_link_filter'
+
+ DEFAULT_ADOC_ATTRS = [
+ 'showtitle', 'idprefix=user-content-', 'idseparator=-', 'env=gitlab',
+ 'env-gitlab', 'source-highlighter=html-pipeline'
+ ].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
+ # html_opts - a Hash of options for HTML output:
+ # :xhtml - output XHTML instead of HTML
+ #
+ def self.render(input, context, asciidoc_opts = {}, html_opts = {})
+ asciidoc_opts = asciidoc_opts.reverse_merge(
+ safe: :secure,
+ backend: html_opts[:xhtml] ? :xhtml5 : :html5,
+ attributes: []
+ )
+ asciidoc_opts[:attributes].unshift(*DEFAULT_ADOC_ATTRS)
+
+ html = ::Asciidoctor.convert(input, asciidoc_opts)
+
+ if context[:project]
+ result = HTML::Pipeline.new(filters).call(html, context)
+
+ save_opts = html_opts[:xhtml] ?
+ Nokogiri::XML::Node::SaveOptions::AS_XHTML : 0
+
+ html = result[:output].to_html(save_with: save_opts)
+ end
+
+ html.html_safe
+ end
+
+ private
+
+ def self.filters
+ [
+ Gitlab::Markdown::RelativeLinkFilter
+ ]
+ end
+ end
+end