changeset 6187:923db690b56f

helpers: inline changeset hash markup in urlify_text
author Mads Kiilerich <madski@unity3d.com>
date Tue, 06 Sep 2016 00:51:18 +0200
parents 24632b87a263
children f486d1d27025
files kallithea/lib/helpers.py kallithea/tests/other/test_libs.py
diffstat 2 files changed, 12 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Tue Sep 06 00:51:18 2016 +0200
+++ b/kallithea/lib/helpers.py	Tue Sep 06 00:51:18 2016 +0200
@@ -1250,6 +1250,10 @@
 (?P<url>%s) |
 # @mention markup
 (?P<mention>%s) |
+# Changeset hash markup
+(?<!\w|[-_])
+  (?P<hash>[0-9a-f]{12,40})
+(?!\w|[-_]) |
 # "Stylize" markup
 \[see\ \=&gt;\ *(?P<seen>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] |
 \[license\ \=&gt;\ *(?P<license>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] |
@@ -1278,6 +1282,13 @@
         mention = match_obj.group('mention')
         if mention is not None:
             return '<b>%s</b>' % mention
+        hash_ = match_obj.group('hash')
+        if hash_ is not None and repo_name is not None:
+            from pylons import url  # doh, we need to re-import url to mock it later
+            return '<a class="revision-link" href="%(url)s">%(hash)s</a>' % {
+                 'url': url('changeset_home', repo_name=repo_name, revision=hash_),
+                 'hash': hash_,
+                }
         if stylize:
             seen = match_obj.group('seen')
             if seen:
@@ -1308,8 +1319,6 @@
     else:
         s = truncatef(s, truncate, whole_word=True)
     s = html_escape(s)
-    if repo_name is not None:
-        s = urlify_changesets(s, repo_name)
     s = _urlify(s)
     if repo_name is not None:
         s = urlify_issues(s, repo_name, link_)
@@ -1317,27 +1326,6 @@
     return literal(s)
 
 
-def _urlify_changeset_replace_f(repo_name):
-    from pylons import url  # doh, we need to re-import url to mock it later
-    def urlify_changeset_replace(match_obj):
-        rev = match_obj.group(0)
-        return '<a class="revision-link" href="%(url)s">%(rev)s</a>' % {
-         'url': url('changeset_home', repo_name=repo_name, revision=rev),
-         'rev': rev,
-        }
-    return urlify_changeset_replace
-
-
-urilify_changeset_re = r'(?:^|(?<=[\s(),]))([0-9a-fA-F]{12,40})(?=$|\s|[.,:()])'
-
-def urlify_changesets(text_, repo_name):
-    """
-    Extract revision ids from changeset and make link from them
-    """
-    urlify_changeset_replace = _urlify_changeset_replace_f(repo_name)
-    return re.sub(urilify_changeset_re, urlify_changeset_replace, text_)
-
-
 def linkify_others(t, l):
     """Add a default link to html with links.
     HTML doesn't allow nesting of links, so the outer link must be broken up
--- a/kallithea/tests/other/test_libs.py	Tue Sep 06 00:51:18 2016 +0200
+++ b/kallithea/tests/other/test_libs.py	Tue Sep 06 00:51:18 2016 +0200
@@ -313,7 +313,7 @@
        """       some text url[123123123123]<br/>"""
        """       sometimes !"""),
     ])
-    def test_urlify_changesets(self, sample, expected):
+    def test_urlify_text(self, sample, expected):
         def fake_url(self, *args, **kwargs):
             return '/some-url'