BigW Consortium Gitlab

asciidoc.rb 1.87 KB
Newer Older
1
require 'asciidoctor'
2
require 'asciidoctor/converter/html5'
3
require "asciidoctor-plantuml"
4 5 6 7 8 9 10

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',
11
      'env-gitlab', 'source-highlighter=html-pipeline', 'icons=font'
12 13 14 15 16 17
    ].freeze

    # Public: Converts the provided Asciidoc markup into HTML.
    #
    # input         - the source text in Asciidoc format
    #
18
    def self.render(input, context)
19 20 21
      asciidoc_opts = { safe: :secure,
                        backend: :gitlab_html5,
                        attributes: DEFAULT_ADOC_ATTRS }
22

23
      context[:pipeline] = :ascii_doc
24

25 26
      plantuml_setup

27
      html = ::Asciidoctor.convert(input, asciidoc_opts)
28
      html = Banzai.render(html, context)
29 30
      html.html_safe
    end
31

32 33
    def self.plantuml_setup
      Asciidoctor::PlantUml.configure do |conf|
34 35 36
        conf.url = current_application_settings.plantuml_url
        conf.svg_enable = current_application_settings.plantuml_enabled
        conf.png_enable = current_application_settings.plantuml_enabled
37 38 39 40
        conf.txt_enable = false
      end
    end

41 42 43 44 45 46 47 48
    class Html5Converter < Asciidoctor::Converter::Html5Converter
      extend Asciidoctor::Converter::Config

      register_for 'gitlab_html5'

      def stem(node)
        return super unless node.style.to_sym == :latexmath

49
        %(<pre#{id_attribute(node)} data-math-style="display"><code>#{node.content}</code></pre>)
50 51 52 53 54
      end

      def inline_quoted(node)
        return super unless node.type.to_sym == :latexmath

55
        %(<code#{id_attribute(node)} data-math-style="inline">#{node.text}</code>)
56 57 58 59 60 61 62 63 64 65
      end

      private

      def id_attribute(node)
        node.id ? %( id="#{node.id}") : nil
      end
    end
  end
end