Skip to content

Commit

Permalink
Properly resolving target file location for block macros
Browse files Browse the repository at this point in the history
Fixes #446
  • Loading branch information
zeldigas committed Mar 24, 2024
1 parent 43dfd75 commit 66350e7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
asciidoctor-kroki (0.10.0)
asciidoctor-kroki (0.10.1)
asciidoctor (~> 2.0)

GEM
Expand Down
14 changes: 11 additions & 3 deletions ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def initialize(name = nil, config = {})
super(name, config)
end

# Processes the diagram block or block macro by converting it into an image or literal block.
#
# @param parent [Asciidoctor::AbstractBlock] the parent asciidoc block of the block or block macro being processed
# @param target [String] the target value of a block macro
# @param attrs [Hash] the attributes of the block or block macro
# @return [Asciidoctor::AbstractBlock] a new block that replaces the original block or block macro
def process(parent, target, attrs)
diagram_type = @name
target = parent.apply_subs(target, [:attributes])
Expand All @@ -62,7 +68,7 @@ def process(parent, target, attrs)
return create_block(parent, :paragraph, link.convert, {}, content_model: :raw)
end

unless (path = resolve_target_path(target))
unless (path = resolve_target_path(parent, target))
logger.error message_with_context "#{diagram_type} block macro not found: #{target}.", source_location: parent.document.reader.cursor_at_mark
return create_block(parent, 'paragraph', unresolved_block_macro_message(diagram_type, target), {})
end
Expand All @@ -80,8 +86,10 @@ def process(parent, target, attrs)

attr_reader :logger

def resolve_target_path(target)
target
# @param parent [Asciidoctor::AbstractBlock] the parent asciidoc block of the block or block macro being processed
# @param target [Asciidoctor::Reader, String] the target value of a block macro
def resolve_target_path(parent, target)
parent.normalize_system_path(target)
end

def read_allowed?(_target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Asciidoctor
module AsciidoctorKroki
VERSION = '0.10.0'
VERSION = '0.10.1'
end
end
22 changes: 19 additions & 3 deletions ruby/spec/asciidoctor_kroki_block_macro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
describe ::AsciidoctorExtensions::KrokiBlockMacroProcessor do
context 'convert to html5' do
it 'should catch exception if target is not readable' do
class PlainResolutionKrokiMacroProcessor < ::AsciidoctorExtensions::KrokiBlockMacroProcessor
def resolve_target_path(_parent, target)
target
end
end
registry = Asciidoctor::Extensions.create do
block_macro PlainResolutionKrokiMacroProcessor, 'plantuml'
end
input = <<~'ADOC'
plantuml::spec/fixtures/missing.puml[svg,role=sequence]
ADOC
output = Asciidoctor.convert(input, standalone: false)
output = Asciidoctor.convert(input, standalone: false, extension_registry: registry)
(expect output).to eql %(<div class="paragraph">
<p>Unresolved block macro - plantuml::spec/fixtures/missing.puml[]</p>
</div>)
Expand All @@ -22,6 +30,10 @@
it 'should disallow read' do
# noinspection RubyClassModuleNamingConvention
class DisallowReadKrokiBlockMacroProcessor < ::AsciidoctorExtensions::KrokiBlockMacroProcessor
def resolve_target_path(_parent, target)
target
end

def read_allowed?(_target)
false
end
Expand Down Expand Up @@ -72,7 +84,7 @@ def read_allowed?(target)
it 'should override the resolve target method' do
# noinspection RubyClassModuleNamingConvention
class FixtureResolveTargetKrokiBlockMacroProcessor < ::AsciidoctorExtensions::KrokiBlockMacroProcessor
def resolve_target_path(target)
def resolve_target_path(_parent, target)
"spec/fixtures/#{target}"
end
end
Expand All @@ -92,7 +104,7 @@ def resolve_target_path(target)
it 'should display unresolved block macro message when the target cannot be resolved' do
# noinspection RubyClassModuleNamingConvention
class UnresolvedTargetKrokiBlockMacroProcessor < ::AsciidoctorExtensions::KrokiBlockMacroProcessor
def resolve_target_path(_target)
def resolve_target_path(_target, _parent)
nil
end
end
Expand All @@ -110,6 +122,10 @@ def resolve_target_path(_target)
it 'should override the unresolved block macro message' do
# noinspection RubyClassModuleNamingConvention
class CustomUnresolvedTargetMessageKrokiBlockMacroProcessor < ::AsciidoctorExtensions::KrokiBlockMacroProcessor
def resolve_target_path(_parent, target)
target
end

def unresolved_block_macro_message(name, target)
"*[ERROR: #{name}::#{target}[] - unresolved block macro]*"
end
Expand Down

0 comments on commit 66350e7

Please sign in to comment.