Skip to content

Commit

Permalink
List: inherit from GroupedLabeledFlowables
Browse files Browse the repository at this point in the history
- adjust the docutils frontend to this change
- determine list item indices dynamically
  • Loading branch information
brechtm committed Oct 25, 2020
1 parent a3aa0e3 commit 6b97ebf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
18 changes: 11 additions & 7 deletions src/rinoh/frontend/rst/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 10 additions & 13 deletions src/rinoh/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)
Expand Down

0 comments on commit 6b97ebf

Please sign in to comment.