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@319 | 6 | Copyright (C) 2019, 2022 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@243 | 22 | from mimetypes import guess_type |
paul@234 | 23 | from moinformat.macros.common import Macro |
paul@234 | 24 | from moinformat.tree.moin import Link, LinkLabel, List, ListItem, Text |
paul@234 | 25 | from moinformat.utils.links import LinkTarget |
paul@234 | 26 | |
paul@234 | 27 | class AttachListMacro(Macro): |
paul@234 | 28 | |
paul@234 | 29 | "An attachment list macro." |
paul@234 | 30 | |
paul@234 | 31 | name = "AttachList" |
paul@234 | 32 | |
paul@234 | 33 | def evaluate(self): |
paul@234 | 34 | |
paul@234 | 35 | "Evaluate the macro, producing a list of attachments." |
paul@234 | 36 | |
paul@234 | 37 | # Obtain the parameters. |
paul@234 | 38 | |
paul@234 | 39 | args = self.node.args |
paul@234 | 40 | |
paul@234 | 41 | pagename = args and args[0] or self.metadata.get("pagename") |
paul@234 | 42 | mimetype = len(args) > 1 and args[1] or None |
paul@234 | 43 | |
paul@234 | 44 | # Access the input context to get the attachment details. |
paul@234 | 45 | |
paul@234 | 46 | input = self.metadata.get_input() |
paul@234 | 47 | |
paul@234 | 48 | # Select attachments by type. |
paul@243 | 49 | |
paul@243 | 50 | all_filenames = pagename and input.get_attachments(pagename) or [] |
paul@243 | 51 | |
paul@243 | 52 | if mimetype: |
paul@243 | 53 | filenames = [] |
paul@243 | 54 | |
paul@243 | 55 | for filename in all_filenames: |
paul@243 | 56 | type, encoding = guess_type(filename) |
paul@243 | 57 | |
paul@243 | 58 | if type == mimetype: |
paul@243 | 59 | filenames.append(filename) |
paul@243 | 60 | else: |
paul@243 | 61 | filenames = all_filenames |
paul@234 | 62 | |
paul@244 | 63 | # Sort the filenames. |
paul@244 | 64 | # NOTE: The actual sort order should be controlled. |
paul@244 | 65 | |
paul@244 | 66 | filenames.sort() |
paul@244 | 67 | |
paul@234 | 68 | # Prepare a list of links. |
paul@234 | 69 | |
paul@234 | 70 | items = [] |
paul@234 | 71 | indent = 0 |
paul@234 | 72 | marker = "*" |
paul@234 | 73 | space = " " |
paul@234 | 74 | num = None |
paul@234 | 75 | |
paul@234 | 76 | for filename in filenames: |
paul@234 | 77 | text = [Text(filename)] |
paul@319 | 78 | nodes = [Link([LinkLabel(text)], LinkTarget("attachment", filename, |
paul@319 | 79 | pagename=pagename))] |
paul@234 | 80 | items.append(ListItem(nodes, indent, marker, space, num)) |
paul@234 | 81 | |
paul@234 | 82 | # Replace the macro node with the list. |
paul@234 | 83 | |
paul@234 | 84 | macro = self.node |
paul@234 | 85 | macro.parent.replace(macro, List(items)) |
paul@234 | 86 | |
paul@234 | 87 | macro = AttachListMacro |
paul@234 | 88 | |
paul@234 | 89 | # vim: tabstop=4 expandtab shiftwidth=4 |