Skip to content

Commit 2b7073c

Browse files
authored
Asciidoctor: Better handling for resources attribute (#574)
Prevents the copy images asciidoctor extension from dying if the `resources` attribute is an empty string and improves the error message if it is invalid CSV.
1 parent 4b9ede0 commit 2b7073c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

resources/asciidoctor/lib/copy_images/extension.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ def find_source block, uri
6363
checked = []
6464

6565
resources = block.document.attr 'resources'
66-
if resources
67-
to_check += CSV.parse_line(resources)
66+
if resources and not resources.empty?
67+
begin
68+
to_check += CSV.parse_line(resources)
69+
rescue CSV::MalformedCSVError => error
70+
logger.error message_with_context "Error loading [resources]: #{error}",
71+
:source_location => block.source_location
72+
end
6873
end
6974

7075
while (dir = to_check.shift)

resources/asciidoctor/spec/copy_images_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,39 @@ def copy_attributes copied
163163
}
164164
end
165165

166+
it "doesn't mind an empty resources attribute" do
167+
copied = []
168+
attributes = copy_attributes copied
169+
attributes['resources'] = ''
170+
input = <<~ASCIIDOC
171+
== Example
172+
image::example1.png[]
173+
ASCIIDOC
174+
convert input, attributes,
175+
eq("INFO: <stdin>: line 2: copying #{spec_dir}/resources/copy_images/example1.png")
176+
expect(copied).to eq([
177+
["example1.png", "#{spec_dir}/resources/copy_images/example1.png"]
178+
])
179+
end
180+
181+
it "has a nice error message if resources is invalid CSV" do
182+
copied = []
183+
attributes = copy_attributes copied
184+
attributes['resources'] = '"'
185+
input = <<~ASCIIDOC
186+
== Example
187+
image::example1.png[]
188+
ASCIIDOC
189+
expected_warnings = <<~LOG
190+
ERROR: <stdin>: line 2: Error loading [resources]: Unclosed quoted field on line 1.
191+
INFO: <stdin>: line 2: copying #{spec_dir}/resources/copy_images/example1.png
192+
LOG
193+
convert input, attributes, eq(expected_warnings.strip)
194+
expect(copied).to eq([
195+
["example1.png", "#{spec_dir}/resources/copy_images/example1.png"]
196+
])
197+
end
198+
166199
it "has a nice error message when it can't find a file with single valued resources attribute" do
167200
Dir.mktmpdir {|tmp|
168201
copied = []

0 commit comments

Comments
 (0)