Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix corner cases in recognizing links to issues and revisions.

Add the patterns for these two in ``config.py`` so they can be
overridden.

Also add the template to use when recognizing a link to another issue
there.

Fixes http://plone.org/products/poi/issues/262
  • Loading branch information...
commit a5df1caa315f894733f0eb502263a76785b2c266 1 parent 49ea2eb
@mauritsvanrees mauritsvanrees authored
View
7 CHANGES.rst
@@ -5,7 +5,12 @@ Changelog for Poi
2.2.6 (unreleased)
------------------
-- Nothing changed yet.
+- Fix corner cases in recognizing links to issues and revisions. Add
+ the patterns for these two in ``config.py`` so they can be
+ overridden. Also add the template to use when recognizing a link to
+ another issue there.
+ Fixes http://plone.org/products/poi/issues/262
+ [maurits]
2.2.5 (2013-08-22)
View
8 Products/Poi/config.py
@@ -18,3 +18,11 @@
# issue/response text.
ISSUE_MIME_TYPES = ('text/x-web-intelligent', 'text/plain')
DEFAULT_ISSUE_MIME_TYPE = 'text/x-web-intelligent'
+# Patterns used for recognizing links to issues and revisions:
+ISSUE_RECOGNITION_PATTERNS = \
+ [r'\B#[1-9][0-9]*\b', r'\bissue:[1-9][0-9]*\b',
+ r'\bticket:[1-9][0-9]*\b', r'\bbug:[1-9][0-9]*\b']
+REVISION_RECOGNITION_PATTERNS = \
+ [r'\br[0-9]+\b', r'\bchangeset:[0-9]+\b', r'\B\[[0-9]+\]\B']
+# Template to use when recognizing a link to another issue:
+ISSUE_LINK_TEMPLATE = '<a href="../%(bug)s">%(linktext)s</a>'
View
12 Products/Poi/content/PoiTracker.py
@@ -43,6 +43,8 @@
from Products.Poi import PoiMessageFactory as _
from Products.Poi import permissions
from Products.Poi.config import PROJECTNAME
+from Products.Poi.config import ISSUE_RECOGNITION_PATTERNS
+from Products.Poi.config import REVISION_RECOGNITION_PATTERNS
from Products.Poi.interfaces import ITracker
from Products.Poi.utils import linkBugs
from Products.Poi.utils import linkSvn
@@ -321,15 +323,9 @@ def linkDetection(self, text):
issuefolder = self.restrictedTraverse('@@issuefolder')
issues = catalog.searchResults(issuefolder.buildIssueSearchQuery(None))
ids = frozenset([issue.id for issue in issues])
-
- # XXX/TODO: should these patterns live in the config file?
- text = linkBugs(text, ids,
- ['#[1-9][0-9]*', 'issue:[1-9][0-9]*',
- 'ticket:[1-9][0-9]*', 'bug:[1-9][0-9]*'])
+ text = linkBugs(text, ids, ISSUE_RECOGNITION_PATTERNS)
svnUrl = self.getSvnUrl()
- text = linkSvn(text, svnUrl,
- ['r[0-9]+', 'changeset:[0-9]+', '\[[0-9]+\]'])
-
+ text = linkSvn(text, svnUrl, REVISION_RECOGNITION_PATTERNS)
return text
security.declareProtected(permissions.View, 'isUsingReleases')
View
23 Products/Poi/tests/linkdetection.txt
@@ -45,6 +45,8 @@ Look for problems::
True
>>> getNumberFromString('#007')
'7'
+ >>> getNumberFromString('my.html#7')
+ '7'
With more numbers, the first one is taken::
@@ -64,6 +66,7 @@ text with possibly links::
Now we test this with patterns::
+ >>> from Products.Poi.config import ISSUE_RECOGNITION_PATTERNS
>>> patterns = ['test']
>>> linkBugs(text, ids, patterns)
'issue:1 #2 r3 [4] ticket:5.'
@@ -76,13 +79,17 @@ Now we test this with patterns::
>>> patterns = ['[[1-9][0-9]*]']
>>> linkBugs(text, ids, patterns)
'issue:1 #2 r3 <a href="../4">[4]</a> ticket:5.'
- >>> patterns = ['#[1-9][0-9]*', 'issue:[1-9][0-9]*', 'ticket:[1-9][0-9]*', 'bug:[1-9][0-9]*']
+ >>> patterns = ISSUE_RECOGNITION_PATTERNS
>>> linkBugs(text, ids, patterns)
'<a href="../1">issue:1</a> <a href="../2">#2</a> r3 [4] <a href="../5">ticket:5</a>.'
>>> linkBugs(text, [], patterns)
'issue:1 #2 r3 [4] ticket:5.'
>>> linkBugs("#9#9#9", ['9'], ["#9"])
'<a href="../9">#9</a><a href="../9">#9</a><a href="../9">#9</a>'
+ >>> linkBugs("foo#9 #9a #99 #9", ['9'], patterns)
+ 'foo#9 #9a #99 <a href="../9">#9</a>'
+ >>> linkBugs("noticket:5 ticket:5", ['5'], patterns)
+ 'noticket:5 <a href="../5">ticket:5</a>'
linkSvn
@@ -92,6 +99,7 @@ Replace patterns with links to changesets in a repository. It does
not need to be subversion of course. Specify something to test with::
>>> from Products.Poi.utils import linkSvn
+ >>> from Products.Poi.config import REVISION_RECOGNITION_PATTERNS
>>> text = "r1 #22 changeset:333 [4444]"
>>> svnUrl = "someurl?rev=%(rev)s"
@@ -103,7 +111,7 @@ And test it::
>>> patterns = ["r1"]
>>> linkSvn(text, svnUrl, patterns)
'<a href="someurl?rev=1">r1</a> #22 changeset:333 [4444]'
- >>> patterns = ['r[0-9]+', 'changeset:[0-9]+', '\[[0-9]+\]']
+ >>> patterns = REVISION_RECOGNITION_PATTERNS
>>> linkSvn(text, svnUrl, patterns)
'<a href="someurl?rev=1">r1</a> #22 <a href="someurl?rev=333">changeset:333</a> <a href="someurl?rev=4444">[4444]</a>'
@@ -111,3 +119,14 @@ Of course if you want to be silly, you can::
>>> linkSvn(text, "here", patterns)
'<a href="here">r1</a> #22 <a href="here">changeset:333</a> <a href="here">[4444]</a>'
+
+Test with multiple similar revisions. This is more a text of our
+regular expression skills::
+
+ >>> text = "r1 r2 norevisionr3 r4nope (r5) r6."
+ >>> linkSvn(text, svnUrl, patterns)
+ '<a href="someurl?rev=1">r1</a> <a href="someurl?rev=2">r2</a> norevisionr3 r4nope (<a href="someurl?rev=5">r5</a>) <a href="someurl?rev=6">r6</a>.'
+ >>> text = "[1] link[2] [3]."
+ >>> linkSvn(text, svnUrl, patterns)
+ '<a href="someurl?rev=1">[1]</a> link[2] <a href="someurl?rev=3">[3]</a>.'
+
View
5 Products/Poi/utils.py
@@ -1,5 +1,7 @@
import re
+from .config import ISSUE_LINK_TEMPLATE
+
def getNumberFromString(linktext):
"""
@@ -30,8 +32,7 @@ def linkBugs(text, ids, patterns):
bug = getNumberFromString(linktext)
if bug is not None and bug in ids:
- # XXX/TODO: this is a little too hardcoded for my taste
- link = '<a href="../' + bug + '">' + linktext + '</a>'
+ link = ISSUE_LINK_TEMPLATE % dict(bug=bug, linktext=linktext)
text = text[0:pos] + link + text[res.end():]
pos += len(link)
else:
Please sign in to comment.
Something went wrong with that request. Please try again.