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