1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/macros/mailto.py Mon Apr 15 16:13:12 2019 +0200
1.3 @@ -0,0 +1,71 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Mail-to macro for Moin compatibility.
1.8 +
1.9 +Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from moinformat.macros.common import Macro
1.26 +from moinformat.tree.moin import Link, LinkLabel, Text
1.27 +from moinformat.utils.links import LinkTarget
1.28 +
1.29 +def get_address(label):
1.30 +
1.31 + "Decode the address found in 'label'."
1.32 +
1.33 + l = []
1.34 + d = {"AT" : "@", "DASH" : "-", "DOT" : "."}
1.35 +
1.36 + for word in label.split():
1.37 + if d.has_key(word):
1.38 + l.append(d[word])
1.39 + elif not word.isupper():
1.40 + l.append(word)
1.41 +
1.42 + return u"".join(l)
1.43 +
1.44 +class MailToMacro(Macro):
1.45 +
1.46 + "A mail-to macro."
1.47 +
1.48 + name = "MailTo"
1.49 +
1.50 + def evaluate(self):
1.51 +
1.52 + "Evaluate the macro, producing a mailto link."
1.53 +
1.54 + # Replace the macro node with the link.
1.55 +
1.56 + macro = self.node
1.57 + args = self.node.args
1.58 +
1.59 + # Obtain a label and an address.
1.60 +
1.61 + label = args and args[0] or None
1.62 + address = get_address(label)
1.63 +
1.64 + if address:
1.65 + link = Link([LinkLabel([Text(label)])],
1.66 + LinkTarget("url", "mailto:%s" % address))
1.67 + else:
1.68 + link = Text("")
1.69 +
1.70 + macro.parent.replace(macro, link)
1.71 +
1.72 +macro = MailToMacro
1.73 +
1.74 +# vim: tabstop=4 expandtab shiftwidth=4