Skip to content

Commit

Permalink
Asciidoctor: Copy admonition images (#604)
Browse files Browse the repository at this point in the history
Copies the images used by admonitions like `WARNING` and `NOTE` and the
change admonitions like `coming`.
  • Loading branch information
nik9000 authored Feb 13, 2019
1 parent 7785582 commit 44cff5f
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/ES/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ sub build_chunked {
'-a' => 'asciidoc-dir=' . $asciidoc_dir,
'-a' => 'resources=' . join(',', @$resources),
'-a' => 'copy-callout-images=png',
'-a' => 'copy-admonition-images=png',
'--destination-dir=' . $dest,
docinfo($index),
$index
Expand Down Expand Up @@ -217,6 +218,8 @@ sub build_single {
$private ? () : ( '-a' => "edit_url=$edit_url" ),
'-a' => 'asciidoc-dir=' . $asciidoc_dir,
'-a' => 'resources=' . join(',', @$resources),
'-a' => 'copy-callout-images=png',
'-a' => 'copy-admonition-images=png',
# Disable warning on missing attributes because we have
# missing attributes!
# '-a' => 'attribute-missing=warn',
Expand Down
3 changes: 2 additions & 1 deletion resources/asciidoctor/lib/change_admonishment/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def process parent, target, attrs
# go with this funny compound pass thing.
note = Block.new(parent, :pass, :content_model => :compound)
note << Block.new(note, :pass,
:source => "<note revisionflag=\"#{@revisionflag}\" revision=\"#{version}\">")
:source => "<note revisionflag=\"#{@revisionflag}\" revision=\"#{version}\">",
:attributes => {'revisionflag' => @revisionflag})
note << Block.new(note, :paragraph,
:source => attrs[:passtext],
:subs => Substitutors::NORMAL_SUBS)
Expand Down
32 changes: 26 additions & 6 deletions resources/asciidoctor/lib/copy_images/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,32 @@ def process_block block
uri = block.image_uri(block.attr 'target')
return if Helpers.uriish? uri # Skip external images
copy_image block, uri
elsif (extension = block.document.attr 'copy-callout-images') &&
block.parent &&
block.parent.context == :colist
id = block.attr('coids').scan(/CO(?:\d+)-(\d+)/) {
copy_image block, "images/icons/callouts/#{$1}.#{extension}"
}
return
end
callout_extension = block.document.attr 'copy-callout-images'
if callout_extension
if block.parent && block.parent.context == :colist
block.attr('coids').scan(/CO(?:\d+)-(\d+)/) {
copy_image block, "images/icons/callouts/#{$1}.#{callout_extension}"
}
return
end
end
admonition_extension = block.document.attr 'copy-admonition-images'
if admonition_extension
if block.context == :admonition
# The image for a standard admonition comes from the style
style = block.attr 'style'
return unless style
copy_image block, "images/icons/#{style.downcase(:ascii)}.#{admonition_extension}"
return
end
# The image for a change admonition comes from the revisionflag
revisionflag = block.attr 'revisionflag'
if revisionflag
copy_image block, "images/icons/#{revisionflag}.#{admonition_extension}"
return
end
end
end

Expand Down
70 changes: 70 additions & 0 deletions resources/asciidoctor/spec/copy_images_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'change_admonishment/extension'
require 'copy_images/extension'
require 'fileutils'
require 'tmpdir'
Expand All @@ -6,6 +7,7 @@
RSpec::Matchers.define_negated_matcher :not_match, :match

before(:each) do
Extensions.register ChangeAdmonishment
Extensions.register do
tree_processor CopyImages
end
Expand Down Expand Up @@ -375,4 +377,72 @@ def copy_attributes copied
convert input, attributes
expect(copied).to eq([])
end

['note', 'tip', 'important', 'caution', 'warning'].each { |(name)|
it "copies images for the #{name} admonition when requested" do
copied = []
attributes = copy_attributes copied
attributes['copy-admonition-images'] = 'png'
input = <<~ASCIIDOC
#{name.upcase}: Words words words.
ASCIIDOC
expected_warnings = <<~WARNINGS
INFO: <stdin>: line 1: copying #{spec_dir}/resources/copy_images/images/icons/#{name}.png
WARNINGS
convert input, attributes, eq(expected_warnings.strip)
expect(copied).to eq([
["images/icons/#{name}.png", "#{spec_dir}/resources/copy_images/images/icons/#{name}.png"],
])
end
}

[
['added', 'added'],
['coming', 'changed'],
['deprecated', 'deleted']
].each { |(name, revisionflag)|
it "copies images for the block formatted #{name} change admonition when requested" do
copied = []
attributes = copy_attributes copied
attributes['copy-admonition-images'] = 'png'
input = <<~ASCIIDOC
#{name}::[some_version]
ASCIIDOC
# We can't get the location of the blocks because asciidoctor doesn't
# make it available to us here!
expected_warnings = <<~WARNINGS
INFO: copying #{spec_dir}/resources/copy_images/images/icons/#{revisionflag}.png
WARNINGS
convert input, attributes, eq(expected_warnings.strip)
expect(copied).to eq([
["images/icons/#{revisionflag}.png", "#{spec_dir}/resources/copy_images/images/icons/#{revisionflag}.png"],
])
end
}

it "copies images for admonitions when requested with a different file extension" do
copied = []
attributes = copy_attributes copied
attributes['copy-admonition-images'] = 'gif'
input = <<~ASCIIDOC
NOTE: Words words words.
ASCIIDOC
expected_warnings = <<~WARNINGS
INFO: <stdin>: line 1: copying #{spec_dir}/resources/copy_images/images/icons/note.gif
WARNINGS
convert input, attributes, eq(expected_warnings.strip)
expect(copied).to eq([
["images/icons/note.gif", "#{spec_dir}/resources/copy_images/images/icons/note.gif"],
])
end

it "doesn't copy images for admonitions if not requested" do
copied = []
attributes = copy_attributes copied
input = <<~ASCIIDOC
NOTE: Words words words.
ASCIIDOC
convert input, attributes
expect(copied).to eq([])
end
end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 44cff5f

Please sign in to comment.