A new macro for the Italian wiki

This Stephan Hermann’s article shows a simple and useful macro, which gets a Launchpad bug’s id and returns a link to its bugtracker page.

So, I had an idea: why don’t generalize it and make it compatible with other projects bugtrackers?

Ok, let’s start analyzing the code:

from MoinMoin import config, wikiutil

projects = {
    "ubuntu"    : "https://bugs.launchpad.net/ubuntu/+bug/%d",
    "debian"	: "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=%d",
    "gnome"     : "http://bugzilla.gnome.org/show_bug.cgi?id=%d",
    "kde"       : "http://bugs.kde.org/show_bug.cgi?id=%d",
    "xfce"      : "http://bugzilla.xfce.org/show_bug.cgi?id=%d"
    }

projects_emblems = {
    "ubuntu"    : "https://launchpadlibrarian.net/9608570/ubuntu_emblem.png",
    "debian"	: "http://www.debian.org/favicon.ico",
    "gnome"     : "https://launchpadlibrarian.net/11396521/gnome.png",
    "kde"       : "https://launchpadlibrarian.net/7101115/klogo-official-crystal-14.png",
    "xfce"      : "https://launchpadlibrarian.net/4040580/mouse.png"
    }

def NoProjectSelectedException(Exception):
    """
    The macro raises an instance of this exception
    when the argument doesn't contain any project
    valid name.
    """
    def __init__(self, message):
        """
        Default __init__ method.
        """
        Exception.__init__(self, message)

def args_to_dict(args):
    """
    This method converts a string in a dictionary.
    It uses project's name (if valid) as key, and
    a list of bug ids as value.
    For example:

    The call:

    args_to_dict("gnome 3 4 6 ubuntu 1 9 6 kde 7 xfce")

    returns:

    {'kde': [7], 'xfce': [], 'gnome': [3, 4, 6], 'ubuntu': [1, 9, 6]}
    """
    args_dict = dict()
    project_selected = None
    for i in projects.keys():
        args_dict[i] = list()
    if args:
        args_list = args.split()
        for cur in args_list:
            if cur in projects.keys():
                project_selected = cur
            elif project_selected != None:
                try:
                    args_dict[project_selected].append(int(cur))
                except ValueError:
                    raise ValueError, u"Carattere non valido, i bug devono essere indicati con numeri interi: %s"
            else:
                raise NoProjectSelectedException, u"Nessun progetto selezionato per i bug elencati: %s"
    return args_dict

def execute(macro,args):
    """
    Execute method. It formats and returns
    the result.
    """
    request=macro.request
    formatter=macro.formatter
    result=""
    try:
        args_dict = args_to_dict(args)
    except ValueError, e:
        raise ValueError(e.message % args)
    except NoProjectSelectedException, e:
        raise NoProjectSelectedException(e.message % args)
    for i in args_dict.keys(): # i = current project
        for j in args_dict[i]: # j = current bug id
            result += u'<img src="%s" />' % projects_emblems[i]
            # result+='<img src="https://launchpadlibrarian.net/7094128/launchpad-icon.png" /> '
            # result+=formatter.url(on=1,url="http://launchpad.net/bugs/%s" % i)
            result+=formatter.url(on=1,url=projects[i] % j)
            result+=u"#%d" % j
            result+=formatter.url(on=0)
            result+=" "
    return result

Just like all wiki macros, it gets a string and returns a formatted output, in according with a specific model: input string contains projects’ names and lists of related bugs’ ids, then the output will be an HTML line containing the project emblem, followed by bugtracker links.

The correct syntax is:

[[Bug(project1 bug_id_1 project2 bug_id_1 bug_id_2 project3 bug_id_1 bug_id_2 bug_id_3)]]

Isn’t it easy? There aren’t separators, you need to remember that bugs ids refer to the first previous project (the ids order isn’t important). In case of error, depending by its kind, one of two exceptions will raise.

If you would have an example, open an Ubuntu-It wiki page in text mode and insert this:

Bug segnalati: [[Bug(ubuntu 68 3 5 gnome 21 kde 9 xfce 8 debian 76)]]

That’s the result of the previous example:

A list of reported bugs

That’s all!

One more thing: I would bring to your attention the Ubuntu Italian Documentation Team bazaar branch, where you can find the source code of all our MoinMoin mods.

1 thought on “A new macro for the Italian wiki

  1. Pingback: Bookmarks about Wiki

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.