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@162 | 23 | from moinformat.tree.moin import Block, Container, Heading, Link, List, \ |
paul@162 | 24 | ListItem, Text |
paul@162 | 25 | |
paul@162 | 26 | def in_range(min_level, level, max_level): |
paul@162 | 27 | |
paul@162 | 28 | """ |
paul@162 | 29 | Test that 'min_level' <= 'level' <= 'max_level', only imposing tests |
paul@162 | 30 | involving limits not set to None. |
paul@162 | 31 | """ |
paul@162 | 32 | |
paul@162 | 33 | return (min_level is None or min_level <= level) and \ |
paul@162 | 34 | (max_level is None or level <= max_level) |
paul@162 | 35 | |
paul@162 | 36 | def above_minimum(min_level, level, max_level): |
paul@162 | 37 | |
paul@162 | 38 | """ |
paul@162 | 39 | Test that 'min_level' < 'level' <= 'max_level', only imposing tests |
paul@162 | 40 | involving limits not set to None. |
paul@162 | 41 | """ |
paul@162 | 42 | |
paul@162 | 43 | return (min_level is None or min_level < level) and \ |
paul@162 | 44 | (max_level is None or level <= max_level) |
paul@89 | 45 | |
paul@89 | 46 | class TableOfContents(Macro): |
paul@89 | 47 | |
paul@89 | 48 | "A table of contents macro." |
paul@89 | 49 | |
paul@89 | 50 | name = "TableOfContents" |
paul@89 | 51 | |
paul@89 | 52 | def evaluate(self): |
paul@89 | 53 | |
paul@89 | 54 | "Evaluate the macro, producing a table of contents." |
paul@89 | 55 | |
paul@89 | 56 | arglist = [] |
paul@89 | 57 | _defaults = [None] * 2 |
paul@89 | 58 | |
paul@89 | 59 | for arg, default in map(None, self.node.args, _defaults): |
paul@89 | 60 | if arg is not None: |
paul@89 | 61 | try: |
paul@89 | 62 | arg = max(1, int(arg.strip())) |
paul@89 | 63 | except ValueError: |
paul@89 | 64 | arg = None |
paul@89 | 65 | arglist.append(arg) |
paul@89 | 66 | |
paul@89 | 67 | self.make_table(arglist[0], arglist[1]) |
paul@89 | 68 | |
paul@89 | 69 | def make_table(self, min_level=None, max_level=None): |
paul@89 | 70 | |
paul@89 | 71 | """ |
paul@89 | 72 | Make a table of contents with the given 'min_level' and 'max_level' of |
paul@89 | 73 | headings. |
paul@89 | 74 | """ |
paul@89 | 75 | |
paul@89 | 76 | headings = [] |
paul@89 | 77 | self.find_headings(self.doc, headings) |
paul@89 | 78 | |
paul@89 | 79 | if not headings: |
paul@89 | 80 | return |
paul@89 | 81 | |
paul@89 | 82 | # Common list features. |
paul@89 | 83 | |
paul@89 | 84 | marker = "1." |
paul@89 | 85 | space = " " |
paul@89 | 86 | num = "1" |
paul@89 | 87 | |
paul@89 | 88 | # Start with no lists, no current item. |
paul@89 | 89 | |
paul@89 | 90 | lists = [] |
paul@89 | 91 | item = None |
paul@89 | 92 | level = 0 |
paul@89 | 93 | |
paul@89 | 94 | for heading in headings: |
paul@89 | 95 | new_level = heading.level |
paul@89 | 96 | |
paul@89 | 97 | # Create new lists if the level increases. |
paul@89 | 98 | |
paul@89 | 99 | if new_level > level: |
paul@89 | 100 | while level < new_level: |
paul@89 | 101 | level += 1 |
paul@89 | 102 | |
paul@125 | 103 | # Ignore levels outside the range of interest. |
paul@125 | 104 | |
paul@162 | 105 | if not in_range(min_level, level, max_level): |
paul@89 | 106 | continue |
paul@89 | 107 | |
paul@89 | 108 | # Determine whether the heading should be generated at this |
paul@125 | 109 | # level or whether there are intermediate levels being |
paul@125 | 110 | # produced. |
paul@89 | 111 | |
paul@127 | 112 | nodes = level == new_level and self.get_entry(heading) or [] |
paul@89 | 113 | indent = level - 1 |
paul@89 | 114 | |
paul@125 | 115 | # Create a new item for the heading or sublists. |
paul@125 | 116 | |
paul@125 | 117 | new_item = ListItem(nodes, indent, marker, space, None) |
paul@125 | 118 | |
paul@125 | 119 | # Either revive an existing list. |
paul@89 | 120 | |
paul@125 | 121 | if level == min_level and lists: |
paul@125 | 122 | new_list = lists[-1] |
paul@125 | 123 | new_items = new_list.nodes |
paul@125 | 124 | |
paul@125 | 125 | # Or make a list and add an item to it. |
paul@89 | 126 | |
paul@125 | 127 | else: |
paul@125 | 128 | new_items = [] |
paul@125 | 129 | new_list = List(new_items, indent, marker, num) |
paul@125 | 130 | |
paul@125 | 131 | # Add the list to the current item, if any. |
paul@89 | 132 | |
paul@125 | 133 | if item: |
paul@125 | 134 | item.nodes.append(new_list) |
paul@125 | 135 | |
paul@125 | 136 | # Record the new list. |
paul@89 | 137 | |
paul@125 | 138 | lists.append(new_list) |
paul@89 | 139 | |
paul@125 | 140 | # Add the item to the new or revived list. |
paul@125 | 141 | |
paul@125 | 142 | new_items.append(new_item) |
paul@89 | 143 | |
paul@89 | 144 | # Reference the new list's items and current item. |
paul@89 | 145 | |
paul@89 | 146 | items = new_items |
paul@89 | 147 | item = new_item |
paul@89 | 148 | |
paul@89 | 149 | else: |
paul@89 | 150 | # Retrieve an existing list if the level decreases. |
paul@89 | 151 | |
paul@89 | 152 | if new_level < level: |
paul@89 | 153 | while level > new_level: |
paul@125 | 154 | |
paul@125 | 155 | # Retain a list at the minimum level. |
paul@125 | 156 | |
paul@162 | 157 | if above_minimum(min_level, level, max_level): |
paul@89 | 158 | lists.pop() |
paul@125 | 159 | |
paul@89 | 160 | level -= 1 |
paul@89 | 161 | |
paul@89 | 162 | # Obtain the existing list and the current item. |
paul@89 | 163 | |
paul@89 | 164 | items = lists[-1].nodes |
paul@89 | 165 | item = items[-1] |
paul@89 | 166 | |
paul@89 | 167 | # Add the heading as an item. |
paul@89 | 168 | |
paul@162 | 169 | if in_range(min_level, level, max_level): |
paul@162 | 170 | |
paul@89 | 171 | indent = level - 1 |
paul@127 | 172 | nodes = self.get_entry(heading) |
paul@89 | 173 | |
paul@89 | 174 | item = ListItem(nodes, indent, marker, space, None) |
paul@89 | 175 | items.append(item) |
paul@89 | 176 | |
paul@162 | 177 | # Replace the macro node with the top-level list. |
paul@162 | 178 | |
paul@162 | 179 | self.insert_table(lists[0]) |
paul@162 | 180 | |
paul@162 | 181 | def insert_table(self, content): |
paul@162 | 182 | |
paul@162 | 183 | "Insert the given 'content' into the document." |
paul@162 | 184 | |
paul@162 | 185 | macro = self.node |
paul@162 | 186 | parent = macro.parent |
paul@162 | 187 | region = macro.region |
paul@162 | 188 | |
paul@162 | 189 | # Replace the macro if it is not inside a block. |
paul@162 | 190 | # NOTE: This attempts to avoid blocks being used in inline-only contexts |
paul@162 | 191 | # NOTE: but may not be successful in every case. |
paul@162 | 192 | |
paul@162 | 193 | if not isinstance(parent, Block) or parent is region: |
paul@162 | 194 | parent.replace(macro, content) |
paul@89 | 195 | |
paul@162 | 196 | # Split any block containing the macro into preceding and following |
paul@162 | 197 | # parts. |
paul@162 | 198 | |
paul@162 | 199 | else: |
paul@162 | 200 | following = parent.split_at(macro) |
paul@162 | 201 | |
paul@162 | 202 | # Insert any non-empty following block. |
paul@162 | 203 | |
paul@162 | 204 | if not following.whitespace_only(): |
paul@162 | 205 | region.insert_after(parent, following) |
paul@162 | 206 | |
paul@162 | 207 | # Insert the new content. |
paul@162 | 208 | |
paul@162 | 209 | region.insert_after(parent, content) |
paul@162 | 210 | |
paul@162 | 211 | # Remove any empty preceding block. |
paul@162 | 212 | |
paul@162 | 213 | if parent.whitespace_only(): |
paul@162 | 214 | region.remove(parent) |
paul@89 | 215 | |
paul@89 | 216 | def find_headings(self, node, headings): |
paul@89 | 217 | |
paul@89 | 218 | "Find headings under 'node', adding them to the 'headings' list." |
paul@89 | 219 | |
paul@89 | 220 | if node.nodes: |
paul@89 | 221 | for n in node.nodes: |
paul@89 | 222 | if isinstance(n, Heading): |
paul@89 | 223 | headings.append(n) |
paul@89 | 224 | elif isinstance(n, Container): |
paul@89 | 225 | self.find_headings(n, headings) |
paul@89 | 226 | |
paul@127 | 227 | def get_entry(self, heading): |
paul@127 | 228 | |
paul@127 | 229 | "Return nodes for an entry involving 'heading'." |
paul@127 | 230 | |
paul@128 | 231 | return [Link(heading.nodes[:], "#%s" % heading.identifier), Text("\n")] |
paul@127 | 232 | |
paul@89 | 233 | macro = TableOfContents |
paul@89 | 234 | |
paul@89 | 235 | # vim: tabstop=4 expandtab shiftwidth=4 |