changeset 5195:53f19cdfa40c

helpers: introduce urlify_stylize_text for correctly and safely stylizing and urlifying a string
author Mads Kiilerich <madski@unity3d.com>
date Fri, 19 Jun 2015 18:00:42 +0200
parents d60f54b3eeb3
children 273860c8fd85
files kallithea/controllers/journal.py kallithea/lib/helpers.py kallithea/model/repo.py kallithea/templates/index_base.html kallithea/templates/summary/summary.html kallithea/tests/other/test_libs.py
diffstat 6 files changed, 18 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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 '<a href="%(url)s">%(url)s</a>' % ({'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)
 
--- 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)
--- 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 @@
                               </a>
                             </div>
                         </td>
-                        %if c.visual.stylify_metatags:
-                            <td>${h.urlify_text(h.desc_stylize(gr.group_description))}</td>
-                        %else:
-                            <td>${gr.group_description}</td>
-                        %endif
+                        <td>${h.urlify_text(gr.group_description, stylize=c.visual.stylify_metatags)}</td>
                         ## this is commented out since for multi nested repos can be HEAVY!
                         ## in number of executed queries during traversing uncomment at will
                         ##<td><b>${gr.repositories_recursive_count}</b></td>
--- 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 @@
               <div class="label-summary">
                   <label>${_('Description')}:</label>
               </div>
-                 %if c.visual.stylify_metatags:
-                   <div class="input ${summary(c.show_stats)} desc">${h.urlify_text(h.desc_stylize(h.html_escape(c.db_repo.description)))}</div>
-                 %else:
-                   <div class="input ${summary(c.show_stats)} desc">${h.urlify_text(h.html_escape(c.db_repo.description))}</div>
-                 %endif
+              <div class="input ${summary(c.show_stats)} desc">${h.urlify_text(c.db_repo.description, stylize=c.visual.stylify_metatags)}</div>
             </div>
 
             <div class="field">
--- 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('<div class="metatag" tag="tag">tag</div>' in res)
-        self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res)
-        self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res)
-        self.assertTrue('<div class="metatag" tag="lang">python</div>' in res)
-        self.assertTrue('<div class="metatag" tag="requires">requires =&gt; <a href="/url">url</a></div>' in res)
-        self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
+        from kallithea.lib.helpers import urlify_text
+        res = urlify_text(sample, stylize=True)
+        self.assertIn('<div class="metatag" tag="tag">tag</div>', res)
+        self.assertIn('<div class="metatag" tag="obsolete">obsolete</div>', res)
+        self.assertIn('<div class="metatag" tag="stale">stale</div>', res)
+        self.assertIn('<div class="metatag" tag="lang">python</div>', res)
+        self.assertIn('<div class="metatag" tag="requires">requires =&gt; <a href="/url">url</a></div>', res)
+        self.assertIn('<div class="metatag" tag="tag">tag</div>', res)
 
     def test_alternative_gravatar(self):
         from kallithea.lib.helpers import gravatar_url