Skip to content

Commit

Permalink
modify how plantuml injects itself in the kramdown process
Browse files Browse the repository at this point in the history
This commit changes how the injection happens from plain monkey
patching to using a module and prepending it into the class hierarchy.

This technique fixes compatibility with middleman. Middleman creates
a subclass of Kramdown::Converter::Html, and this was making the
method-overriding technique fail, while this one works.

To use in middleman, just require "kramdown-plantuml" in config.rb
  • Loading branch information
doudou committed Jun 30, 2022
1 parent 4a4114b commit 6e17c92
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
32 changes: 32 additions & 0 deletions lib/kramdown-plantuml/converter_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Kramdown
module PlantUml
# Plugs into Kramdown::Converter::Html to provide conversion of PlantUML markup
# into beautiful SVG.
module ConverterExtension
def convert_codeblock(element, indent)
return super(element, indent) unless plantuml? element

convert_plantuml(element.value)
end

private

def plantuml?(element)
element.attr['class'] == 'language-plantuml'
end

def convert_plantuml(plantuml)
puml_opts = PlantUml::Options.new(@options)
diagram = PlantUml::PlantUmlDiagram.new(plantuml, puml_opts)
diagram.svg.to_s
rescue StandardError => e
raise e if puml_opts.nil? || puml_opts.raise_errors?

logger = PlantUml::LogWrapper.init
logger.error "Error while converting diagram: #{e.inspect}"
end
end
end
end
34 changes: 2 additions & 32 deletions lib/kramdown_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,6 @@
require_relative 'kramdown-plantuml/plantuml_error'
require_relative 'kramdown-plantuml/options'
require_relative 'kramdown-plantuml/plantuml_diagram'
require_relative 'kramdown-plantuml/converter_extension'

module Kramdown
module Converter
# Plugs into Kramdown::Converter::Html to provide conversion of PlantUML markup
# into beautiful SVG.
class Html
alias super_convert_codeblock convert_codeblock

def convert_codeblock(element, indent)
return super_convert_codeblock(element, indent) unless plantuml? element

convert_plantuml(element.value)
end

private

def plantuml?(element)
element.attr['class'] == 'language-plantuml'
end

def convert_plantuml(plantuml)
puml_opts = ::Kramdown::PlantUml::Options.new(@options)
diagram = ::Kramdown::PlantUml::PlantUmlDiagram.new(plantuml, puml_opts)
diagram.svg.to_s
rescue StandardError => e
raise e if puml_opts.nil? || puml_opts.raise_errors?

logger = ::Kramdown::PlantUml::LogWrapper.init
logger.error "Error while converting diagram: #{e.inspect}"
end
end
end
end
Kramdown::Converter::Html.prepend Kramdown::PlantUml::ConverterExtension

0 comments on commit 6e17c92

Please sign in to comment.