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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1.2 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
2021-12-11 22:18:48 +05:30
# HTML that replaces all `lang plantuml` tags with PlantUML img tags.
2017-08-17 22:00:37 +05:30
#
class PlantumlFilter < HTML::Pipeline::Filter
def call
2021-12-11 22:18:48 +05:30
return doc unless settings.plantuml_enabled? && doc.at_xpath(lang_tag)
2017-08-17 22:00:37 +05:30
2023-09-09 17:08:58 +05:30
Gitlab::Plantuml.configure
2017-08-17 22:00:37 +05:30
2021-12-11 22:18:48 +05:30
doc.xpath(lang_tag).each do |node|
2017-08-17 22:00:37 +05:30
img_tag = Nokogiri::HTML::DocumentFragment.parse(
2022-06-21 17:19:12 +05:30
Asciidoctor::PlantUml::Processor.plantuml_content(node.content, {})).css('img').first
2023-01-13 00:05:48 +05:30
next if img_tag.nil?
2022-06-21 17:19:12 +05:30
2023-01-13 00:05:48 +05:30
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)
2017-08-17 22:00:37 +05:30
end
doc
end
private
2021-12-11 22:18:48 +05:30
def lang_tag
2022-10-11 01:57:18 +05:30
@lang_tag ||= Gitlab::Utils::Nokogiri
2023-07-09 08:55:56 +05:30
.css_to_xpath('pre[data-canonical-lang="plantuml"] > code, pre > code[data-canonical-lang="plantuml"]').freeze
2021-12-11 22:18:48 +05:30
end
2017-08-17 22:00:37 +05:30
def settings
2018-11-08 19:23:39 +05:30
Gitlab::CurrentSettings.current_application_settings
2017-08-17 22:00:37 +05:30
end
end
end
end