changeset 2330:74c1dbabeec8

jp (blog/edit): pretty format XHTML before editing it
author Goffi <goffi@goffi.org>
date Thu, 13 Jul 2017 21:54:15 +0200
parents f15b428852a0
children 01877048c94a
files frontends/src/jp/cmd_blog.py
diffstat 1 files changed, 19 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_blog.py	Thu Jul 13 21:45:26 2017 +0200
+++ b/frontends/src/jp/cmd_blog.py	Thu Jul 13 21:54:15 2017 +0200
@@ -38,15 +38,16 @@
 
 __commands__ = ["Blog"]
 
+SYNTAX_XHTML = u'xhtml'
 # extensions to use with known syntaxes
 SYNTAX_EXT = {
     '': 'txt', # used when the syntax is not found
-    "xhtml": "xhtml",
+    SYNTAX_XHTML: "xhtml",
     "markdown": "md"
     }
 
 
-CONF_SYNTAX_EXT = 'syntax_ext_dict'
+CONF_SYNTAX_EXT = u'syntax_ext_dict'
 BLOG_TMP_DIR = u"blog"
 # key to remove from metadata tmp file if they exist
 KEY_TO_REMOVE_METADATA = ('id','content', 'content_xhtml', 'comments_node', 'comments_service', 'updated')
@@ -367,7 +368,7 @@
             # default syntax has been used
             mb_data['content_rich'] =  content
         else:
-            mb_data['content_xhtml'] = self.host.bridge.syntaxConvert(content, self.current_syntax, 'XHTML', False, self.profile)
+            mb_data['content_xhtml'] = self.host.bridge.syntaxConvert(content, self.current_syntax, SYNTAX_XHTML, False, self.profile)
 
         if self.pubsub_item is not None:
             mb_data['id'] = self.pubsub_item
@@ -389,9 +390,19 @@
         except KeyError:
             content = mb_data['content']
             if content:
-                content = self.host.bridge.syntaxConvert(content, 'text', 'XHTML', False, self.profile)
-        if content and self.current_syntax != 'XHTML':
-            content = self.host.bridge.syntaxConvert(content, 'XHTML', self.current_syntax, False, self.profile)
+                content = self.host.bridge.syntaxConvert(content, 'text', SYNTAX_XHTML, False, self.profile)
+        if content and self.current_syntax != SYNTAX_XHTML:
+            content = self.host.bridge.syntaxConvert(content, SYNTAX_XHTML, self.current_syntax, False, self.profile)
+        if content and self.current_syntax == SYNTAX_XHTML:
+            try:
+                from lxml import etree
+            except ImportError:
+                self.disp(_(u"You need lxml to edit pretty XHTML"))
+            else:
+                parser = etree.XMLParser(remove_blank_text=True)
+                root = etree.fromstring(content, parser)
+                content = etree.tostring(root, encoding=unicode, pretty_print=True)
+
         return content, mb_data, mb_data['id']
 
     def start(self):
@@ -445,10 +456,10 @@
     def updateContent(self):
         with open(self.content_file_path, 'rb') as f:
             content = f.read().decode('utf-8-sig')
-            if content and self.syntax != 'XHTML':
+            if content and self.syntax != SYNTAX_XHTML:
                 # we use safe=True because we want to have a preview as close as possible to what the
                 # people will see
-                content = self.host.bridge.syntaxConvert(content, self.syntax, 'XHTML', True, self.profile)
+                content = self.host.bridge.syntaxConvert(content, self.syntax, SYNTAX_XHTML, True, self.profile)
 
         xhtml = (u'<html xmlns="http://www.w3.org/1999/xhtml">' +
                  u'<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>'+