# HG changeset patch # User Paul Boddie # Date 1555337592 -7200 # Node ID 3d6705cc567d6b4ae1933a91961a6f91a914e78d # Parent ccd7e45890fb4f48e037c1117a9c027362ce5667 Added an initial version of the MailTo macro. diff -r ccd7e45890fb -r 3d6705cc567d moinformat/macros/mailto.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinformat/macros/mailto.py Mon Apr 15 16:13:12 2019 +0200 @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +""" +Mail-to macro for Moin compatibility. + +Copyright (C) 2019 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from moinformat.macros.common import Macro +from moinformat.tree.moin import Link, LinkLabel, Text +from moinformat.utils.links import LinkTarget + +def get_address(label): + + "Decode the address found in 'label'." + + l = [] + d = {"AT" : "@", "DASH" : "-", "DOT" : "."} + + for word in label.split(): + if d.has_key(word): + l.append(d[word]) + elif not word.isupper(): + l.append(word) + + return u"".join(l) + +class MailToMacro(Macro): + + "A mail-to macro." + + name = "MailTo" + + def evaluate(self): + + "Evaluate the macro, producing a mailto link." + + # Replace the macro node with the link. + + macro = self.node + args = self.node.args + + # Obtain a label and an address. + + label = args and args[0] or None + address = get_address(label) + + if address: + link = Link([LinkLabel([Text(label)])], + LinkTarget("url", "mailto:%s" % address)) + else: + link = Text("") + + macro.parent.replace(macro, link) + +macro = MailToMacro + +# vim: tabstop=4 expandtab shiftwidth=4