diff options
author | Jakub Jirutka <jakub@jirutka.cz> | 2015-05-13 01:07:48 +0200 |
---|---|---|
committer | Jakub Jirutka <jakub@jirutka.cz> | 2015-05-18 20:48:03 +0200 |
commit | 8dbc4746fe7c723b67f3c90cbf40fd7bf6c29cb7 (patch) | |
tree | 268aba3884c8e2e09fdefda9cad7a4c569154cc2 /lib | |
parent | dc348baf18240dc05e209ec781daada2cbcfe16f (diff) | |
download | gitlab-ce-8dbc4746fe7c723b67f3c90cbf40fd7bf6c29cb7.tar.gz |
Handle AsciiDoc better, reuse HTML pipeline filters (fixes #9263)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/asciidoc.rb | 60 | ||||
-rw-r--r-- | lib/gitlab/markdown_helper.rb | 11 |
2 files changed, 70 insertions, 1 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 diff --git a/lib/gitlab/markdown_helper.rb b/lib/gitlab/markdown_helper.rb index 5e3cfc0585b..70384b1db2c 100644 --- a/lib/gitlab/markdown_helper.rb +++ b/lib/gitlab/markdown_helper.rb @@ -9,7 +9,7 @@ module Gitlab # Returns boolean def markup?(filename) filename.downcase.end_with?(*%w(.textile .rdoc .org .creole .wiki - .mediawiki .rst .adoc .asciidoc .asc)) + .mediawiki .rst .adoc .ad .asciidoc)) end # Public: Determines if a given filename is compatible with @@ -22,6 +22,15 @@ module Gitlab filename.downcase.end_with?(*%w(.mdown .md .markdown)) end + # Public: Determines if the given filename has AsciiDoc extension. + # + # filename - Filename string to check + # + # Returns boolean + def asciidoc?(filename) + filename.downcase.end_with?(*%w(.adoc .ad .asciidoc)) + end + def previewable?(filename) gitlab_markdown?(filename) || markup?(filename) end |