Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly resolving target file location for block macros #447

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [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
45 changes: 42 additions & 3 deletions ruby/spec/asciidoctor_kroki_block_macro_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# rubocop:disable Lint/ConstantDefinitionInBlock
# frozen_string_literal: true

require 'tmpdir'
require 'rspec_helper'
require 'asciidoctor'
require_relative '../lib/asciidoctor/extensions/asciidoctor_kroki'
Expand All @@ -9,10 +10,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 +31,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 +85,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 +105,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(_, _)
nil
end
end
Expand All @@ -110,6 +123,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 All @@ -125,6 +142,28 @@ def unresolved_block_macro_message(name, target)
<p><strong>[ERROR: plantuml::spec/fixtures/missing.puml[] - unresolved block macro]</strong></p>
</div>)
end

it 'should properly resolve relative path to files' do
Dir.mktmpdir('rspec-') do |temp_dir|
temp_file = "#{temp_dir}/test.adoc"
assets_dir = "#{temp_dir}/_assets"

File.open(temp_file, 'w') do |f|
content = <<~'ADOC'
plantuml::_assets/alice.puml[svg,role=sequence]
ADOC
f.write(content)
end
Dir.mkdir(assets_dir)
FileUtils.cp('spec/fixtures/alice.puml', "#{assets_dir}/alice.puml")
Asciidoctor.convert_file(temp_file, standalone: false)
(expect File.read("#{temp_dir}/test.html")).to eql %(<div class="imageblock sequence kroki-format-svg kroki">
<div class="content">
<img src="https://kroki.io/plantuml/svg/eNpLzMlMTlXQtVNIyk-yUshIzcnJ5wIAQ-AGVQ==" alt="Diagram">
</div>
</div>)
end
end
end
end
# rubocop:enable Lint/ConstantDefinitionInBlock
Loading