1 #!/usr/bin/env python 2 3 """ 4 Mail-to 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, Text 24 from moinformat.utils.links import LinkTarget 25 26 def get_address(label): 27 28 "Decode the address found in 'label'." 29 30 l = [] 31 d = {"AT" : "@", "DASH" : "-", "DOT" : "."} 32 33 for word in label.split(): 34 if d.has_key(word): 35 l.append(d[word]) 36 elif not word.isupper(): 37 l.append(word) 38 39 return u"".join(l) 40 41 class MailToMacro(Macro): 42 43 "A mail-to macro." 44 45 name = "MailTo" 46 47 def evaluate(self): 48 49 "Evaluate the macro, producing a mailto link." 50 51 # Replace the macro node with the link. 52 53 macro = self.node 54 args = self.node.args 55 56 # Obtain a label and an address. 57 58 label = args and args[0] or None 59 address = get_address(label) 60 61 if address: 62 link = Link([LinkLabel([Text(label)])], 63 LinkTarget("url", "mailto:%s" % address)) 64 else: 65 link = Text("") 66 67 macro.parent.replace(macro, link) 68 69 macro = MailToMacro 70 71 # vim: tabstop=4 expandtab shiftwidth=4