2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-16 00:07:02 +00:00
|
|
|
require "nokogiri"
|
2019-12-24 12:08:01 +00:00
|
|
|
require "asciidoctor_plantuml/plantuml"
|
2017-01-16 00:07:02 +00:00
|
|
|
|
|
|
|
module Banzai
|
|
|
|
module Filter
|
2021-11-17 15:10:28 +00:00
|
|
|
# HTML that replaces all `lang plantuml` tags with PlantUML img tags.
|
2017-01-16 00:07:02 +00:00
|
|
|
#
|
|
|
|
class PlantumlFilter < HTML::Pipeline::Filter
|
|
|
|
def call
|
2021-11-17 15:10:28 +00:00
|
|
|
return doc unless settings.plantuml_enabled? && doc.at_xpath(lang_tag)
|
2017-01-16 00:07:02 +00:00
|
|
|
|
|
|
|
plantuml_setup
|
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
doc.xpath(lang_tag).each do |node|
|
2017-01-16 00:07:02 +00:00
|
|
|
img_tag = Nokogiri::HTML::DocumentFragment.parse(
|
2022-03-28 09:07:06 +00:00
|
|
|
Asciidoctor::PlantUml::Processor.plantuml_content(node.content, {})).css('img').first
|
|
|
|
|
|
|
|
unless img_tag.nil?
|
|
|
|
img_tag.set_attribute('data-diagram', 'plantuml')
|
|
|
|
img_tag.set_attribute('data-diagram-src', "data:text/plain;base64,#{Base64.strict_encode64(node.content)}")
|
|
|
|
|
|
|
|
node.parent.replace(img_tag)
|
|
|
|
end
|
2017-01-16 00:07:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
def lang_tag
|
2022-01-10 21:16:38 +00:00
|
|
|
@lang_tag ||= Gitlab::Utils::Nokogiri.css_to_xpath('pre[lang="plantuml"] > code').freeze
|
2021-11-17 15:10:28 +00:00
|
|
|
end
|
|
|
|
|
2017-01-16 00:07:02 +00:00
|
|
|
def settings
|
2018-05-16 15:10:04 +00:00
|
|
|
Gitlab::CurrentSettings.current_application_settings
|
2017-01-16 00:07:02 +00:00
|
|
|
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
|