jp (blog/edit): pretty format XHTML before editing it

This commit is contained in:
Goffi 2017-07-13 21:54:15 +02:00
commit bd6ac76b45

View file

@ -38,15 +38,16 @@ from sat.tools.common import data_format
__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 @@ class Edit(base.CommandBase, BlogCommon, common.BaseEdit):
# 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 @@ class Edit(base.CommandBase, BlogCommon, common.BaseEdit):
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 @@ class Preview(base.CommandBase, BlogCommon):
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>'+