Skip to content

Commit 44cff5f

Browse files
authored
Asciidoctor: Copy admonition images (#604)
Copies the images used by admonitions like `WARNING` and `NOTE` and the change admonitions like `coming`.
1 parent 7785582 commit 44cff5f

File tree

13 files changed

+101
-7
lines changed

13 files changed

+101
-7
lines changed

lib/ES/Util.pm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ sub build_chunked {
9797
'-a' => 'asciidoc-dir=' . $asciidoc_dir,
9898
'-a' => 'resources=' . join(',', @$resources),
9999
'-a' => 'copy-callout-images=png',
100+
'-a' => 'copy-admonition-images=png',
100101
'--destination-dir=' . $dest,
101102
docinfo($index),
102103
$index
@@ -217,6 +218,8 @@ sub build_single {
217218
$private ? () : ( '-a' => "edit_url=$edit_url" ),
218219
'-a' => 'asciidoc-dir=' . $asciidoc_dir,
219220
'-a' => 'resources=' . join(',', @$resources),
221+
'-a' => 'copy-callout-images=png',
222+
'-a' => 'copy-admonition-images=png',
220223
# Disable warning on missing attributes because we have
221224
# missing attributes!
222225
# '-a' => 'attribute-missing=warn',

resources/asciidoctor/lib/change_admonishment/extension.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def process parent, target, attrs
4343
# go with this funny compound pass thing.
4444
note = Block.new(parent, :pass, :content_model => :compound)
4545
note << Block.new(note, :pass,
46-
:source => "<note revisionflag=\"#{@revisionflag}\" revision=\"#{version}\">")
46+
:source => "<note revisionflag=\"#{@revisionflag}\" revision=\"#{version}\">",
47+
:attributes => {'revisionflag' => @revisionflag})
4748
note << Block.new(note, :paragraph,
4849
:source => attrs[:passtext],
4950
:subs => Substitutors::NORMAL_SUBS)

resources/asciidoctor/lib/copy_images/extension.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,32 @@ def process_block block
2727
uri = block.image_uri(block.attr 'target')
2828
return if Helpers.uriish? uri # Skip external images
2929
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-
}
30+
return
31+
end
32+
callout_extension = block.document.attr 'copy-callout-images'
33+
if callout_extension
34+
if block.parent && block.parent.context == :colist
35+
block.attr('coids').scan(/CO(?:\d+)-(\d+)/) {
36+
copy_image block, "images/icons/callouts/#{$1}.#{callout_extension}"
37+
}
38+
return
39+
end
40+
end
41+
admonition_extension = block.document.attr 'copy-admonition-images'
42+
if admonition_extension
43+
if block.context == :admonition
44+
# The image for a standard admonition comes from the style
45+
style = block.attr 'style'
46+
return unless style
47+
copy_image block, "images/icons/#{style.downcase(:ascii)}.#{admonition_extension}"
48+
return
49+
end
50+
# The image for a change admonition comes from the revisionflag
51+
revisionflag = block.attr 'revisionflag'
52+
if revisionflag
53+
copy_image block, "images/icons/#{revisionflag}.#{admonition_extension}"
54+
return
55+
end
3656
end
3757
end
3858

resources/asciidoctor/spec/copy_images_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'change_admonishment/extension'
12
require 'copy_images/extension'
23
require 'fileutils'
34
require 'tmpdir'
@@ -6,6 +7,7 @@
67
RSpec::Matchers.define_negated_matcher :not_match, :match
78

89
before(:each) do
10+
Extensions.register ChangeAdmonishment
911
Extensions.register do
1012
tree_processor CopyImages
1113
end
@@ -375,4 +377,72 @@ def copy_attributes copied
375377
convert input, attributes
376378
expect(copied).to eq([])
377379
end
380+
381+
['note', 'tip', 'important', 'caution', 'warning'].each { |(name)|
382+
it "copies images for the #{name} admonition when requested" do
383+
copied = []
384+
attributes = copy_attributes copied
385+
attributes['copy-admonition-images'] = 'png'
386+
input = <<~ASCIIDOC
387+
#{name.upcase}: Words words words.
388+
ASCIIDOC
389+
expected_warnings = <<~WARNINGS
390+
INFO: <stdin>: line 1: copying #{spec_dir}/resources/copy_images/images/icons/#{name}.png
391+
WARNINGS
392+
convert input, attributes, eq(expected_warnings.strip)
393+
expect(copied).to eq([
394+
["images/icons/#{name}.png", "#{spec_dir}/resources/copy_images/images/icons/#{name}.png"],
395+
])
396+
end
397+
}
398+
399+
[
400+
['added', 'added'],
401+
['coming', 'changed'],
402+
['deprecated', 'deleted']
403+
].each { |(name, revisionflag)|
404+
it "copies images for the block formatted #{name} change admonition when requested" do
405+
copied = []
406+
attributes = copy_attributes copied
407+
attributes['copy-admonition-images'] = 'png'
408+
input = <<~ASCIIDOC
409+
#{name}::[some_version]
410+
ASCIIDOC
411+
# We can't get the location of the blocks because asciidoctor doesn't
412+
# make it available to us here!
413+
expected_warnings = <<~WARNINGS
414+
INFO: copying #{spec_dir}/resources/copy_images/images/icons/#{revisionflag}.png
415+
WARNINGS
416+
convert input, attributes, eq(expected_warnings.strip)
417+
expect(copied).to eq([
418+
["images/icons/#{revisionflag}.png", "#{spec_dir}/resources/copy_images/images/icons/#{revisionflag}.png"],
419+
])
420+
end
421+
}
422+
423+
it "copies images for admonitions when requested with a different file extension" do
424+
copied = []
425+
attributes = copy_attributes copied
426+
attributes['copy-admonition-images'] = 'gif'
427+
input = <<~ASCIIDOC
428+
NOTE: Words words words.
429+
ASCIIDOC
430+
expected_warnings = <<~WARNINGS
431+
INFO: <stdin>: line 1: copying #{spec_dir}/resources/copy_images/images/icons/note.gif
432+
WARNINGS
433+
convert input, attributes, eq(expected_warnings.strip)
434+
expect(copied).to eq([
435+
["images/icons/note.gif", "#{spec_dir}/resources/copy_images/images/icons/note.gif"],
436+
])
437+
end
438+
439+
it "doesn't copy images for admonitions if not requested" do
440+
copied = []
441+
attributes = copy_attributes copied
442+
input = <<~ASCIIDOC
443+
NOTE: Words words words.
444+
ASCIIDOC
445+
convert input, attributes
446+
expect(copied).to eq([])
447+
end
378448
end
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)