| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'rspec_helper' |
| 4 |
require 'asciidoctor' |
| 5 |
require_relative '../lib/asciidoctor/extensions/asciidoctor_kroki' |
| 6 |
|
| 7 |
describe AsciidoctorExtensions::KrokiBlockProcessor do |
| 8 |
context 'convert to html5' do |
| 9 |
it 'should convert a PlantUML block to an image' do |
| 10 |
input = <<~ADOC |
| 11 |
[plantuml] |
| 12 |
.... |
| 13 |
alice -> bob: hello |
| 14 |
.... |
| 15 |
ADOC |
| 16 |
output = Asciidoctor.convert(input, standalone: false) |
| 17 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 18 |
<div class="content"> |
| 19 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJBwA9iwZL" alt="Diagram"> |
| 20 |
</div> |
| 21 |
</div>) |
| 22 |
end |
| 23 |
it 'should only pass diagram options as query parameters' do |
| 24 |
input = <<~ADOC |
| 25 |
[plantuml,alice-bob,svg,role=sequence,width=100,format=svg,link=https://asciidoc.org/,align=center,float=right,theme=bluegray] |
| 26 |
.... |
| 27 |
alice -> bob: hello |
| 28 |
.... |
| 29 |
ADOC |
| 30 |
output = Asciidoctor.convert(input, standalone: false) |
| 31 |
(expect output).to eql %(<div class="imageblock right text-center sequence kroki-format-svg kroki"> |
| 32 |
<div class="content"> |
| 33 |
<a class="image" href="https://asciidoc.org/"><img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJBwA9iwZL?theme=bluegray" alt="alice-bob" width="100"></a> |
| 34 |
</div> |
| 35 |
</div>) |
| 36 |
end |
| 37 |
it 'should use the title attribute as the alt value' do |
| 38 |
input = <<~ADOC |
| 39 |
[plantuml,title="Alice saying hello to Bob"] |
| 40 |
.... |
| 41 |
alice -> bob: hello |
| 42 |
.... |
| 43 |
ADOC |
| 44 |
output = Asciidoctor.convert(input, standalone: false) |
| 45 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 46 |
<div class="content"> |
| 47 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJBwA9iwZL" alt="Alice saying hello to Bob"> |
| 48 |
</div> |
| 49 |
<div class="title">Figure 1. Alice saying hello to Bob</div> |
| 50 |
</div>) |
| 51 |
end |
| 52 |
it 'should use png if kroki-default-format is set to png' do |
| 53 |
input = <<~ADOC |
| 54 |
[plantuml] |
| 55 |
.... |
| 56 |
alice -> bob: hello |
| 57 |
.... |
| 58 |
ADOC |
| 59 |
output = Asciidoctor.convert(input, attributes: { 'kroki-default-format' => 'png' }, standalone: false) |
| 60 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 61 |
<div class="content"> |
| 62 |
<img src="https://kroki.io/plantuml/png/eNpLzMlMTlXQtVNIyk-yUshIzcnJBwA9iwZL" alt="Diagram"> |
| 63 |
</div> |
| 64 |
</div>) |
| 65 |
end |
| 66 |
it 'should use svg if kroki-default-format is set to png and the diagram type does not support png' do |
| 67 |
input = <<~ADOC |
| 68 |
[nomnoml] |
| 69 |
.... |
| 70 |
[Pirate|eyeCount: Int|raid();pillage()| |
| 71 |
[beard]--[parrot] |
| 72 |
[beard]-:>[foul mouth] |
| 73 |
] |
| 74 |
.... |
| 75 |
ADOC |
| 76 |
output = Asciidoctor.convert(input, attributes: { 'kroki-default-format' => 'png' }, standalone: false) |
| 77 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 78 |
<div class="content"> |
| 79 |
<img src="https://kroki.io/nomnoml/svg/eNqLDsgsSixJrUmtTHXOL80rsVLwzCupKUrMTNHQtC7IzMlJTE_V0KzhUlCITkpNLEqJ1dWNLkgsKsoviUUSs7KLTssvzVHIzS8tyYjligUAMhEd0g==" alt="Diagram"> |
| 80 |
</div> |
| 81 |
</div>) |
| 82 |
end |
| 83 |
it 'should include the plantuml-include file when safe mode is safe' do |
| 84 |
input = <<~ADOC |
| 85 |
[plantuml] |
| 86 |
.... |
| 87 |
alice -> bob: hello |
| 88 |
.... |
| 89 |
ADOC |
| 90 |
output = Asciidoctor.convert(input, |
| 91 |
attributes: { 'kroki-plantuml-include' => 'spec/fixtures/config.puml' }, |
| 92 |
standalone: false, safe: :safe) |
| 93 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 94 |
<div class="content"> |
| 95 |
<img src="https://kroki.io/plantuml/svg/eNorzs7MK0gsSsxVyM3Py0_OKMrPTVUoKSpN5eJKzMlMTlXQtVNIyk-yUshIzcnJBwCT9xBc" alt="Diagram"> |
| 96 |
</div> |
| 97 |
</div>) |
| 98 |
end |
| 99 |
it 'should normalize plantuml-include path when safe mode is safe' do |
| 100 |
input = <<~ADOC |
| 101 |
[plantuml] |
| 102 |
.... |
| 103 |
alice -> bob: hello |
| 104 |
.... |
| 105 |
ADOC |
| 106 |
output = Asciidoctor.convert(input, attributes: { 'kroki-plantuml-include' => '../../../spec/fixtures/config.puml' }, standalone: false, safe: :safe) |
| 107 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 108 |
<div class="content"> |
| 109 |
<img src="https://kroki.io/plantuml/svg/eNorzs7MK0gsSsxVyM3Py0_OKMrPTVUoKSpN5eJKzMlMTlXQtVNIyk-yUshIzcnJBwCT9xBc" alt="Diagram"> |
| 110 |
</div> |
| 111 |
</div>) |
| 112 |
end |
| 113 |
it 'should not include file which reside outside of the parent directory of the source when safe mode is safe' do |
| 114 |
input = <<~ADOC |
| 115 |
[plantuml] |
| 116 |
.... |
| 117 |
alice -> bob: hello |
| 118 |
.... |
| 119 |
ADOC |
| 120 |
output = Asciidoctor.convert(input, attributes: { 'kroki-plantuml-include' => '/etc/passwd' }, standalone: false, safe: :safe) |
| 121 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 122 |
<div class="content"> |
| 123 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJBwA9iwZL" alt="Diagram"> |
| 124 |
</div> |
| 125 |
</div>) |
| 126 |
end |
| 127 |
it 'should not include file when safe mode is secure' do |
| 128 |
input = <<~ADOC |
| 129 |
[plantuml] |
| 130 |
.... |
| 131 |
alice -> bob: hello |
| 132 |
.... |
| 133 |
ADOC |
| 134 |
output = Asciidoctor.convert(input, attributes: { 'kroki-plantuml-include' => 'spec/fixtures/config.puml' }, standalone: false, safe: :secure) |
| 135 |
(expect output).to eql %(<div class="imageblock kroki"> |
| 136 |
<div class="content"> |
| 137 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJBwA9iwZL" alt="Diagram"> |
| 138 |
</div> |
| 139 |
</div>) |
| 140 |
end |
| 141 |
it 'should create SVG diagram in imagesdir if kroki-fetch-diagram is set' do |
| 142 |
input = <<~ADOC |
| 143 |
:imagesdir: .asciidoctor/kroki |
| 144 |
|
| 145 |
plantuml::spec/fixtures/alice.puml[svg,role=sequence] |
| 146 |
ADOC |
| 147 |
output = Asciidoctor.convert(input, attributes: { 'kroki-fetch-diagram' => '' }, standalone: false, safe: :safe) |
| 148 |
(expect output).to eql %(<div class="imageblock sequence kroki-format-svg kroki"> |
| 149 |
<div class="content"> |
| 150 |
<img src=".asciidoctor/kroki/diag-f6acdc206506b6ca7badd3fe722f252af992871426e580c8361ff4d47c2c7d9b.svg" alt="Diagram"> |
| 151 |
</div> |
| 152 |
</div>) |
| 153 |
end |
| 154 |
it 'should not fetch diagram when safe mode is secure' do |
| 155 |
input = <<~ADOC |
| 156 |
:imagesdir: .asciidoctor/kroki |
| 157 |
|
| 158 |
plantuml::spec/fixtures/alice.puml[svg,role=sequence] |
| 159 |
ADOC |
| 160 |
output = Asciidoctor.convert(input, attributes: { 'kroki-fetch-diagram' => '' }, standalone: false) |
| 161 |
(expect output).to eql %(<div class="imageblock sequence kroki-format-svg kroki"> |
| 162 |
<div class="content"> |
| 163 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJ5wIAQ-AGVQ==" alt="Diagram"> |
| 164 |
</div> |
| 165 |
</div>) |
| 166 |
end |
| 167 |
it 'should create PNG diagram in imagesdir if kroki-fetch-diagram is set' do |
| 168 |
input = <<~ADOC |
| 169 |
:imagesdir: .asciidoctor/kroki |
| 170 |
|
| 171 |
plantuml::spec/fixtures/alice.puml[png,role=sequence] |
| 172 |
ADOC |
| 173 |
output = Asciidoctor.convert(input, attributes: { 'kroki-fetch-diagram' => '' }, standalone: false, safe: :safe) |
| 174 |
(expect output).to eql %(<div class="imageblock sequence kroki-format-png kroki"> |
| 175 |
<div class="content"> |
| 176 |
<img src=".asciidoctor/kroki/diag-d4f314b2d4e75cc08aa4f8c2c944f7bf78321895d8ec5f665b42476d4e67e610.png" alt="Diagram"> |
| 177 |
</div> |
| 178 |
</div>) |
| 179 |
end |
| 180 |
end |
| 181 |
context 'instantiate' do |
| 182 |
it 'should instantiate block processor without warning' do |
| 183 |
original_stderr = $stderr |
| 184 |
$stderr = StringIO.new |
| 185 |
AsciidoctorExtensions::KrokiBlockProcessor.new :plantuml, {} |
| 186 |
output = $stderr.string |
| 187 |
(expect output).to eql '' |
| 188 |
ensure |
| 189 |
$stderr = original_stderr |
| 190 |
end |
| 191 |
end |
| 192 |
end |
| 193 |
|
| 194 |
describe AsciidoctorExtensions::Kroki do |
| 195 |
it 'should return the list of supported diagrams' do |
| 196 |
diagram_names = AsciidoctorExtensions::Kroki::SUPPORTED_DIAGRAM_NAMES |
| 197 |
expect(diagram_names).to include('vegalite', 'plantuml', 'bytefield', 'bpmn', 'excalidraw', 'wavedrom', 'pikchr', 'structurizr', 'diagramsnet') |
| 198 |
end |
| 199 |
it 'should register the extension for the list of supported diagrams' do |
| 200 |
doc = Asciidoctor::Document.new |
| 201 |
registry = Asciidoctor::Extensions::Registry.new |
| 202 |
registry.activate doc |
| 203 |
AsciidoctorExtensions::Kroki::SUPPORTED_DIAGRAM_NAMES.each do |name| |
| 204 |
expect(registry.find_block_extension(name)).to_not be_nil, "expected block extension named '#{name}' to be registered" |
| 205 |
expect(registry.find_block_macro_extension(name)).to_not be_nil, "expected block macro extension named '#{name}' to be registered " |
| 206 |
end |
| 207 |
end |
| 208 |
end |