paul@241 | 1 | #!/usr/bin/env python |
paul@241 | 2 | |
paul@241 | 3 | """ |
paul@241 | 4 | Mail-to macro for Moin compatibility. |
paul@241 | 5 | |
paul@241 | 6 | Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> |
paul@241 | 7 | |
paul@241 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@241 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@241 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@241 | 11 | version. |
paul@241 | 12 | |
paul@241 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@241 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@241 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@241 | 16 | details. |
paul@241 | 17 | |
paul@241 | 18 | You should have received a copy of the GNU General Public License along with |
paul@241 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@241 | 20 | """ |
paul@241 | 21 | |
paul@241 | 22 | from moinformat.macros.common import Macro |
paul@241 | 23 | from moinformat.tree.moin import Link, LinkLabel, Text |
paul@241 | 24 | from moinformat.utils.links import LinkTarget |
paul@241 | 25 | |
paul@241 | 26 | def get_address(label): |
paul@241 | 27 | |
paul@241 | 28 | "Decode the address found in 'label'." |
paul@241 | 29 | |
paul@241 | 30 | l = [] |
paul@241 | 31 | d = {"AT" : "@", "DASH" : "-", "DOT" : "."} |
paul@241 | 32 | |
paul@241 | 33 | for word in label.split(): |
paul@241 | 34 | if d.has_key(word): |
paul@241 | 35 | l.append(d[word]) |
paul@241 | 36 | elif not word.isupper(): |
paul@241 | 37 | l.append(word) |
paul@241 | 38 | |
paul@241 | 39 | return u"".join(l) |
paul@241 | 40 | |
paul@241 | 41 | class MailToMacro(Macro): |
paul@241 | 42 | |
paul@241 | 43 | "A mail-to macro." |
paul@241 | 44 | |
paul@241 | 45 | name = "MailTo" |
paul@241 | 46 | |
paul@241 | 47 | def evaluate(self): |
paul@241 | 48 | |
paul@241 | 49 | "Evaluate the macro, producing a mailto link." |
paul@241 | 50 | |
paul@241 | 51 | # Replace the macro node with the link. |
paul@241 | 52 | |
paul@241 | 53 | macro = self.node |
paul@241 | 54 | args = self.node.args |
paul@241 | 55 | |
paul@241 | 56 | # Obtain a label and an address. |
paul@241 | 57 | |
paul@241 | 58 | label = args and args[0] or None |
paul@241 | 59 | address = get_address(label) |
paul@241 | 60 | |
paul@241 | 61 | if address: |
paul@241 | 62 | link = Link([LinkLabel([Text(label)])], |
paul@241 | 63 | LinkTarget("url", "mailto:%s" % address)) |
paul@241 | 64 | else: |
paul@241 | 65 | link = Text("") |
paul@241 | 66 | |
paul@241 | 67 | macro.parent.replace(macro, link) |
paul@241 | 68 | |
paul@241 | 69 | macro = MailToMacro |
paul@241 | 70 | |
paul@241 | 71 | # vim: tabstop=4 expandtab shiftwidth=4 |