1.1 --- a/moinformat/macros/toc.py Wed Aug 01 15:07:14 2018 +0200
1.2 +++ b/moinformat/macros/toc.py Wed Aug 01 15:35:40 2018 +0200
1.3 @@ -80,30 +80,46 @@
1.4 while level < new_level:
1.5 level += 1
1.6
1.7 + # Ignore levels outside the range of interest.
1.8 +
1.9 if not (min_level <= level <= max_level):
1.10 continue
1.11
1.12 # Determine whether the heading should be generated at this
1.13 - # level.
1.14 + # level or whether there are intermediate levels being
1.15 + # produced.
1.16
1.17 nodes = level == new_level and heading.nodes[:] + nl or []
1.18 indent = level - 1
1.19
1.20 - # Make a list and add an item to it.
1.21 + # Create a new item for the heading or sublists.
1.22 +
1.23 + new_item = ListItem(nodes, indent, marker, space, None)
1.24 +
1.25 + # Either revive an existing list.
1.26
1.27 - new_items = []
1.28 - new_list = List(new_items, indent, marker, num)
1.29 - new_item = ListItem(nodes, indent, marker, space, None)
1.30 - new_items.append(new_item)
1.31 + if level == min_level and lists:
1.32 + new_list = lists[-1]
1.33 + new_items = new_list.nodes
1.34 +
1.35 + # Or make a list and add an item to it.
1.36
1.37 - # Add the list to the current item, if any.
1.38 + else:
1.39 + new_items = []
1.40 + new_list = List(new_items, indent, marker, num)
1.41 +
1.42 + # Add the list to the current item, if any.
1.43
1.44 - if item:
1.45 - item.nodes.append(new_list)
1.46 + if item:
1.47 + item.nodes.append(new_list)
1.48 +
1.49 + # Record the new list.
1.50
1.51 - # Record the new list.
1.52 + lists.append(new_list)
1.53
1.54 - lists.append(new_list)
1.55 + # Add the item to the new or revived list.
1.56 +
1.57 + new_items.append(new_item)
1.58
1.59 # Reference the new list's items and current item.
1.60
1.61 @@ -115,8 +131,12 @@
1.62
1.63 if new_level < level:
1.64 while level > new_level:
1.65 - if min_level <= level <= max_level:
1.66 +
1.67 + # Retain a list at the minimum level.
1.68 +
1.69 + if min_level < level <= max_level:
1.70 lists.pop()
1.71 +
1.72 level -= 1
1.73
1.74 # Obtain the existing list and the current item.
1.75 @@ -136,7 +156,7 @@
1.76 # Replace the macro node's children with the top-level list.
1.77 # The macro cannot be replaced because it will be appearing inline.
1.78
1.79 - self.node.nodes = [lists[0]]
1.80 + self.node.nodes = lists and [lists[0]] or []
1.81
1.82 def find_headings(self, node, headings):
1.83