# HG changeset patch # User Paul Boddie # Date 1555261983 -7200 # Node ID 617e344adb77a1a5c9b6bc0dc1c32706f9250d95 # Parent bd56fc44652e0d772a0c5f908dcf289bb62590f0 Added an initial version of the PageList macro. diff -r bd56fc44652e -r 617e344adb77 moinformat/macros/pagelist.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinformat/macros/pagelist.py Sun Apr 14 19:13:03 2019 +0200 @@ -0,0 +1,81 @@ +#!/usr/bin/env python + +""" +PageList macro for Moin compatibility. + +Copyright (C) 2019 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from moinformat.macros.common import Macro +from moinformat.tree.moin import Link, LinkLabel, List, ListItem, Text +from moinformat.utils.links import LinkTarget + +class PageListMacro(Macro): + + "A page list macro." + + name = "PageList" + + def evaluate(self): + + "Evaluate the macro, producing a list of pages." + + # Obtain the parameters. + + args = self.node.args + pattern = args and args[0] or None + + # Access the input context to get page details. + + input = self.metadata.get_input() + + # Select pages according to the parameters. + # NOTE: Currently using prefix matching only. + + if pattern: + pagenames = [] + for pagename in input.all(): + if pagename.startswith(pattern): + pagenames.append(pagename) + else: + pagenames = input.all() + + # Sort the pagenames. + # NOTE: The actual sort order should be controlled. + + pagenames.sort() + + # Prepare a list of links. + + items = [] + indent = 0 + marker = "*" + space = " " + num = None + + for pagename in pagenames: + text = [Text(pagename)] + nodes = [Link([LinkLabel(text)], LinkTarget("page", pagename))] + items.append(ListItem(nodes, indent, marker, space, num)) + + # Replace the macro node with the list. + + macro = self.node + macro.parent.replace(macro, List(items)) + +macro = PageListMacro + +# vim: tabstop=4 expandtab shiftwidth=4