From f89a821a0e8154d55186cacfa5325f4342e91a12 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 18 2015 14:34:56 +0000 Subject: Add a crossref filter to add cross-reference links between issues in their comment Fixes http://209.132.184.222/progit/issue/57 --- diff --git a/progit/ui/filters.py b/progit/ui/filters.py index e5bbd86..0f0b575 100644 --- a/progit/ui/filters.py +++ b/progit/ui/filters.py @@ -180,6 +180,36 @@ def shorted_commit(cid): return cid[:APP.config['SHORT_LENGTH']] +@APP.template_filter('crossref') +def crossref_filter(text): + """ Template filter adding a link when the provided text references + another issue or pull-request. + """ + regex = re.compile('.*\s#(\d+)', re.I) + if text: + if regex.match(text): + + url = flask.request.url + username = None + if 'fork/' in flask.request.url: + username, project = url.split('fork/')[1].split('/', 2)[:2] + else: + project = url.split( + flask.request.url_root)[1].split('/', 1)[0] + + issueid = regex.match(text).group(1) + text = text.replace('#%s' % issueid, '[#%s](%s)' % ( + issueid, flask.url_for( + 'view_issue', username=username, repo=project, + issueid=issueid) + ) + ) + + return text + else: + return '' + + @APP.template_filter('markdown') def markdown_filter(text): """ Template filter converting a string into html content using the