Skip to content

Commit

Permalink
docutils inline images: support options (fixes #254)
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtm committed May 19, 2021
1 parent 13294fc commit 9111deb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ New Features:

* Document part templates now accept a *page_number_prefix* (StyledText). For
example, set ``page_number_prefix = '{SECTION_NUMBER(1)}-'`` to prefix the
page number with the chapter number. You'll want to use this to with the new
page number with the chapter number. You'll want to use this with the new
page break options (see next item).
* The ``page_break`` style attribute now also accepts *left restart*, *right
restart* and *any restart* values to restart page numbering
Expand All @@ -29,6 +29,7 @@ Fixed:
* Setting the *base* for a style to ``PARENT_STYLE`` results in a crash.
* docutils image directive: crash when encountering a width/height containing a
decimal point (#251 by Karel Frajtak)
* docutils inline images don't support width, height and scale options


Release 0.5.2 (2021-02-24)
Expand Down
14 changes: 9 additions & 5 deletions src/rinoh/frontend/rst/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ class Image(DocutilsBodyNode, DocutilsInlineNode):
def image_path(self):
return self.get('uri')

def build_flowable(self):
@property
def options(self):
width = convert_quantity(self.get('width'))
height = convert_quantity(self.get('height'))
align = self.get('align')
Expand All @@ -613,16 +614,19 @@ def build_flowable(self):
width = width * scale if width else None
height = height * scale if height else None
scale = 1
return rt.Image(self.image_path, width=width, height=height,
scale=scale, align=align)
return dict(align=align, width=width, height=height, scale=scale)

def build_flowable(self):
return rt.Image(self.image_path, **self.options)

ALIGN_TO_BASELINE = {'bottom': 0,
'middle': 50*PERCENT,
'top': 100*PERCENT}

def build_styled_text(self):
baseline = self.ALIGN_TO_BASELINE.get(self.get('align'))
return rt.InlineImage(self.image_path, baseline=baseline)
options = self.options
baseline = self.ALIGN_TO_BASELINE.get(options.pop('align'))
return rt.InlineImage(self.image_path, baseline=baseline, **options)


class Figure(DocutilsGroupingNode):
Expand Down
Binary file added tests_regression/rst/inline_image.pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions tests_regression/rst/inline_image.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Default: |biohazard|

Width, top: |biowidth|

Height, bottom: |bioheight|

Scale, middle: |bioscaled|


.. |biohazard| image:: ../images/biohazard.png

.. |biowidth| image:: ../images/biohazard.png
:width: 40px
:align: top

.. |bioheight| image:: ../images/biohazard.png
:height: 30px
:align: bottom

.. |bioscaled| image:: ../images/biohazard.png
:scale: 70
:align: middle
44 changes: 44 additions & 0 deletions tests_regression/rst/inline_image.stylelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---------------------------------- page 1 ----------------------------------
#### FootnoteContainer('footnotes')
StaticGroupedFlowables()
#### DownExpandingContainer('floats')
StaticGroupedFlowables()
#### ChainedContainer('column1')
StaticGroupedFlowables()
DocumentTree() None:None <document>
Paragraph('Default: InlineImage') inline_image.rst:2 <paragraph>
> (0,0,0,0,2) body [Sphinx] > default
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image
MixedStyledText('Default: InlineImage')
MixedStyledText('Default: InlineImage')
SingleStyledText('Default: ')
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image
Paragraph('Width, top: InlineImage') inline_image.rst:4 <paragraph>
> (0,0,0,0,2) body [Sphinx] > default
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image
MixedStyledText('Width, top: InlineImage')
MixedStyledText('Width, top: InlineImage')
SingleStyledText('Width, top: ')
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image
Paragraph('Height, bottom: InlineImage') inline_image.rst:6 <paragraph>
> (0,0,0,0,2) body [Sphinx] > default
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image
MixedStyledText('Height, bottom: InlineImage')
MixedStyledText('Height, bottom: InlineImage')
SingleStyledText('Height, bottom: ')
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image
Paragraph('Scale, middle: InlineImage') inline_image.rst:8 <paragraph>
> (0,0,0,0,2) body [Sphinx] > default
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image
MixedStyledText('Scale, middle: InlineImage')
MixedStyledText('Scale, middle: InlineImage')
SingleStyledText('Scale, middle: ')
InlineImage('../images/biohazard.png') None:None <image>
x (0,0,0,0,2) inline image

0 comments on commit 9111deb

Please sign in to comment.