summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/plantuml_filter.rb
blob: b2537117558746442c7bf99e3a4fd1ef05338193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
        return doc unless doc.at('pre.plantuml') && settings.plantuml_enabled

        plantuml_setup

        doc.css('pre.plantuml').each do |el|
          img_tag = Nokogiri::HTML::DocumentFragment.parse(
            Asciidoctor::PlantUml::Processor.plantuml_content(el.content, {}))
          el.replace img_tag
        end

        doc
      end

      private

      def settings
        ApplicationSetting.current || ApplicationSetting.create_from_defaults
      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