plugin text syntaxes: a non blocking syntax callback can now return a unicode directly instead of a Deferred
This commit is contained in:
parent
8b825d4055
commit
9fe9a339f1
1 changed files with 9 additions and 7 deletions
|
|
@ -189,13 +189,15 @@ class TextSyntaxes(object):
|
|||
return deferToThread(blocking_cleaning, xhtml)
|
||||
|
||||
def convert(self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True, profile=None):
|
||||
""" Convert a text between two syntaxes
|
||||
"""Convert a text between two syntaxes
|
||||
|
||||
@param text: text to convert
|
||||
@param syntax_from: source syntax (e.g. "markdown")
|
||||
@param syntax_to: dest syntax (e.g.: "XHTML")
|
||||
@param safe: clean resulting XHTML to avoid malicious code if True
|
||||
@param profile: needed only when syntax_from or syntax_to is set to _SYNTAX_CURRENT
|
||||
@return: converted text """
|
||||
@return(unicode): converted text
|
||||
"""
|
||||
|
||||
if syntax_from == _SYNTAX_CURRENT:
|
||||
syntax_from = self.getCurrentSyntax(profile)
|
||||
|
|
@ -209,7 +211,7 @@ class TextSyntaxes(object):
|
|||
d = None
|
||||
|
||||
if TextSyntaxes.OPT_NO_THREAD in syntaxes[syntax_from]["flags"]:
|
||||
d = syntaxes[syntax_from]["to"](text)
|
||||
d = defer.maybeDeferred(syntaxes[syntax_from]["to"], text)
|
||||
else:
|
||||
d = deferToThread(syntaxes[syntax_from]["to"], text)
|
||||
|
||||
|
|
@ -228,20 +230,20 @@ class TextSyntaxes(object):
|
|||
return d
|
||||
|
||||
def addSyntax(self, name, to_xhtml_cb, from_xhtml_cb, flags = None):
|
||||
""" Add a new syntax to the manager
|
||||
"""Add a new syntax to the manager
|
||||
|
||||
@param name: unique name of the syntax
|
||||
@param to_xhtml_cb: callback to convert from syntax to XHTML
|
||||
@param from_xhtml_cb: callback to convert from XHTML to syntax
|
||||
@param flags: set of optional flags, can be:
|
||||
TextSyntaxes.OPT_DEFAULT: use as the default syntax (replace former one)
|
||||
TextSyntaxes.OPT_HIDDEN: do not show in parameters
|
||||
TextSyntaxes.OPT_NO_THREAD: do not defer to thread when converting (the callback must then return a deferred)
|
||||
|
||||
TextSyntaxes.OPT_NO_THREAD: do not defer to thread when converting (the callback may then return a deferred)
|
||||
"""
|
||||
name = unicode(name)
|
||||
flags = flags or []
|
||||
if TextSyntaxes.OPT_HIDDEN in flags and TextSyntaxes.OPT_DEFAULT in flags:
|
||||
raise ValueError("%s and %s are mutually exclusive" % (TextSyntaxes.OPT_HIDDEN, TextSyntaxes.OPT_DEFAULT))
|
||||
raise ValueError(u"{} and {} are mutually exclusive".format(TextSyntaxes.OPT_HIDDEN, TextSyntaxes.OPT_DEFAULT))
|
||||
|
||||
syntaxes = TextSyntaxes.params_data['syntaxes']
|
||||
syntaxes[name] = {"to": to_xhtml_cb, "from": from_xhtml_cb, "flags": flags}
|
||||
|
|
|
|||
Loading…
Reference in a new issue