Skip to content

Commit

Permalink
resolves #4552 add support for scaledwidth and scale attributes on in…
Browse files Browse the repository at this point in the history
…line image macro in DocBook output (PR #4554)
  • Loading branch information
mojavelinux committed Feb 20, 2024
1 parent 31af659 commit 5897ec0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Compliance::
* Turn off system-dependent newline conversion when writing files; don't convert line feeds to system-dependent newline (#4550)
* Support logger in Ruby 3.3 by instantiating super class (#4493) (*@mtasaka*)
* Don't promote level-0 special section at start of document to document title (#4151)
* Add support for `scaledwidth` and `scale` attributes on inline image macro in DocBook output (#4552)
* Disallow the use of dot (`.`) in the name of a named element attribute (#4147)
* Disallow the use of the left square bracket in an attribute list on formatted text (#4306)
* Update stylesheet to use break-after in place of page-break-before/after/inside (#3466)
Expand Down
61 changes: 26 additions & 35 deletions lib/asciidoctor/converter/docbook5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,11 @@ def convert_floating_title node
end

def convert_image node
# NOTE according to the DocBook spec, content area, scaling, and scaling to fit are mutually exclusive
# See http://tdg.docbook.org/tdg/4.5/imagedata-x.html#d0e79635
if node.attr? 'scaledwidth'
width_attribute = %( width="#{node.attr 'scaledwidth'}")
depth_attribute = ''
scale_attribute = ''
elsif node.attr? 'scale'
# QUESTION should we set the viewport using width and depth? (the scaled image would be contained within this box)
#width_attribute = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : ''
#depth_attribute = (node.attr? 'height') ? %( depth="#{node.attr 'height'}") : ''
scale_attribute = %( scale="#{node.attr 'scale'}")
else
width_attribute = (node.attr? 'width') ? %( contentwidth="#{node.attr 'width'}") : ''
depth_attribute = (node.attr? 'height') ? %( contentdepth="#{node.attr 'height'}") : ''
scale_attribute = ''
end
align_attribute = (node.attr? 'align') ? %( align="#{node.attr 'align'}") : ''

mediaobject = %(<mediaobject>
<imageobject>
<imagedata fileref="#{node.image_uri node.attr 'target'}"#{width_attribute}#{depth_attribute}#{scale_attribute}#{align_attribute}/>
<imagedata fileref="#{node.image_uri node.attr 'target'}"#{image_size_attributes node.attributes}#{align_attribute}/>
</imageobject>
<textobject><phrase>#{node.alt}</phrase></textobject>
</mediaobject>)
Expand Down Expand Up @@ -553,11 +537,9 @@ def convert_inline_footnote node
end

def convert_inline_image node
width_attribute = (node.attr? 'width') ? %( contentwidth="#{node.attr 'width'}") : ''
depth_attribute = (node.attr? 'height') ? %( contentdepth="#{node.attr 'height'}") : ''
%(<inlinemediaobject#{common_attributes nil, node.role}>
<imageobject>
<imagedata fileref="#{node.type == 'icon' ? (node.icon_uri node.target) : (node.image_uri node.target)}"#{width_attribute}#{depth_attribute}/>
<imagedata fileref="#{node.type == 'icon' ? (node.icon_uri node.target) : (node.image_uri node.target)}"#{image_size_attributes node.attributes}/>
</imageobject>
<textobject><phrase>#{node.alt}</phrase></textobject>
</inlinemediaobject>)
Expand Down Expand Up @@ -665,6 +647,23 @@ def common_attributes id, role = nil, reftext = nil
end
end

def image_size_attributes attributes
# NOTE according to the DocBook spec, content area, scaling, and scaling to fit are mutually exclusive
# See http://tdg.docbook.org/tdg/4.5/imagedata-x.html#d0e79635
if attributes.key? 'scaledwidth'
%( width="#{attributes['scaledwidth']}")
elsif attributes.key? 'scale'
# QUESTION should we set the viewport using width and depth? (the scaled image would be contained within this box)
#width_attribute = (attributes.key? 'width') ? %( width="#{attributes['width']}") : ''
#depth_attribute = (attributes.key? 'height') ? %( depth="#{attributes['height']}") : ''
%( scale="#{attributes['scale']}")
else
width_attribute = (attributes.key? 'width') ? %( contentwidth="#{attributes['width']}") : ''
depth_attribute = (attributes.key? 'height') ? %( contentdepth="#{attributes['height']}") : ''
%(#{width_attribute}#{depth_attribute})
end
end

def author_tag doc, author
result = []
result << '<author>'
Expand Down Expand Up @@ -785,26 +784,18 @@ def title_tag node, optional = true

def cover_tag doc, face, use_placeholder = false
if (cover_image = doc.attr %(#{face}-cover-image))
width_attr = ''
depth_attr = ''
if (cover_image.include? ':') && ImageMacroRx =~ cover_image
attrlist = $2
cover_image = doc.image_uri $1
if attrlist
attrs = (AttributeList.new attrlist).parse %w(alt width height)
if attrs.key? 'scaledwidth'
# NOTE scalefit="1" is the default in this case
width_attr = %( width="#{attrs['scaledwidth']}")
else
width_attr = %( contentwidth="#{attrs['width']}") if attrs.key? 'width'
depth_attr = %( contentdepth="#{attrs['height']}") if attrs.key? 'height'
end
end
target, attrlist = $1, $2
cover_image = doc.image_uri target
# NOTE scalefit="1" is the default for a cover image
size_attrs = image_size_attributes (AttributeList.new attrlist).parse %w(alt width height) if attrlist
else
size_attrs = ''
end
%(<cover role="#{face}">
<mediaobject>
<imageobject>
<imagedata fileref="#{cover_image}"#{width_attr}#{depth_attr}/>
<imagedata fileref="#{cover_image}"#{size_attrs}/>
</imageobject>
</mediaobject>
</cover>)
Expand Down
12 changes: 12 additions & 0 deletions test/substitutions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,18 @@
para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'a single-line image macro with scaledwidth attribute should be supported in docbook' do
para = block_from_string 'image:tiger.png[Tiger,scaledwidth=25%]', backend: 'docbook'
assert_equal '<inlinemediaobject><imageobject><imagedata fileref="tiger.png" width="25%"/></imageobject><textobject><phrase>Tiger</phrase></textobject></inlinemediaobject>',
para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'a single-line image macro with scaled attribute should be supported in docbook' do
para = block_from_string 'image:tiger.png[Tiger,scale=200]', backend: 'docbook'
assert_equal '<inlinemediaobject><imageobject><imagedata fileref="tiger.png" scale="200"/></imageobject><textobject><phrase>Tiger</phrase></textobject></inlinemediaobject>',
para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'should pass through role on image macro to DocBook output' do
para = block_from_string 'image:tiger.png[Tiger,200,role=animal]', backend: 'docbook'
result = para.sub_macros para.source
Expand Down

0 comments on commit 5897ec0

Please sign in to comment.