From 6b97ebfb6a37b341ac14dde7744305ea0d6307ad Mon Sep 17 00:00:00 2001 From: Brecht Machiels Date: Sat, 24 Oct 2020 13:41:59 +0200 Subject: [PATCH] List: inherit from GroupedLabeledFlowables - adjust the docutils frontend to this change - determine list item indices dynamically --- src/rinoh/frontend/rst/nodes.py | 18 +++++++++++------- src/rinoh/structure.py | 23 ++++++++++------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/rinoh/frontend/rst/nodes.py b/src/rinoh/frontend/rst/nodes.py index df0ce9dc2..8236d5c4b 100644 --- a/src/rinoh/frontend/rst/nodes.py +++ b/src/rinoh/frontend/rst/nodes.py @@ -471,25 +471,29 @@ def build_flowable(self): return rt.AnchorFlowable() -class Enumerated_List(DocutilsBodyNode): +class Enumerated_List(DocutilsGroupingNode): + style = 'enumerated' + grouped_flowables_class = rt.List + def build_flowable(self): # TODO: handle different numbering styles start = self.attributes.get('start', 1) - return rt.List([item.flowable() for item in self.list_item], - start_index=start, style='enumerated') + return super().build_flowable(start_index=start) +class Bullet_List(DocutilsGroupingNode): + style = 'bulleted' + grouped_flowables_class = rt.List -class Bullet_List(DocutilsBodyNode): def build_flowable(self): try: - return rt.List([item.flowable() for item in self.list_item], - style='bulleted') + return super().build_flowable() except AttributeError: # empty list return rt.DummyFlowable() class List_Item(DocutilsGroupingNode): - pass + def build_flowable(self): + return rt.ListItem(super().build_flowable()) class Definition_List(DocutilsGroupingNode): diff --git a/src/rinoh/structure.py b/src/rinoh/structure.py index eb431f198..53856b709 100644 --- a/src/rinoh/structure.py +++ b/src/rinoh/structure.py @@ -185,19 +185,18 @@ class ListStyle(GroupedFlowablesStyle, NumberStyle): 'Bullet to use in unordered lists') -class List(StaticGroupedFlowables, Label): +class List(GroupedLabeledFlowables, StaticGroupedFlowables): style_class = ListStyle - def __init__(self, flowables, start_index=1, + def __init__(self, list_items, start_index=1, id=None, style=None, parent=None): - items = [ListItem(i, flowable, parent=self) - for i, flowable in enumerate(flowables, start_index)] - super().__init__(items, id=id, style=style, parent=parent) + super().__init__(list_items, id=id, style=style, parent=parent) + self.start_index = start_index class ListItem(LabeledFlowable): - def __init__(self, index, flowable, id=None, style=None, parent=None): - label = ListItemLabel(index) + def __init__(self, flowable, id=None, style=None, parent=None): + label = ListItemLabel() super().__init__(label, flowable, id=id, style=style, parent=parent) @@ -208,15 +207,13 @@ class ListItemLabelStyle(ParagraphStyle, LabelStyle): class ListItemLabel(ParagraphBase, Label): style_class = ListItemLabelStyle - def __init__(self, index, style=None, parent=None): - super().__init__(style=style, parent=parent) - self.index = index - def text(self, container): - list = self.parent.parent + list_item = self.parent + list = list_item.parent if list.get_style('ordered', container): + index = list.start_index + list.children.index(list_item) number_format = list.get_style('number_format', container) - label = format_number(self.index, number_format) + label = format_number(index, number_format) else: label = list.get_style('bullet', container) return MixedStyledText(self.format_label(label, container), parent=self)