Skip to content

Commit

Permalink
ColumnWidths: add support for fractions
Browse files Browse the repository at this point in the history
Also: add 'HALVES' (/2), similar to 'QUARTERS' (/4)
  • Loading branch information
brechtm committed Feb 19, 2021
1 parent c959cc2 commit 0693f86
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Fixed:
* When a caption occurs in an unnumbered chapter, an exception aborts rendering
(even when ``number_separator`` style attribute is set to ``None``)
* Handling of base template specified as string in a template configuration
* Table column widths entries now also accept fractions


Release 0.5.0 (2021-02-03)
Expand All @@ -33,7 +34,7 @@ New Features:
style property.
* Table: in addition to fixed and relative-width columns, you can indicate
columns to be automatically sized by specifying a value of 'auto' in the
'column_widths' style parameter in yout style sheet.
'column_widths' style parameter in your style sheet.
* docutils frontend: support the ``:align:`` option to table directives, which
will override the alignment set for the table in the style sheet.
* The starting number of enumerated lists in reStructuredText is respected.
Expand Down
1 change: 1 addition & 0 deletions src/rinoh/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,5 @@ def __rmul__(self, nominator):


PERCENT = FractionUnit(100, '%') #: fraction of 100
HALVES = FractionUnit(2, '/2') #: fraction of 2
QUARTERS = FractionUnit(4, '/4') #: fraction of 4
2 changes: 1 addition & 1 deletion src/rinoh/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ColumnWidths(AcceptNoneAttributeType):
def check_type(cls, value):
return (super().check_type(value)
or (isinstance(value, list)
and all(item is None or isinstance(item, (Dimension, int))
and all(item is None or isinstance(item, (DimBase, int))
for item in value)))

@classmethod
Expand Down
8 changes: 7 additions & 1 deletion tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rinoh.attribute import OptionSet, Bool, Integer, ParseError
from rinoh.color import Color, HexColor
from rinoh.dimension import (Dimension, PT, PICA, INCH, MM, CM,
PERCENT, QUARTERS)
PERCENT, HALVES, QUARTERS)
from rinoh.draw import Stroke
from rinoh.flowable import FlowableWidth, HorizontalAlignment, Break
from rinoh.font.style import ClassSet, FontWeight, FontSlant, FontWidth
Expand Down Expand Up @@ -261,6 +261,8 @@ def test_dimension_from_string():
assert Dimension.from_string('-2.1 cm') == -2.1*CM
assert Dimension.from_string('21%') == 21*PERCENT
assert Dimension.from_string('-16.12%') == -16.12*PERCENT
assert Dimension.from_string('1/2') == 1*HALVES
assert Dimension.from_string('3/2') == 3*HALVES
assert Dimension.from_string('3/4') == 3*QUARTERS
with pytest.raises(ParseError):
assert Dimension.from_string('20inch')
Expand Down Expand Up @@ -456,6 +458,10 @@ def test_column_widths_from_string():
assert ColumnWidths.from_string('2 4 6') == [2, 4, 6]
assert ColumnWidths.from_string('6 5 4 ') == [6, 5, 4]
assert ColumnWidths.from_string('1pt 2cm 3in') == [1*PT, 2*CM, 3*INCH]
assert ColumnWidths.from_string('20% 30%') == [20*PERCENT, 30*PERCENT]
assert ColumnWidths.from_string('1/2 3/4') == [1*HALVES, 3*QUARTERS]
assert ColumnWidths.from_string('1/2 auto 20%') == [1*HALVES, None,
20*PERCENT]
assert ColumnWidths.from_string('4 pt 5 cm 6 in') == [4*PT, 5*CM, 6*INCH]
assert ColumnWidths.from_string('7pt 8 cm 9in') == [7*PT, 8*CM, 9*INCH]
assert ColumnWidths.from_string('10 20 1cm 30') == [10, 20, 1*CM, 30]
Expand Down

0 comments on commit 0693f86

Please sign in to comment.