Skip to content

Commit

Permalink
Merge pull request #106 from tidewise/use_prepend_to_inject
Browse files Browse the repository at this point in the history
modify how plantuml injects itself in the kramdown process
  • Loading branch information
asbjornu authored Jul 19, 2022
2 parents 4a4114b + 6e17c92 commit 96700f9
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 96700f9

Please sign in to comment.