diff --git a/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb b/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb index d25049c0..6213283c 100644 --- a/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb +++ b/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb @@ -208,13 +208,13 @@ def get_role(format, role) end def get_format(doc, attrs, diagram_type) - format = attrs['format'] || 'svg' - # The JavaFX preview doesn't support SVG well, therefore we'll use PNG format... - if doc.attr?('env-idea') && format == 'svg' - # ... unless the diagram library does not support PNG as output format! + format = attrs['format'] || doc.attr('kroki-default-format') || 'svg' + if format == 'png' + # redirect PNG format to SVG if the diagram library only supports SVG as output format. + # this is useful when the default format has been set to PNG # Currently, mermaid, nomnoml, svgbob, wavedrom only support SVG as output format. - svg_only_diagram_types = %w[:mermaid :nomnoml :svgbob :wavedrom] - format = 'png' unless svg_only_diagram_types.include?(diagram_type) + svg_only_diagram_types = %i[mermaid nomnoml svgbob wavedrom] + format = 'svg' if svg_only_diagram_types.include?(diagram_type) end format end diff --git a/ruby/spec/asciidoctor_kroki_spec.rb b/ruby/spec/asciidoctor_kroki_spec.rb index 37a6137b..70fdf840 100644 --- a/ruby/spec/asciidoctor_kroki_spec.rb +++ b/ruby/spec/asciidoctor_kroki_spec.rb @@ -20,18 +20,33 @@ ) end - it 'should use png if env-idea is defined' do + it 'should use png if kroki-default-format is set to png' do input = <<~'ADOC' [plantuml] .... alice -> bob: hello .... ADOC - output = Asciidoctor.convert(input, attributes: { 'env-idea' => '' }, standalone: false) + output = Asciidoctor.convert(input, attributes: { 'kroki-default-format' => 'png' }, standalone: false) (expect output).to eql %(
Diagram
+
) + end + it 'should use svg if kroki-default-format is set to png and the diagram type does not support png' do + input = <<~'ADOC' + [mermaid] + .... + graph TD; + A-->B; + .... + ADOC + output = Asciidoctor.convert(input, attributes: { 'kroki-default-format' => 'png' }, standalone: false) + (expect output).to eql %(
+
+Diagram +
) end it 'should include the plantuml-include file when safe mode is safe' do @@ -41,10 +56,12 @@ alice -> bob: hello .... ADOC - output = Asciidoctor.convert(input, attributes: { 'env-idea' => '', 'kroki-plantuml-include' => 'spec/fixtures/config.puml' }, standalone: false, safe: :safe) + output = Asciidoctor.convert(input, + attributes: { 'kroki-plantuml-include' => 'spec/fixtures/config.puml' }, + standalone: false, safe: :safe) (expect output).to eql %(
-Diagram +Diagram
) end @@ -55,10 +72,10 @@ alice -> bob: hello .... ADOC - output = Asciidoctor.convert(input, attributes: { 'env-idea' => '', 'kroki-plantuml-include' => '../../../spec/fixtures/config.puml' }, standalone: false, safe: :safe) + output = Asciidoctor.convert(input, attributes: { 'kroki-plantuml-include' => '../../../spec/fixtures/config.puml' }, standalone: false, safe: :safe) (expect output).to eql %(
-Diagram +Diagram
) end @@ -69,10 +86,10 @@ alice -> bob: hello .... ADOC - output = Asciidoctor.convert(input, attributes: { 'env-idea' => '', 'kroki-plantuml-include' => '/etc/passwd' }, standalone: false, safe: :safe) + output = Asciidoctor.convert(input, attributes: { 'kroki-plantuml-include' => '/etc/passwd' }, standalone: false, safe: :safe) (expect output).to eql %(
-Diagram +Diagram
) end @@ -83,10 +100,10 @@ alice -> bob: hello .... ADOC - output = Asciidoctor.convert(input, attributes: { 'env-idea' => '', 'kroki-plantuml-include' => 'spec/fixtures/config.puml' }, standalone: false, safe: :secure) + output = Asciidoctor.convert(input, attributes: { 'kroki-plantuml-include' => 'spec/fixtures/config.puml' }, standalone: false, safe: :secure) (expect output).to eql %(
-Diagram +Diagram
) end