paul@236 | 1 | #!/usr/bin/env python |
paul@236 | 2 | |
paul@236 | 3 | """ |
paul@236 | 4 | PageList macro for Moin compatibility. |
paul@236 | 5 | |
paul@236 | 6 | Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> |
paul@236 | 7 | |
paul@236 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@236 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@236 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@236 | 11 | version. |
paul@236 | 12 | |
paul@236 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@236 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@236 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@236 | 16 | details. |
paul@236 | 17 | |
paul@236 | 18 | You should have received a copy of the GNU General Public License along with |
paul@236 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@236 | 20 | """ |
paul@236 | 21 | |
paul@236 | 22 | from moinformat.macros.common import Macro |
paul@236 | 23 | from moinformat.tree.moin import Link, LinkLabel, List, ListItem, Text |
paul@236 | 24 | from moinformat.utils.links import LinkTarget |
paul@236 | 25 | |
paul@236 | 26 | class PageListMacro(Macro): |
paul@236 | 27 | |
paul@236 | 28 | "A page list macro." |
paul@236 | 29 | |
paul@236 | 30 | name = "PageList" |
paul@236 | 31 | |
paul@236 | 32 | def evaluate(self): |
paul@236 | 33 | |
paul@236 | 34 | "Evaluate the macro, producing a list of pages." |
paul@236 | 35 | |
paul@236 | 36 | # Obtain the parameters. |
paul@236 | 37 | |
paul@236 | 38 | args = self.node.args |
paul@236 | 39 | pattern = args and args[0] or None |
paul@236 | 40 | |
paul@236 | 41 | # Access the input context to get page details. |
paul@236 | 42 | |
paul@236 | 43 | input = self.metadata.get_input() |
paul@236 | 44 | |
paul@236 | 45 | # Select pages according to the parameters. |
paul@236 | 46 | # NOTE: Currently using prefix matching only. |
paul@236 | 47 | |
paul@236 | 48 | if pattern: |
paul@236 | 49 | pagenames = [] |
paul@236 | 50 | for pagename in input.all(): |
paul@236 | 51 | if pagename.startswith(pattern): |
paul@236 | 52 | pagenames.append(pagename) |
paul@236 | 53 | else: |
paul@236 | 54 | pagenames = input.all() |
paul@236 | 55 | |
paul@236 | 56 | # Sort the pagenames. |
paul@236 | 57 | # NOTE: The actual sort order should be controlled. |
paul@236 | 58 | |
paul@236 | 59 | pagenames.sort() |
paul@236 | 60 | |
paul@236 | 61 | # Prepare a list of links. |
paul@236 | 62 | |
paul@236 | 63 | items = [] |
paul@236 | 64 | indent = 0 |
paul@236 | 65 | marker = "*" |
paul@236 | 66 | space = " " |
paul@236 | 67 | num = None |
paul@236 | 68 | |
paul@236 | 69 | for pagename in pagenames: |
paul@236 | 70 | text = [Text(pagename)] |
paul@236 | 71 | nodes = [Link([LinkLabel(text)], LinkTarget("page", pagename))] |
paul@236 | 72 | items.append(ListItem(nodes, indent, marker, space, num)) |
paul@236 | 73 | |
paul@236 | 74 | # Replace the macro node with the list. |
paul@236 | 75 | |
paul@236 | 76 | macro = self.node |
paul@236 | 77 | macro.parent.replace(macro, List(items)) |
paul@236 | 78 | |
paul@236 | 79 | macro = PageListMacro |
paul@236 | 80 | |
paul@236 | 81 | # vim: tabstop=4 expandtab shiftwidth=4 |