Skip to content

Commit

Permalink
resolves #2349 check mandatory attributes when creating an image bloc…
Browse files Browse the repository at this point in the history
…k (PR #2355)

* check mandatory attributes (target) when creating an image block
* auto generate alt text in create_image_block
  • Loading branch information
ggrossetie authored and mojavelinux committed Mar 20, 2018
1 parent e404899 commit fe14766
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/asciidoctor/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,20 @@ def create_block parent, context, source, attrs, opts = {}
Block.new parent, context, { :source => source, :attributes => attrs }.merge(opts)
end

# Public: Creates an image block node and links it to the specified parent.
#
# parent - The parent Block (Block, Section, or Document) of this new image block.
# attrs - A Hash of attributes to control how the image block is built.
# Use the target attribute to set the source of the image.
# Use the alt attribute to specify an alternate text for the image.
# opts - An optional Hash of options (default: {})
#
# Returns a [Block] node with all properties properly initialized.
def create_image_block parent, attrs, opts = {}
unless (target = attrs['target'])
raise ::ArgumentError, 'Unable to create an image block, target attribute is required'
end
attrs['alt'] ||= (attrs['default-alt'] = Helpers.basename(target, true).tr('_-', ' '))
create_block parent, :image, nil, attrs, opts
end

Expand Down
56 changes: 55 additions & 1 deletion test/extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ def activate registry
end
end

def create_cat_in_sink_block_macro
Asciidoctor::Extensions.create do
block_macro do
named :cat_in_sink
process do |parent, target, attrs|
image_attrs = {}
unless target.nil_or_empty?
image_attrs['target'] = %(cat-in-sink-day-#{target}.png)
end
if (alt = attrs.delete 1)
image_attrs['alt'] = alt
end
create_image_block parent, image_attrs
end
end
end
end

context 'Extensions' do
context 'Register' do
test 'should register extension group class' do
Expand Down Expand Up @@ -1161,7 +1179,6 @@ def process parent, reader, attributes
end
end


test 'should append docinfo to document' do
begin
Asciidoctor::Extensions.register do
Expand All @@ -1181,5 +1198,42 @@ def process parent, reader, attributes
Asciidoctor::Extensions.unregister_all
end
end

test 'should raise an exception if mandatory target attribute is not provided for image block' do
input = <<-EOS
.Cat in Sink?
cat_in_sink::[]
EOS
exception = assert_raises ArgumentError do
render_embedded_string input, :extension_registry => create_cat_in_sink_block_macro
end
assert_match(/target attribute is required/, exception.message)
end

test 'should assign alt attribute to image block if alt is not provided' do
input = <<-EOS
.Cat in Sink?
cat_in_sink::25[]
EOS
doc = document_from_string input, :header_footer => false, :extension_registry => create_cat_in_sink_block_macro
image = doc.blocks[0]
assert_equal 'cat in sink day 25', (image.attr 'alt')
assert_equal 'cat in sink day 25', (image.attr 'default-alt')
output = doc.convert
assert_includes output, '<img src="cat-in-sink-day-25.png" alt="cat in sink day 25">'
end

test 'should create an image block if mandatory attributes are provided' do
input = <<-EOS
.Cat in Sink?
cat_in_sink::30[cat in sink (yes)]
EOS
doc = document_from_string input, :header_footer => false, :extension_registry => create_cat_in_sink_block_macro
image = doc.blocks[0]
assert_equal 'cat in sink (yes)', (image.attr 'alt')
refute(image.attr? 'default-alt')
output = doc.convert
assert_includes output, '<img src="cat-in-sink-day-30.png" alt="cat in sink (yes)">'
end
end
end

0 comments on commit fe14766

Please sign in to comment.