comparison src/plugins/plugin_misc_text_syntaxes.py @ 1803:14a97a5fe1c0

plugin text syntaxes: a non blocking syntax callback can now return a unicode directly instead of a Deferred
author Goffi <goffi@goffi.org>
date Fri, 15 Jan 2016 17:13:59 +0100
parents d17772b0fe22
children 3c40fa0dcd7a
comparison
equal deleted inserted replaced
1802:fed95a6c56f8 1803:14a97a5fe1c0
187 return html.tostring(xhtml_elt, encoding=unicode, method='xml') 187 return html.tostring(xhtml_elt, encoding=unicode, method='xml')
188 188
189 return deferToThread(blocking_cleaning, xhtml) 189 return deferToThread(blocking_cleaning, xhtml)
190 190
191 def convert(self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True, profile=None): 191 def convert(self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True, profile=None):
192 """ Convert a text between two syntaxes 192 """Convert a text between two syntaxes
193
193 @param text: text to convert 194 @param text: text to convert
194 @param syntax_from: source syntax (e.g. "markdown") 195 @param syntax_from: source syntax (e.g. "markdown")
195 @param syntax_to: dest syntax (e.g.: "XHTML") 196 @param syntax_to: dest syntax (e.g.: "XHTML")
196 @param safe: clean resulting XHTML to avoid malicious code if True 197 @param safe: clean resulting XHTML to avoid malicious code if True
197 @param profile: needed only when syntax_from or syntax_to is set to _SYNTAX_CURRENT 198 @param profile: needed only when syntax_from or syntax_to is set to _SYNTAX_CURRENT
198 @return: converted text """ 199 @return(unicode): converted text
200 """
199 201
200 if syntax_from == _SYNTAX_CURRENT: 202 if syntax_from == _SYNTAX_CURRENT:
201 syntax_from = self.getCurrentSyntax(profile) 203 syntax_from = self.getCurrentSyntax(profile)
202 if syntax_to == _SYNTAX_CURRENT: 204 if syntax_to == _SYNTAX_CURRENT:
203 syntax_to = self.getCurrentSyntax(profile) 205 syntax_to = self.getCurrentSyntax(profile)
207 if syntax_to not in syntaxes: 209 if syntax_to not in syntaxes:
208 raise UnknownSyntax(syntax_to) 210 raise UnknownSyntax(syntax_to)
209 d = None 211 d = None
210 212
211 if TextSyntaxes.OPT_NO_THREAD in syntaxes[syntax_from]["flags"]: 213 if TextSyntaxes.OPT_NO_THREAD in syntaxes[syntax_from]["flags"]:
212 d = syntaxes[syntax_from]["to"](text) 214 d = defer.maybeDeferred(syntaxes[syntax_from]["to"], text)
213 else: 215 else:
214 d = deferToThread(syntaxes[syntax_from]["to"], text) 216 d = deferToThread(syntaxes[syntax_from]["to"], text)
215 217
216 #TODO: keep only body element and change it to a div here ? 218 #TODO: keep only body element and change it to a div here ?
217 219
226 # converters can add new lines that disturb the microblog change detection 228 # converters can add new lines that disturb the microblog change detection
227 d.addCallback(lambda text: text.rstrip()) 229 d.addCallback(lambda text: text.rstrip())
228 return d 230 return d
229 231
230 def addSyntax(self, name, to_xhtml_cb, from_xhtml_cb, flags = None): 232 def addSyntax(self, name, to_xhtml_cb, from_xhtml_cb, flags = None):
231 """ Add a new syntax to the manager 233 """Add a new syntax to the manager
234
232 @param name: unique name of the syntax 235 @param name: unique name of the syntax
233 @param to_xhtml_cb: callback to convert from syntax to XHTML 236 @param to_xhtml_cb: callback to convert from syntax to XHTML
234 @param from_xhtml_cb: callback to convert from XHTML to syntax 237 @param from_xhtml_cb: callback to convert from XHTML to syntax
235 @param flags: set of optional flags, can be: 238 @param flags: set of optional flags, can be:
236 TextSyntaxes.OPT_DEFAULT: use as the default syntax (replace former one) 239 TextSyntaxes.OPT_DEFAULT: use as the default syntax (replace former one)
237 TextSyntaxes.OPT_HIDDEN: do not show in parameters 240 TextSyntaxes.OPT_HIDDEN: do not show in parameters
238 TextSyntaxes.OPT_NO_THREAD: do not defer to thread when converting (the callback must then return a deferred) 241 TextSyntaxes.OPT_NO_THREAD: do not defer to thread when converting (the callback may then return a deferred)
239
240 """ 242 """
241 name = unicode(name) 243 name = unicode(name)
242 flags = flags or [] 244 flags = flags or []
243 if TextSyntaxes.OPT_HIDDEN in flags and TextSyntaxes.OPT_DEFAULT in flags: 245 if TextSyntaxes.OPT_HIDDEN in flags and TextSyntaxes.OPT_DEFAULT in flags:
244 raise ValueError("%s and %s are mutually exclusive" % (TextSyntaxes.OPT_HIDDEN, TextSyntaxes.OPT_DEFAULT)) 246 raise ValueError(u"{} and {} are mutually exclusive".format(TextSyntaxes.OPT_HIDDEN, TextSyntaxes.OPT_DEFAULT))
245 247
246 syntaxes = TextSyntaxes.params_data['syntaxes'] 248 syntaxes = TextSyntaxes.params_data['syntaxes']
247 syntaxes[name] = {"to": to_xhtml_cb, "from": from_xhtml_cb, "flags": flags} 249 syntaxes[name] = {"to": to_xhtml_cb, "from": from_xhtml_cb, "flags": flags}
248 if TextSyntaxes.OPT_DEFAULT in flags: 250 if TextSyntaxes.OPT_DEFAULT in flags:
249 syntaxes = TextSyntaxes.params_data['default'] = name 251 syntaxes = TextSyntaxes.params_data['default'] = name