comparison src/plugins/plugin_misc_text_syntaxes.py @ 832:c4b22aedb7d7

plugin groupblog, XEP-0071, XEP-0277, text_syntaxes: manage raw/rich/xhtml data for content/title: Implementation should follow the following formal specification: "title" and "content" data can be passed in raw, xhtml or rich format. When we receive from a frontend a new/updated microblog item: - keys "title" or "content" have to be escaped (disable HTML tags) - keys "title_rich" or "content_rich" have to be converted from the current syntax to XHTML - keys "title_xhtml" or "content_xhtml" have to be cleaned from unwanted XHTML content Rules to deal with concurrent keys: - existence of both "*_xhtml" and "*_rich" keys must raise an exception - existence of both raw and ("*_xhtml" or "*_rich") is OK As the storage always need raw data, if it is not given by the user it can be extracted from the "*_rich" or "*_xhtml" data (remove the XHTML tags). When a frontend wants to edit a blog post that contains XHTML title or content, the conversion is made from XHTML to the current user-defined syntax. - plugin text_syntaxes: added "text" syntax (using lxml)
author souliane <souliane@mailoo.org>
date Wed, 05 Feb 2014 16:36:51 +0100
parents 1fe00f0c9a91
children 2cc0201b4613
comparison
equal deleted inserted replaced
831:d7f9cd8a08cd 832:c4b22aedb7d7
24 from twisted.internet import defer 24 from twisted.internet import defer
25 from twisted.internet.threads import deferToThread 25 from twisted.internet.threads import deferToThread
26 from sat.core import exceptions 26 from sat.core import exceptions
27 from lxml import html 27 from lxml import html
28 from lxml.html import clean 28 from lxml.html import clean
29 from cgi import escape
29 import re 30 import re
30 31
31 32
32 CATEGORY = D_("Composition") 33 CATEGORY = D_("Composition")
33 NAME = "Syntax" 34 NAME = "Syntax"
68 OPT_DEFAULT = "DEFAULT" 69 OPT_DEFAULT = "DEFAULT"
69 OPT_HIDDEN = "HIDDEN" 70 OPT_HIDDEN = "HIDDEN"
70 OPT_NO_THREAD = "NO_THREAD" 71 OPT_NO_THREAD = "NO_THREAD"
71 SYNTAX_XHTML = _SYNTAX_XHTML 72 SYNTAX_XHTML = _SYNTAX_XHTML
72 SYNTAX_MARKDOWN = "markdown" 73 SYNTAX_MARKDOWN = "markdown"
74 SYNTAX_TEXT = "text"
73 75
74 params = """ 76 params = """
75 <params> 77 <params>
76 <individual> 78 <individual>
77 <category name="%(category_name)s" label="%(category_label)s"> 79 <category name="%(category_name)s" label="%(category_label)s">
97 info(_("Text syntaxes plugin initialization")) 99 info(_("Text syntaxes plugin initialization"))
98 self.host = host 100 self.host = host
99 self.syntaxes = {} 101 self.syntaxes = {}
100 self.addSyntax(self.SYNTAX_XHTML, lambda xhtml: defer.succeed(xhtml), lambda xhtml: defer.succeed(xhtml), 102 self.addSyntax(self.SYNTAX_XHTML, lambda xhtml: defer.succeed(xhtml), lambda xhtml: defer.succeed(xhtml),
101 TextSyntaxes.OPT_NO_THREAD) 103 TextSyntaxes.OPT_NO_THREAD)
104 self.addSyntax(self.SYNTAX_TEXT, lambda text: escape(text), lambda xhtml: self._removeMarkups(xhtml), [TextSyntaxes.OPT_HIDDEN])
102 try: 105 try:
103 import markdown, html2text 106 import markdown, html2text
104 self.addSyntax(self.SYNTAX_MARKDOWN, markdown.markdown, html2text.html2text, [TextSyntaxes.OPT_DEFAULT]) 107 self.addSyntax(self.SYNTAX_MARKDOWN, markdown.markdown, html2text.html2text, [TextSyntaxes.OPT_DEFAULT])
105 except ImportError: 108 except ImportError:
106 warning("markdown or html2text not found, can't use Markdown syntax") 109 warning("markdown or html2text not found, can't use Markdown syntax")
236 if TextSyntaxes.OPT_DEFAULT in flags: 239 if TextSyntaxes.OPT_DEFAULT in flags:
237 syntaxes = TextSyntaxes.params_data['default'] = name 240 syntaxes = TextSyntaxes.params_data['default'] = name
238 241
239 self._updateParamOptions() 242 self._updateParamOptions()
240 243
244 def _removeMarkups(self, xhtml):
245 """
246 Remove XHTML markups from the given string.
247 @param xhtml: the XHTML string to be cleaned
248 @return: the cleaned string
249 """
250 cleaner = clean.Cleaner(kill_tags=['style'])
251 cleaned = cleaner.clean_html(html.fromstring(xhtml))
252 return html.tostring(cleaned, method="text")