Skip to content

Commit aaea37a

Browse files
authored
Asciidoctor: Copy callout icons (#570)
Adds support for copying callout icons into the directory containing the built documents. Without this callout icons all 404 which is sad because they are pretty.
2 parents 0fe8640 + 97c45fb commit aaea37a

File tree

7 files changed

+171
-7
lines changed

7 files changed

+171
-7
lines changed

lib/ES/Util.pm

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ sub build_chunked {
7575
# Emulate asciidoc_dir because we use it to find shared asciidoc files
7676
# but asciidoctor doesn't support it.
7777
my $asciidoc_dir = dir('resources/asciidoc-8.6.8/')->absolute;
78+
# We use the callouts from asciidoc so add it as a resource so we
79+
# can find them
80+
push @$resources, $asciidoc_dir;
7881
eval {
7982
$output = run(
8083
'asciidoctor', '-v', '--trace',
@@ -92,7 +95,8 @@ sub build_chunked {
9295
# missing attributes!
9396
# '-a' => 'attribute-missing=warn',
9497
'-a' => 'asciidoc-dir=' . $asciidoc_dir,
95-
$resources ? ( '-a' => 'resources=' . join(',', @$resources)) : (),
98+
'-a' => 'resources=' . join(',', @$resources),
99+
'-a' => 'copy-callout-images=png',
96100
'--destination-dir=' . $dest,
97101
docinfo($index),
98102
$index
@@ -107,7 +111,7 @@ sub build_chunked {
107111
file('resources/website_chunked.xsl')->absolute,
108112
"$dest/index.xml"
109113
);
110-
unlink "$dest/index.xml";
114+
# unlink "$dest/index.xml";
111115
1;
112116
} or do { $output = $@; $died = 1; };
113117
}
@@ -198,7 +202,9 @@ sub build_single {
198202
# Emulate asciidoc_dir because we use it to find shared asciidoc files
199203
# but asciidoctor doesn't support it.
200204
my $asciidoc_dir = dir('resources/asciidoc-8.6.8/')->absolute;
201-
205+
# We use the callouts from asciidoc so add it as a resource so we
206+
# can find them
207+
push @$resources, $asciidoc_dir;
202208
eval {
203209
$output = run(
204210
'asciidoctor', '-v', '--trace',
@@ -210,7 +216,7 @@ sub build_single {
210216
'-a' => 'repo_root=' . $root_dir,
211217
$private ? () : ( '-a' => "edit_url=$edit_url" ),
212218
'-a' => 'asciidoc-dir=' . $asciidoc_dir,
213-
$resources ? ( '-a' => 'resources=' . join(',', @$resources)) : (),
219+
'-a' => 'resources=' . join(',', @$resources),
214220
# Disable warning on missing attributes because we have
215221
# missing attributes!
216222
# '-a' => 'attribute-missing=warn',

resources/asciidoctor/lib/copy_images/extension.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
##
99
# Copies images that are referenced into the same directory as the output files.
1010
#
11+
# It finds the images by looking in a comma separated list of directories
12+
# defined by the `resources` attribute.
13+
#
14+
# It can also be configured to copy the images that number callout lists by
15+
# setting `copy-callout-images` to the file extension of the images to copy.
16+
#
1117
class CopyImages < TreeProcessorScaffold
1218
include Logging
1319

@@ -17,9 +23,20 @@ def initialize name
1723
end
1824

1925
def process_block block
20-
return unless block.context == :image
21-
uri = block.image_uri(block.attr 'target')
22-
return if Helpers.uriish? uri # Skip external images
26+
if block.context == :image
27+
uri = block.image_uri(block.attr 'target')
28+
return if Helpers.uriish? uri # Skip external images
29+
copy_image block, uri
30+
elsif (extension = block.document.attr 'copy-callout-images') &&
31+
block.parent &&
32+
block.parent.context == :colist
33+
id = block.attr('coids').scan(/CO(?:\d+)-(\d+)/) {
34+
copy_image block, "images/icons/callouts/#{$1}.#{extension}"
35+
}
36+
end
37+
end
38+
39+
def copy_image block, uri
2340
return unless @copied.add? uri # Skip images we've copied before
2441
source = find_source block, uri
2542
return unless source # Skip images we can't find

resources/asciidoctor/spec/copy_images_spec.rb

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,145 @@ def copy_attributes copied
201201
expect(copied).to eq([])
202202
}
203203
end
204+
205+
it "copies images for callouts when requested (png)" do
206+
copied = []
207+
attributes = copy_attributes copied
208+
attributes['copy-callout-images'] = 'png'
209+
input = <<~ASCIIDOC
210+
== Example
211+
----
212+
foo <1> <2>
213+
----
214+
<1> words
215+
<2> words
216+
ASCIIDOC
217+
expected_warnings = <<~WARNINGS
218+
INFO: <stdin>: line 5: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.png
219+
INFO: <stdin>: line 6: copying #{spec_dir}/resources/copy_images/images/icons/callouts/2.png
220+
WARNINGS
221+
convert input, attributes, eq(expected_warnings.strip)
222+
expect(copied).to eq([
223+
["images/icons/callouts/1.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.png"],
224+
["images/icons/callouts/2.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/2.png"],
225+
])
226+
end
227+
228+
it "copies images for callouts when requested (gif)" do
229+
copied = []
230+
attributes = copy_attributes copied
231+
attributes['copy-callout-images'] = 'gif'
232+
input = <<~ASCIIDOC
233+
== Example
234+
----
235+
foo <1>
236+
----
237+
<1> words
238+
ASCIIDOC
239+
expected_warnings = <<~WARNINGS
240+
INFO: <stdin>: line 5: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.gif
241+
WARNINGS
242+
convert input, attributes, eq(expected_warnings.strip)
243+
expect(copied).to eq([
244+
["images/icons/callouts/1.gif", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.gif"],
245+
])
246+
end
247+
248+
it "has a nice error message when a callout image is missing" do
249+
copied = []
250+
attributes = copy_attributes copied
251+
attributes['copy-callout-images'] = 'gif'
252+
input = <<~ASCIIDOC
253+
== Example
254+
----
255+
foo <1> <2>
256+
----
257+
<1> words
258+
<2> words
259+
ASCIIDOC
260+
convert input, attributes, match(/
261+
WARN:\ <stdin>:\ line\ 6:\ can't\ read\ image\ at\ any\ of\ \[
262+
"#{spec_dir}\/images\/icons\/callouts\/2.gif",\s
263+
"#{spec_dir}\/resources\/images\/icons\/callouts\/2.gif",\s
264+
.+
265+
"#{spec_dir}\/resources\/copy_images\/images\/icons\/callouts\/2.gif"
266+
.+
267+
\]/x).and(match(/INFO: <stdin>: line 5: copying #{spec_dir}\/resources\/copy_images\/images\/icons\/callouts\/1.gif/))
268+
expect(copied).to eq([
269+
["images/icons/callouts/1.gif", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.gif"],
270+
])
271+
end
272+
273+
it "only copies callout images one time" do
274+
copied = []
275+
attributes = copy_attributes copied
276+
attributes['copy-callout-images'] = 'png'
277+
input = <<~ASCIIDOC
278+
== Example
279+
----
280+
foo <1>
281+
----
282+
<1> words
283+
284+
----
285+
foo <1>
286+
----
287+
<1> words
288+
ASCIIDOC
289+
expected_warnings = <<~WARNINGS
290+
INFO: <stdin>: line 5: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.png
291+
WARNINGS
292+
convert input, attributes, eq(expected_warnings.strip)
293+
expect(copied).to eq([
294+
["images/icons/callouts/1.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.png"],
295+
])
296+
end
297+
298+
it "supports callout lists with multiple callouts per item" do
299+
# This is a *super* weird case but we have it in Elasticsearch.
300+
# The only way I can make callout lists be for two things is by making
301+
# blocks with callouts but only having a single callout list below both.
302+
copied = []
303+
attributes = copy_attributes copied
304+
attributes['copy-callout-images'] = 'png'
305+
input = <<~ASCIIDOC
306+
== Example
307+
----
308+
foo <1>
309+
----
310+
311+
----
312+
foo <1>
313+
----
314+
<1> words
315+
ASCIIDOC
316+
expected_warnings = <<~WARNINGS
317+
INFO: <stdin>: line 9: copying #{spec_dir}/resources/copy_images/images/icons/callouts/1.png
318+
INFO: <stdin>: line 9: copying #{spec_dir}/resources/copy_images/images/icons/callouts/2.png
319+
WARNINGS
320+
convert input, attributes, eq(expected_warnings.strip)
321+
expect(copied).to eq([
322+
["images/icons/callouts/1.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/1.png"],
323+
["images/icons/callouts/2.png", "#{spec_dir}/resources/copy_images/images/icons/callouts/2.png"],
324+
])
325+
end
326+
327+
it "doesn't copy callout images if the extension isn't set" do
328+
copied = []
329+
attributes = copy_attributes copied
330+
input = <<~ASCIIDOC
331+
== Example
332+
----
333+
foo <1>
334+
----
335+
<1> words
336+
337+
----
338+
foo <1>
339+
----
340+
<1> words
341+
ASCIIDOC
342+
convert input, attributes
343+
expect(copied).to eq([])
344+
end
204345
end
43 Bytes
Loading
70 Bytes
Loading
70 Bytes
Loading
70 Bytes
Loading

0 commit comments

Comments
 (0)