# HG changeset patch # User Mads Kiilerich # Date 1473115878 -7200 # Node ID c3892e3a6ba33e147ae3db15a5757b3b5ea6e84d # Parent c759acdc463a5c3f41cf36610cbd02e1a9d6725f helpers: merge urlify_commit into urlify_text More reuse - they are already almost doing the same. test_urlify_test is updated to use the new repo_name parameter and urlify hashes. diff -r c759acdc463a -r c3892e3a6ba3 kallithea/lib/helpers.py --- 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 @@ -1279,16 +1279,26 @@ """ return url_re.sub(_urlify_text_replace, s) -def urlify_text(s, truncate=None, stylize=False, truncatef=truncate): + +def urlify_text(s, repo_name=None, link_=None, truncate=None, stylize=False, truncatef=truncate): """ - Extract urls from text and make literal html links out of them + Parses given text message and make literal html with markup. + The text will be truncated to the specified length. + Hashes are turned into changeset links to specified repository. + URLs links to what they say. + Issues are linked to given issue-server. + If link_ is provided, all text not already linking somewhere will link there. """ if truncate is not None: s = truncatef(s, truncate, whole_word=True) s = html_escape(s) + if repo_name is not None: + s = urlify_changesets(s, repo_name) if stylize: s = desc_stylize(s) s = _urlify_text(s) + if repo_name is not None: + s = urlify_issues(s, repo_name, link_) return literal(s) @@ -1328,24 +1338,6 @@ return ''.join(links) -def urlify_commit(text_, repo_name, link_=None): - """ - Parses given text message and makes proper links. - Issues are linked to given issue-server. If link_ is provided, all other - text will link there. - """ - newtext = html_escape(text_) - - # urlify changesets - extract revisions and make link out of them - newtext = urlify_changesets(newtext, repo_name) - - # extract http/https links and make them real urls - newtext = _urlify_text(newtext) - - newtext = urlify_issues(newtext, repo_name, link_) - - return literal(newtext) - def _urlify_issues_replace_f(repo_name, ISSUE_SERVER_LNK, ISSUE_PREFIX): def urlify_issues_replace(match_obj): diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/admin/gists/show.html --- a/kallithea/templates/admin/gists/show.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/admin/gists/show.html Tue Sep 06 00:51:18 2016 +0200 @@ -70,7 +70,7 @@ ${h.gravatar_div(h.email_or_none(c.file_changeset.author), size=16)}
${h.person(c.file_changeset.author)} - ${_('created')} ${h.age(c.file_changeset.date)}
-
${h.urlify_commit(c.file_changeset.message,c.repo_name)}
+
${h.urlify_text(c.file_changeset.message,c.repo_name)}
diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/changelog/changelog.html --- a/kallithea/templates/changelog/changelog.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/changelog/changelog.html Tue Sep 06 00:51:18 2016 +0200 @@ -116,7 +116,7 @@
-
${h.urlify_commit(cs.message, c.repo_name,h.url('changeset_home',repo_name=c.repo_name,revision=cs.raw_id))}
+
${h.urlify_text(cs.message, c.repo_name,h.url('changeset_home',repo_name=c.repo_name,revision=cs.raw_id))}
%if c.comments.get(cs.raw_id):
diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/changelog/changelog_summary_data.html --- a/kallithea/templates/changelog/changelog_summary_data.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/changelog/changelog_summary_data.html Tue Sep 06 00:51:18 2016 +0200 @@ -45,7 +45,7 @@ ${h.show_id(cs)} - ${h.urlify_commit(h.chop_at(cs.message,'\n'),c.repo_name, h.url('changeset_home',repo_name=c.repo_name,revision=cs.raw_id))} + ${h.urlify_text(h.chop_at(cs.message,'\n'),c.repo_name, h.url('changeset_home',repo_name=c.repo_name,revision=cs.raw_id))} ${h.age(cs.date)} diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/changeset/changeset.html --- a/kallithea/templates/changeset/changeset.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/changeset/changeset.html Tue Sep 06 00:51:18 2016 +0200 @@ -156,7 +156,7 @@
% endif -
${h.urlify_commit(c.changeset.message, c.repo_name)}
+
${h.urlify_text(c.changeset.message, c.repo_name)}
diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/changeset/changeset_range.html --- a/kallithea/templates/changeset/changeset_range.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/changeset/changeset_range.html Tue Sep 06 00:51:18 2016 +0200 @@ -50,7 +50,7 @@
%endif -
${h.urlify_commit(h.wrap_paragraphs(cs.message),c.repo_name)}
+
${h.urlify_text(h.wrap_paragraphs(cs.message),c.repo_name)}
%endfor diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/compare/compare_cs.html --- a/kallithea/templates/compare/compare_cs.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/compare/compare_cs.html Tue Sep 06 00:51:18 2016 +0200 @@ -73,7 +73,7 @@
%endfor -
${h.urlify_commit(cs.message, c.repo_name)}
+
${h.urlify_text(cs.message, c.repo_name)}
%endfor diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/files/files_source.html --- a/kallithea/templates/files/files_source.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/files/files_source.html Tue Sep 06 00:51:18 2016 +0200 @@ -51,7 +51,7 @@ ${h.gravatar_div(h.email_or_none(c.changeset.author), size=16)}
${h.person(c.changeset.author)}
-
${h.urlify_commit(c.changeset.message,c.repo_name)}
+
${h.urlify_text(c.changeset.message,c.repo_name)}
%if c.file.is_browser_compatible_image(): diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/pullrequests/pullrequest_show.html --- a/kallithea/templates/pullrequests/pullrequest_show.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/pullrequests/pullrequest_show.html Tue Sep 06 00:51:18 2016 +0200 @@ -43,7 +43,7 @@ %endif
-
${h.urlify_commit(c.pull_request.description, c.pull_request.org_repo.repo_name)}
+
${h.urlify_text(c.pull_request.description, c.pull_request.org_repo.repo_name)}
@@ -205,7 +205,7 @@ %endfor -
${h.urlify_commit(cs.message, c.repo_name)}
+
${h.urlify_text(cs.message, c.repo_name)}
%endif diff -r c759acdc463a -r c3892e3a6ba3 kallithea/templates/search/search_commit.html --- a/kallithea/templates/search/search_commit.html Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/templates/search/search_commit.html Tue Sep 06 00:51:18 2016 +0200 @@ -21,7 +21,7 @@
${h.literal(sr['message_hl'])}
%else: -
${h.urlify_commit(sr['message'], sr['repository'])}
+
${h.urlify_text(sr['message'], sr['repository'])}
%endif diff -r c759acdc463a -r c3892e3a6ba3 kallithea/tests/other/test_libs.py --- 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 @@ -351,7 +351,7 @@ """@mention @someone""", ""), ("deadbeefcafe 123412341234", - """deadbeefcafe 123412341234""", + """deadbeefcafe 123412341234""", ""), # tags are covered by test_tag_extractor ]) @@ -359,7 +359,19 @@ from kallithea.lib.helpers import urlify_text expected = self._quick_url(expected, tmpl="""%s""", url_=url_) - assert urlify_text(sample, stylize=True) == expected + assert urlify_text(sample, 'repo_name', stylize=True) == expected + + @parametrize('sample,expected', [ + ("deadbeefcafe @mention, and http://foo.bar/ yo", + """""" + """deadbeefcafe""" + """ @mention, and """ + """http://foo.bar/""" + """ yo"""), + ]) + def test_urlify_link(self, sample, expected): + from kallithea.lib.helpers import urlify_text + assert urlify_text(sample, 'repo_name', link_='#the-link') == expected @parametrize('test,expected', [ ("", None),