debian-mirror-gitlab/lib/banzai/filter/plantuml_filter.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require "nokogiri"
2020-03-13 15:44:24 +05:30
require "asciidoctor_plantuml/plantuml"
2017-08-17 22:00:37 +05:30
module Banzai
module Filter
# HTML that replaces all `code plantuml` tags with PlantUML img tags.
#
class PlantumlFilter < HTML::Pipeline::Filter
2021-06-02 17:11:27 +05:30
CSS = 'pre > code[lang="plantuml"]'
XPATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze
2017-08-17 22:00:37 +05:30
def call
2021-06-02 17:11:27 +05:30
return doc unless settings.plantuml_enabled? && doc.at_xpath(XPATH)
2017-08-17 22:00:37 +05:30
plantuml_setup
2021-06-02 17:11:27 +05:30
doc.xpath(XPATH).each do |node|
2017-08-17 22:00:37 +05:30
img_tag = Nokogiri::HTML::DocumentFragment.parse(
Asciidoctor::PlantUml::Processor.plantuml_content(node.content, {}))
node.parent.replace(img_tag)
end
doc
end
private
def settings
2018-11-08 19:23:39 +05:30
Gitlab::CurrentSettings.current_application_settings
2017-08-17 22:00:37 +05:30
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