paul@89 | 1 | #!/usr/bin/env python |
paul@89 | 2 | |
paul@89 | 3 | """ |
paul@89 | 4 | Table of contents macro. |
paul@89 | 5 | |
paul@89 | 6 | Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@89 | 7 | |
paul@89 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@89 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@89 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@89 | 11 | version. |
paul@89 | 12 | |
paul@89 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@89 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@89 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@89 | 16 | details. |
paul@89 | 17 | |
paul@89 | 18 | You should have received a copy of the GNU General Public License along with |
paul@89 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@89 | 20 | """ |
paul@89 | 21 | |
paul@89 | 22 | from moinformat.macros.common import Macro |
paul@89 | 23 | from moinformat.tree.moin import Container, Heading, List, ListItem, Text |
paul@89 | 24 | |
paul@89 | 25 | class TableOfContents(Macro): |
paul@89 | 26 | |
paul@89 | 27 | "A table of contents macro." |
paul@89 | 28 | |
paul@89 | 29 | name = "TableOfContents" |
paul@89 | 30 | |
paul@89 | 31 | def evaluate(self): |
paul@89 | 32 | |
paul@89 | 33 | "Evaluate the macro, producing a table of contents." |
paul@89 | 34 | |
paul@89 | 35 | arglist = [] |
paul@89 | 36 | _defaults = [None] * 2 |
paul@89 | 37 | |
paul@89 | 38 | for arg, default in map(None, self.node.args, _defaults): |
paul@89 | 39 | if arg is not None: |
paul@89 | 40 | try: |
paul@89 | 41 | arg = max(1, int(arg.strip())) |
paul@89 | 42 | except ValueError: |
paul@89 | 43 | arg = None |
paul@89 | 44 | arglist.append(arg) |
paul@89 | 45 | |
paul@89 | 46 | self.make_table(arglist[0], arglist[1]) |
paul@89 | 47 | |
paul@89 | 48 | def make_table(self, min_level=None, max_level=None): |
paul@89 | 49 | |
paul@89 | 50 | """ |
paul@89 | 51 | Make a table of contents with the given 'min_level' and 'max_level' of |
paul@89 | 52 | headings. |
paul@89 | 53 | """ |
paul@89 | 54 | |
paul@89 | 55 | headings = [] |
paul@89 | 56 | self.find_headings(self.doc, headings) |
paul@89 | 57 | |
paul@89 | 58 | if not headings: |
paul@89 | 59 | return |
paul@89 | 60 | |
paul@89 | 61 | # Common list features. |
paul@89 | 62 | |
paul@89 | 63 | marker = "1." |
paul@89 | 64 | space = " " |
paul@89 | 65 | num = "1" |
paul@89 | 66 | nl = [Text("\n")] |
paul@89 | 67 | |
paul@89 | 68 | # Start with no lists, no current item. |
paul@89 | 69 | |
paul@89 | 70 | lists = [] |
paul@89 | 71 | item = None |
paul@89 | 72 | level = 0 |
paul@89 | 73 | |
paul@89 | 74 | for heading in headings: |
paul@89 | 75 | new_level = heading.level |
paul@89 | 76 | |
paul@89 | 77 | # Create new lists if the level increases. |
paul@89 | 78 | |
paul@89 | 79 | if new_level > level: |
paul@89 | 80 | while level < new_level: |
paul@89 | 81 | level += 1 |
paul@89 | 82 | |
paul@89 | 83 | if not (min_level <= level <= max_level): |
paul@89 | 84 | continue |
paul@89 | 85 | |
paul@89 | 86 | # Determine whether the heading should be generated at this |
paul@89 | 87 | # level. |
paul@89 | 88 | |
paul@89 | 89 | nodes = level == new_level and heading.nodes[:] + nl or [] |
paul@89 | 90 | indent = level - 1 |
paul@89 | 91 | |
paul@89 | 92 | # Make a list and add an item to it. |
paul@89 | 93 | |
paul@89 | 94 | new_items = [] |
paul@89 | 95 | new_list = List(new_items, indent, marker, num) |
paul@89 | 96 | new_item = ListItem(nodes, indent, marker, space, None) |
paul@89 | 97 | new_items.append(new_item) |
paul@89 | 98 | |
paul@89 | 99 | # Add the list to the current item, if any. |
paul@89 | 100 | |
paul@89 | 101 | if item: |
paul@89 | 102 | item.nodes.append(new_list) |
paul@89 | 103 | |
paul@89 | 104 | # Record the new list. |
paul@89 | 105 | |
paul@89 | 106 | lists.append(new_list) |
paul@89 | 107 | |
paul@89 | 108 | # Reference the new list's items and current item. |
paul@89 | 109 | |
paul@89 | 110 | items = new_items |
paul@89 | 111 | item = new_item |
paul@89 | 112 | |
paul@89 | 113 | else: |
paul@89 | 114 | # Retrieve an existing list if the level decreases. |
paul@89 | 115 | |
paul@89 | 116 | if new_level < level: |
paul@89 | 117 | while level > new_level: |
paul@89 | 118 | if min_level <= level <= max_level: |
paul@89 | 119 | lists.pop() |
paul@89 | 120 | level -= 1 |
paul@89 | 121 | |
paul@89 | 122 | # Obtain the existing list and the current item. |
paul@89 | 123 | |
paul@89 | 124 | items = lists[-1].nodes |
paul@89 | 125 | item = items[-1] |
paul@89 | 126 | |
paul@89 | 127 | # Add the heading as an item. |
paul@89 | 128 | |
paul@89 | 129 | if min_level <= level <= max_level: |
paul@89 | 130 | indent = level - 1 |
paul@89 | 131 | nodes = heading.nodes[:] + nl |
paul@89 | 132 | |
paul@89 | 133 | item = ListItem(nodes, indent, marker, space, None) |
paul@89 | 134 | items.append(item) |
paul@89 | 135 | |
paul@89 | 136 | # Replace the macro node's children with the top-level list. |
paul@110 | 137 | # The macro cannot be replaced because it will be appearing inline. |
paul@89 | 138 | |
paul@89 | 139 | self.node.nodes = [lists[0]] |
paul@89 | 140 | |
paul@89 | 141 | def find_headings(self, node, headings): |
paul@89 | 142 | |
paul@89 | 143 | "Find headings under 'node', adding them to the 'headings' list." |
paul@89 | 144 | |
paul@89 | 145 | if node.nodes: |
paul@89 | 146 | for n in node.nodes: |
paul@89 | 147 | if isinstance(n, Heading): |
paul@89 | 148 | headings.append(n) |
paul@89 | 149 | elif isinstance(n, Container): |
paul@89 | 150 | self.find_headings(n, headings) |
paul@89 | 151 | |
paul@89 | 152 | macro = TableOfContents |
paul@89 | 153 | |
paul@89 | 154 | # vim: tabstop=4 expandtab shiftwidth=4 |