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