diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2024-02-20 04:06:40 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-20 04:06:40 -0700 |
| commit | 5897ec073a37b1aba9f8240c7718ac970c4f47e5 (patch) | |
| tree | 2d5f6294feab0bc2ecc41214ebed5434851d62f4 | |
| parent | 31af659a5608c231cf1557b5f6e9e14b79493693 (diff) | |
resolves #4552 add support for scaledwidth and scale attributes on inline image macro in DocBook output (PR #4554)
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | lib/asciidoctor/converter/docbook5.rb | 61 | ||||
| -rw-r--r-- | test/substitutions_test.rb | 12 |
3 files changed, 39 insertions, 35 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 25ee1621..747371f0 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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) diff --git a/lib/asciidoctor/converter/docbook5.rb b/lib/asciidoctor/converter/docbook5.rb index 4bfda1db..da3a0bd4 100644 --- a/lib/asciidoctor/converter/docbook5.rb +++ b/lib/asciidoctor/converter/docbook5.rb @@ -195,27 +195,11 @@ class Converter::DocBook5Converter < Converter::Base 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>) @@ -553,11 +537,9 @@ class Converter::DocBook5Converter < Converter::Base 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>) @@ -665,6 +647,23 @@ class Converter::DocBook5Converter < Converter::Base 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>' @@ -785,26 +784,18 @@ class Converter::DocBook5Converter < Converter::Base 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>) diff --git a/test/substitutions_test.rb b/test/substitutions_test.rb index 82c67a4b..a4c6c37d 100644 --- a/test/substitutions_test.rb +++ b/test/substitutions_test.rb @@ -849,6 +849,18 @@ context 'Substitutions' do 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 |
