plantuml_filter.rb 1.02 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9 10 11
require "nokogiri"
require "asciidoctor-plantuml/plantuml"

module Banzai
  module Filter
    # HTML that replaces all `code plantuml` tags with PlantUML img tags.
    #
    class PlantumlFilter < HTML::Pipeline::Filter
      def call
12
        return doc unless doc.at('pre > code[lang="plantuml"]') && settings.plantuml_enabled
13 14 15

        plantuml_setup

16
        doc.css('pre > code[lang="plantuml"]').each do |node|
17
          img_tag = Nokogiri::HTML::DocumentFragment.parse(
18 19
            Asciidoctor::PlantUml::Processor.plantuml_content(node.content, {}))
          node.parent.replace(img_tag)
20 21 22 23 24 25 26 27
        end

        doc
      end

      private

      def settings
28
        Gitlab::CurrentSettings.current_application_settings
29 30 31 32 33 34 35 36 37 38 39 40 41
      end

      def plantuml_setup
        Asciidoctor::PlantUml.configure do |conf|
          conf.url = settings.plantuml_url
          conf.png_enable = settings.plantuml_enabled
          conf.svg_enable = false
          conf.txt_enable = false
        end
      end
    end
  end
end