| 1 |
# rubocop:disable Lint/ConstantDefinitionInBlock |
| 2 |
# frozen_string_literal: true |
| 3 |
|
| 4 |
require 'tmpdir' |
| 5 |
require 'rspec_helper' |
| 6 |
require 'asciidoctor' |
| 7 |
require_relative '../lib/asciidoctor/extensions/asciidoctor_kroki' |
| 8 |
require_relative '../lib/asciidoctor/extensions/asciidoctor_kroki/extension' |
| 9 |
|
| 10 |
describe AsciidoctorExtensions::KrokiBlockMacroProcessor do |
| 11 |
context 'convert to html5' do |
| 12 |
it 'should catch exception if target is not readable' do |
| 13 |
class PlainResolutionKrokiMacroProcessor < AsciidoctorExtensions::KrokiBlockMacroProcessor |
| 14 |
def resolve_target_path(_parent, target) |
| 15 |
target |
| 16 |
end |
| 17 |
end |
| 18 |
registry = Asciidoctor::Extensions.create do |
| 19 |
block_macro PlainResolutionKrokiMacroProcessor, 'plantuml' |
| 20 |
end |
| 21 |
input = <<~ADOC |
| 22 |
plantuml::spec/fixtures/missing.puml[svg,role=sequence] |
| 23 |
ADOC |
| 24 |
output = Asciidoctor.convert(input, standalone: false, extension_registry: registry) |
| 25 |
(expect output).to eql %(<div class="paragraph"> |
| 26 |
<p>Unresolved block macro - plantuml::spec/fixtures/missing.puml[]</p> |
| 27 |
</div>) |
| 28 |
end |
| 29 |
end |
| 30 |
context 'using a custom block macro' do |
| 31 |
it 'should disallow read' do |
| 32 |
# noinspection RubyClassModuleNamingConvention |
| 33 |
class DisallowReadKrokiBlockMacroProcessor < AsciidoctorExtensions::KrokiBlockMacroProcessor |
| 34 |
def resolve_target_path(_parent, target) |
| 35 |
target |
| 36 |
end |
| 37 |
|
| 38 |
def read_allowed?(_target) |
| 39 |
false |
| 40 |
end |
| 41 |
end |
| 42 |
registry = Asciidoctor::Extensions.create do |
| 43 |
block_macro DisallowReadKrokiBlockMacroProcessor, 'plantuml' |
| 44 |
end |
| 45 |
input = <<~ADOC |
| 46 |
plantuml::spec/fixtures/alice.puml[svg,role=sequence] |
| 47 |
ADOC |
| 48 |
output = Asciidoctor.convert(input, standalone: false, extension_registry: registry) |
| 49 |
(expect output).to eql %(<div class="paragraph"> |
| 50 |
<p><a href="spec/fixtures/alice.puml">spec/fixtures/alice.puml</a></p> |
| 51 |
</div>) |
| 52 |
end |
| 53 |
it 'should allow read if target is not a URI' do |
| 54 |
# noinspection RubyClassModuleNamingConvention |
| 55 |
class DisallowUriReadKrokiBlockMacroProcessor < AsciidoctorExtensions::KrokiBlockMacroProcessor |
| 56 |
def read_allowed?(target) |
| 57 |
return false if ::Asciidoctor::Helpers.uriish?(target) |
| 58 |
|
| 59 |
true |
| 60 |
end |
| 61 |
end |
| 62 |
registry = Asciidoctor::Extensions.create do |
| 63 |
block_macro DisallowUriReadKrokiBlockMacroProcessor, 'plantuml' |
| 64 |
end |
| 65 |
input = <<~ADOC |
| 66 |
plantuml::https://domain.org/alice.puml[svg,role=sequence] |
| 67 |
|
| 68 |
plantuml::file://path/to/alice.puml[svg,role=sequence] |
| 69 |
|
| 70 |
plantuml::spec/fixtures/alice.puml[svg,role=sequence] |
| 71 |
ADOC |
| 72 |
output = Asciidoctor.convert(input, standalone: false, extension_registry: registry) |
| 73 |
(expect output).to eql %(<div class="paragraph"> |
| 74 |
<p><a href="https://domain.org/alice.puml">https://domain.org/alice.puml</a></p> |
| 75 |
</div> |
| 76 |
<div class="paragraph"> |
| 77 |
<p><a href="file://path/to/alice.puml">file://path/to/alice.puml</a></p> |
| 78 |
</div> |
| 79 |
<div class="imageblock sequence kroki-format-svg kroki"> |
| 80 |
<div class="content"> |
| 81 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJ5wIAQ-AGVQ==" alt="Diagram"> |
| 82 |
</div> |
| 83 |
</div>) |
| 84 |
end |
| 85 |
it 'should override the resolve target method' do |
| 86 |
# noinspection RubyClassModuleNamingConvention |
| 87 |
class FixtureResolveTargetKrokiBlockMacroProcessor < AsciidoctorExtensions::KrokiBlockMacroProcessor |
| 88 |
def resolve_target_path(_parent, target) |
| 89 |
"spec/fixtures/#{target}" |
| 90 |
end |
| 91 |
end |
| 92 |
registry = Asciidoctor::Extensions.create do |
| 93 |
block_macro FixtureResolveTargetKrokiBlockMacroProcessor, 'plantuml' |
| 94 |
end |
| 95 |
input = <<~ADOC |
| 96 |
plantuml::alice.puml[svg,role=sequence] |
| 97 |
ADOC |
| 98 |
output = Asciidoctor.convert(input, standalone: false, extension_registry: registry) |
| 99 |
(expect output).to eql %(<div class="imageblock sequence kroki-format-svg kroki"> |
| 100 |
<div class="content"> |
| 101 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJ5wIAQ-AGVQ==" alt="Diagram"> |
| 102 |
</div> |
| 103 |
</div>) |
| 104 |
end |
| 105 |
it 'should display unresolved block macro message when the target cannot be resolved' do |
| 106 |
# noinspection RubyClassModuleNamingConvention |
| 107 |
class UnresolvedTargetKrokiBlockMacroProcessor < AsciidoctorExtensions::KrokiBlockMacroProcessor |
| 108 |
def resolve_target_path(_, _) |
| 109 |
nil |
| 110 |
end |
| 111 |
end |
| 112 |
registry = Asciidoctor::Extensions.create do |
| 113 |
block_macro UnresolvedTargetKrokiBlockMacroProcessor, 'plantuml' |
| 114 |
end |
| 115 |
input = <<~ADOC |
| 116 |
plantuml::alice.puml[svg,role=sequence] |
| 117 |
ADOC |
| 118 |
output = Asciidoctor.convert(input, standalone: false, extension_registry: registry) |
| 119 |
(expect output).to eql %(<div class="paragraph"> |
| 120 |
<p>Unresolved block macro - plantuml::alice.puml[]</p> |
| 121 |
</div>) |
| 122 |
end |
| 123 |
it 'should override the unresolved block macro message' do |
| 124 |
# noinspection RubyClassModuleNamingConvention |
| 125 |
class CustomUnresolvedTargetMessageKrokiBlockMacroProcessor < AsciidoctorExtensions::KrokiBlockMacroProcessor |
| 126 |
def resolve_target_path(_parent, target) |
| 127 |
target |
| 128 |
end |
| 129 |
|
| 130 |
def unresolved_block_macro_message(name, target) |
| 131 |
"*[ERROR: #{name}::#{target}[] - unresolved block macro]*" |
| 132 |
end |
| 133 |
end |
| 134 |
registry = Asciidoctor::Extensions.create do |
| 135 |
block_macro CustomUnresolvedTargetMessageKrokiBlockMacroProcessor, 'plantuml' |
| 136 |
end |
| 137 |
input = <<~ADOC |
| 138 |
plantuml::spec/fixtures/missing.puml[svg,role=sequence] |
| 139 |
ADOC |
| 140 |
output = Asciidoctor.convert(input, standalone: false, extension_registry: registry) |
| 141 |
(expect output).to eql %(<div class="paragraph"> |
| 142 |
<p><strong>[ERROR: plantuml::spec/fixtures/missing.puml[] - unresolved block macro]</strong></p> |
| 143 |
</div>) |
| 144 |
end |
| 145 |
|
| 146 |
it 'should properly resolve relative path to files' do |
| 147 |
Dir.mktmpdir('rspec-') do |temp_dir| |
| 148 |
temp_file = "#{temp_dir}/test.adoc" |
| 149 |
assets_dir = "#{temp_dir}/_assets" |
| 150 |
|
| 151 |
File.open(temp_file, 'w') do |f| |
| 152 |
content = <<~ADOC |
| 153 |
plantuml::_assets/alice.puml[svg,role=sequence] |
| 154 |
ADOC |
| 155 |
f.write(content) |
| 156 |
end |
| 157 |
Dir.mkdir(assets_dir) |
| 158 |
FileUtils.cp('spec/fixtures/alice.puml', "#{assets_dir}/alice.puml") |
| 159 |
Asciidoctor.convert_file(temp_file, standalone: false) |
| 160 |
(expect File.read("#{temp_dir}/test.html")).to eql %(<div class="imageblock sequence kroki-format-svg kroki"> |
| 161 |
<div class="content"> |
| 162 |
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJ5wIAQ-AGVQ==" alt="Diagram"> |
| 163 |
</div> |
| 164 |
</div>) |
| 165 |
end |
| 166 |
end |
| 167 |
end |
| 168 |
end |
| 169 |
# rubocop:enable Lint/ConstantDefinitionInBlock |