comparison frontends/src/jp/cmd_blog.py @ 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 dad500e7ae50
children 01877048c94a
comparison
equal deleted inserted replaced
2329:f15b428852a0 2330:74c1dbabeec8
36 import subprocess 36 import subprocess
37 from sat.tools.common import data_format 37 from sat.tools.common import data_format
38 38
39 __commands__ = ["Blog"] 39 __commands__ = ["Blog"]
40 40
41 SYNTAX_XHTML = u'xhtml'
41 # extensions to use with known syntaxes 42 # extensions to use with known syntaxes
42 SYNTAX_EXT = { 43 SYNTAX_EXT = {
43 '': 'txt', # used when the syntax is not found 44 '': 'txt', # used when the syntax is not found
44 "xhtml": "xhtml", 45 SYNTAX_XHTML: "xhtml",
45 "markdown": "md" 46 "markdown": "md"
46 } 47 }
47 48
48 49
49 CONF_SYNTAX_EXT = 'syntax_ext_dict' 50 CONF_SYNTAX_EXT = u'syntax_ext_dict'
50 BLOG_TMP_DIR = u"blog" 51 BLOG_TMP_DIR = u"blog"
51 # key to remove from metadata tmp file if they exist 52 # key to remove from metadata tmp file if they exist
52 KEY_TO_REMOVE_METADATA = ('id','content', 'content_xhtml', 'comments_node', 'comments_service', 'updated') 53 KEY_TO_REMOVE_METADATA = ('id','content', 'content_xhtml', 'comments_node', 'comments_service', 'updated')
53 54
54 URL_REDIRECT_PREFIX = 'url_redirect_' 55 URL_REDIRECT_PREFIX = 'url_redirect_'
365 def publish(self, content, mb_data): 366 def publish(self, content, mb_data):
366 if self.args.syntax is None: 367 if self.args.syntax is None:
367 # default syntax has been used 368 # default syntax has been used
368 mb_data['content_rich'] = content 369 mb_data['content_rich'] = content
369 else: 370 else:
370 mb_data['content_xhtml'] = self.host.bridge.syntaxConvert(content, self.current_syntax, 'XHTML', False, self.profile) 371 mb_data['content_xhtml'] = self.host.bridge.syntaxConvert(content, self.current_syntax, SYNTAX_XHTML, False, self.profile)
371 372
372 if self.pubsub_item is not None: 373 if self.pubsub_item is not None:
373 mb_data['id'] = self.pubsub_item 374 mb_data['id'] = self.pubsub_item
374 375
375 self.host.bridge.mbSend(self.pubsub_service, self.pubsub_node, mb_data, self.profile) 376 self.host.bridge.mbSend(self.pubsub_service, self.pubsub_node, mb_data, self.profile)
387 try: 388 try:
388 content = mb_data['content_xhtml'] 389 content = mb_data['content_xhtml']
389 except KeyError: 390 except KeyError:
390 content = mb_data['content'] 391 content = mb_data['content']
391 if content: 392 if content:
392 content = self.host.bridge.syntaxConvert(content, 'text', 'XHTML', False, self.profile) 393 content = self.host.bridge.syntaxConvert(content, 'text', SYNTAX_XHTML, False, self.profile)
393 if content and self.current_syntax != 'XHTML': 394 if content and self.current_syntax != SYNTAX_XHTML:
394 content = self.host.bridge.syntaxConvert(content, 'XHTML', self.current_syntax, False, self.profile) 395 content = self.host.bridge.syntaxConvert(content, SYNTAX_XHTML, self.current_syntax, False, self.profile)
396 if content and self.current_syntax == SYNTAX_XHTML:
397 try:
398 from lxml import etree
399 except ImportError:
400 self.disp(_(u"You need lxml to edit pretty XHTML"))
401 else:
402 parser = etree.XMLParser(remove_blank_text=True)
403 root = etree.fromstring(content, parser)
404 content = etree.tostring(root, encoding=unicode, pretty_print=True)
405
395 return content, mb_data, mb_data['id'] 406 return content, mb_data, mb_data['id']
396 407
397 def start(self): 408 def start(self):
398 # if there are user defined extension, we use them 409 # if there are user defined extension, we use them
399 SYNTAX_EXT.update(config.getConfig(self.sat_conf, 'jp', CONF_SYNTAX_EXT, {})) 410 SYNTAX_EXT.update(config.getConfig(self.sat_conf, 'jp', CONF_SYNTAX_EXT, {}))
443 self._launchPreviewExt(self.update_cb_cmd, "blog_preview_update_cmd") 454 self._launchPreviewExt(self.update_cb_cmd, "blog_preview_update_cmd")
444 455
445 def updateContent(self): 456 def updateContent(self):
446 with open(self.content_file_path, 'rb') as f: 457 with open(self.content_file_path, 'rb') as f:
447 content = f.read().decode('utf-8-sig') 458 content = f.read().decode('utf-8-sig')
448 if content and self.syntax != 'XHTML': 459 if content and self.syntax != SYNTAX_XHTML:
449 # we use safe=True because we want to have a preview as close as possible to what the 460 # we use safe=True because we want to have a preview as close as possible to what the
450 # people will see 461 # people will see
451 content = self.host.bridge.syntaxConvert(content, self.syntax, 'XHTML', True, self.profile) 462 content = self.host.bridge.syntaxConvert(content, self.syntax, SYNTAX_XHTML, True, self.profile)
452 463
453 xhtml = (u'<html xmlns="http://www.w3.org/1999/xhtml">' + 464 xhtml = (u'<html xmlns="http://www.w3.org/1999/xhtml">' +
454 u'<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>'+ 465 u'<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>'+
455 '<body>{}</body>' + 466 '<body>{}</body>' +
456 u'</html>').format(content) 467 u'</html>').format(content)