# HG changeset patch # User Mads Kiilerich # Date 1434729642 -7200 # Node ID 53f19cdfa40ca648859985f444f82f9b630642d7 # Parent d60f54b3eeb3d1c98a8b944b4bf0ab607a1fbf3f helpers: introduce urlify_stylize_text for correctly and safely stylizing and urlifying a string diff -r d60f54b3eeb3 -r 53f19cdfa40c kallithea/controllers/journal.py --- a/kallithea/controllers/journal.py Fri Jun 19 18:00:42 2015 +0200 +++ b/kallithea/controllers/journal.py Fri Jun 19 18:00:42 2015 +0200 @@ -239,10 +239,7 @@ def desc(desc): from pylons import tmpl_context as c - if c.visual.stylify_metatags: - return h.urlify_text(h.desc_stylize(h.truncate(desc, 60))) - else: - return h.urlify_text(h.truncate(desc, 60)) + return h.urlify_text(desc, truncate=60, stylize=c.visual.stylify_metatags) def repo_actions(repo_name): return _render('repo_actions', repo_name) diff -r d60f54b3eeb3 -r 53f19cdfa40c kallithea/lib/helpers.py --- a/kallithea/lib/helpers.py Fri Jun 19 18:00:42 2015 +0200 +++ b/kallithea/lib/helpers.py Fri Jun 19 18:00:42 2015 +0200 @@ -1273,10 +1273,15 @@ return '%(url)s' % ({'url': url_full}) return url_re.sub(url_func, s) -def urlify_text(s): +def urlify_text(s, truncate=None, stylize=False, truncatef=truncate): """ Extract urls from text and make literal html links out of them """ + if truncate is not None: + s = truncatef(s, truncate) + s = html_escape(s) + if stylize: + s = desc_stylize(s) s = _urlify_text(s) return literal(s) diff -r d60f54b3eeb3 -r 53f19cdfa40c kallithea/model/repo.py --- a/kallithea/model/repo.py Fri Jun 19 18:00:42 2015 +0200 +++ b/kallithea/model/repo.py Fri Jun 19 18:00:42 2015 +0200 @@ -209,10 +209,7 @@ cs_cache.get('message')) def desc(desc): - if c.visual.stylify_metatags: - return h.urlify_text(h.desc_stylize(h.html_escape(h.truncate(desc, 60)))) - else: - return h.urlify_text(h.html_escape(h.truncate(desc, 60))) + return h.urlify_text(desc, truncate=60, stylize=c.visual.stylify_metatags) def state(repo_state): return _render("repo_state", repo_state) diff -r d60f54b3eeb3 -r 53f19cdfa40c kallithea/templates/index_base.html --- a/kallithea/templates/index_base.html Fri Jun 19 18:00:42 2015 +0200 +++ b/kallithea/templates/index_base.html Fri Jun 19 18:00:42 2015 +0200 @@ -59,11 +59,7 @@ - %if c.visual.stylify_metatags: - ${h.urlify_text(h.desc_stylize(gr.group_description))} - %else: - ${gr.group_description} - %endif + ${h.urlify_text(gr.group_description, stylize=c.visual.stylify_metatags)} ## this is commented out since for multi nested repos can be HEAVY! ## in number of executed queries during traversing uncomment at will ##${gr.repositories_recursive_count} diff -r d60f54b3eeb3 -r 53f19cdfa40c kallithea/templates/summary/summary.html --- a/kallithea/templates/summary/summary.html Fri Jun 19 18:00:42 2015 +0200 +++ b/kallithea/templates/summary/summary.html Fri Jun 19 18:00:42 2015 +0200 @@ -84,11 +84,7 @@
- %if c.visual.stylify_metatags: -
${h.urlify_text(h.desc_stylize(h.html_escape(c.db_repo.description)))}
- %else: -
${h.urlify_text(h.html_escape(c.db_repo.description))}
- %endif +
${h.urlify_text(c.db_repo.description, stylize=c.visual.stylify_metatags)}
diff -r d60f54b3eeb3 -r 53f19cdfa40c kallithea/tests/other/test_libs.py --- a/kallithea/tests/other/test_libs.py Fri Jun 19 18:00:42 2015 +0200 +++ b/kallithea/tests/other/test_libs.py Fri Jun 19 18:00:42 2015 +0200 @@ -189,14 +189,14 @@ "[requires => url] [lang => python] [just a tag]" "[,d] [ => ULR ] [obsolete] [desc]]" ) - from kallithea.lib.helpers import desc_stylize, html_escape - res = desc_stylize(html_escape(sample)) - self.assertTrue('
tag
' in res) - self.assertTrue('
obsolete
' in res) - self.assertTrue('
stale
' in res) - self.assertTrue('
python
' in res) - self.assertTrue('
requires => url
' in res) - self.assertTrue('
tag
' in res) + from kallithea.lib.helpers import urlify_text + res = urlify_text(sample, stylize=True) + self.assertIn('
tag
', res) + self.assertIn('
obsolete
', res) + self.assertIn('
stale
', res) + self.assertIn('
python
', res) + self.assertIn('
requires => url
', res) + self.assertIn('
tag
', res) def test_alternative_gravatar(self): from kallithea.lib.helpers import gravatar_url