changeset 854:0dfb76b13115

server (blog): fixes: - fixed BlogMessage comments arguments when comments is None - added a couple of forgotten "u" for unicode strings - added a workaround for an issue with empty <div /> element. This workaround doesn't cover all cases
author Goffi <goffi@goffi.org>
date Fri, 22 Jan 2016 20:22:30 +0100
parents fe43cf3ed7d8
children de17f7313cbe
files src/server/blog.py
diffstat 1 files changed, 13 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/server/blog.py	Fri Jan 22 20:22:30 2016 +0100
+++ b/src/server/blog.py	Fri Jan 22 20:22:30 2016 +0100
@@ -38,8 +38,8 @@
 from libervia.server.html_tools import sanitizeHtml, convertNewLinesToXHTML
 from libervia.server.constants import Const as C
 
-
 PARAMS_TO_GET = (C.STATIC_BLOG_PARAM_TITLE, C.STATIC_BLOG_PARAM_BANNER, C.STATIC_BLOG_PARAM_KEYWORDS, C.STATIC_BLOG_PARAM_DESCRIPTION)
+re_strip_empty_div = re.compile(r"<div ?/>|<div> *?</div>")
 
 # TODO: chech disco features and use max_items when RSM is not available
 
@@ -73,6 +73,7 @@
     @param unquote_value(str): value to unquote
     @return (unicode): unquoted value
     """
+    assert not isinstance(quoted_value, unicode)
     return urllib.unquote(quoted_value).decode('utf-8')
 
 
@@ -572,6 +573,8 @@
         @param comments(list[dict]): list of microblog data
         @param comments_count (int): total number of comments
         """
+        if comments is None:
+            comments = []
         timestamp = float(entry.get('published', 0))
 
         # FIXME: for now we assume that the comments' depth is only 1
@@ -583,7 +586,7 @@
         self.content = self.getText(entry, 'content')
 
         if is_comment:
-            self.author = (_("from {}").format(entry['author']))
+            self.author = (_(u"from {}").format(entry['author']))
         else:
             self.author = '&nbsp;'
             self.url = "{}/{}".format(base_url, _quote(entry['id']))
@@ -593,14 +596,14 @@
             self.title = self.getText(entry, 'title')
             self.tags = [sanitizeHtml(tag) for tag in common.dict2iter('tag', entry)]
 
-            count_text = lambda count: D_('comments') if count > 1 else D_('comment')
+            count_text = lambda count: D_(u'comments') if count > 1 else D_(u'comment')
 
             self.comments_text = u"{} {}".format(comments_count, count_text(comments_count))
 
             delta = comments_count - len(comments)
             if request.display_single and delta > 0:
                 prev_url = "{}?{}".format(self.url, urllib.urlencode({'comments_max': comments_count}))
-                prev_text = D_("show {count} previous {comments}").format(
+                prev_text = D_(u"show {count} previous {comments}").format(
                     count = delta, comments = count_text(delta))
                 self.all_comments_link = BlogLink(prev_url, "comments_link", prev_text)
 
@@ -609,10 +612,15 @@
 
     def getText(self, entry, key):
         try:
-            return fixXHTMLLinks(entry['{}_xhtml'.format(key)])
+            xhtml = entry['{}_xhtml'.format(key)]
         except KeyError:
             try:
                 processor = addURLToText if key.startswith('content') else sanitizeHtml
                 return convertNewLinesToXHTML(processor(entry[key]))
             except KeyError:
                 return None
+        else:
+            # FIXME: empty <div /> elements provoke rendering issue
+            #        this regex is a temporary workadound, need more investigation
+            xhtml = re_strip_empty_div.sub("", xhtml)
+            return fixXHTMLLinks(xhtml)