view website/issues/extensions/local_replace.py @ 4883:c0eb1d9f2cb7

website: local_replace.py: now explicitely match hashes with hex characters only if they are at least 4 chars long.
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 02 Apr 2014 14:34:37 +0200
parents 7f02765c6c31
children 595a8c5a6d13
line wrap: on
line source

import re

substitutions = [ (re.compile('debian:\#(?P<id>\d+)'),
                   '<a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=\g<id>">debian#\g<id></a>' ),
                  (re.compile('\#(?P<ws>\s*)(?P<id>\d+)'),
                   "<a href='issue\g<id>'>#\g<ws>\g<id></a>" ),
                  (re.compile('(?P<prews>^|\s+)issue(?P<ws>\s*)(?P<id>\d+)'),
                   "\g<prews><a href='issue\g<id>'>issue\g<ws>\g<id></a>" ),
                  # matching hg revison number or hash
                  (re.compile('(?P<prews>^|\s+)(?P<revstr>(revision|rev|r)\s?)(?P<revision>([1-9][0-9]*)|[0-9a-fA-F]{4,40})(?P<post>\W+|$)'),
                   "\g<prews><a href='http://sourceforge.net/p/roundup/code/ci/\g<revision>'>\g<revstr>\g<revision></a>\g<post>"),
                  ]

def local_replace(message):

    for cre, replacement in substitutions:
        message = cre.sub(replacement, message)

    return message


def init(instance):
    instance.registerUtil('localReplace', local_replace)

def quicktest(msgstr, should_replace = True):
    if not should_replace:
        print "(no)",
    print "'%s' -> '%s'" % (msgstr, local_replace(msgstr))

if "__main__" == __name__:
    print "Replacement examples. '(no)' should result in no replacement:"
    quicktest(" debian:#222")
    quicktest(" #555")
    quicktest("issue333")
    quicktest(" revision 222")
    quicktest(" r 222")
    quicktest(" wordthatendswithr 222", False)
    quicktest(" references", False)
    quicktest(" too many spaces r  222", False)
    quicktest("re-evaluate", False)
    quicktest("rex140eb", False)
    quicktest("rev 012", False) # too short for a hg hash
    quicktest("rev 0123", False) # too short for a hg hash
    quicktest("re140eb")
    quicktest(" r7140eb")
    quicktest(" rev7140eb ")
    quicktest("rev7140eb")
    quicktest("rev7140eb,")

Roundup Issue Tracker: http://roundup-tracker.org/