Mercurial > libervia-backend
changeset 2324:fe922e6fabd4
plugin text syntaxes: various improvments:
- syntaxes and default_syntax are now class attribute instead of members of params_data
- removed local exception UnknownSyntax in favor of exceptions.NotFound
- syntaxes are now saved with a key which is name in lower case and stripped, unmodified name is put in syntax's data
- new getSyntax (syntaxGet in bridge) method to get syntax key and raise an error if not found. Can be used by frontends to check that a syntax is known.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 13 Jul 2017 20:53:51 +0200 |
parents | 2dae79990122 |
children | 760df3a58c24 |
files | src/plugins/plugin_misc_text_syntaxes.py |
diffstat | 1 files changed, 32 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/src/plugins/plugin_misc_text_syntaxes.py Wed Jul 12 19:06:28 2017 +0200 +++ b/src/plugins/plugin_misc_text_syntaxes.py Thu Jul 13 20:53:51 2017 +0200 @@ -63,8 +63,6 @@ C.PI_DESCRIPTION: _("""Management of various text syntaxes (XHTML-IM, Markdown, etc)""") } -class UnknownSyntax(Exception): - pass class TextSyntaxes(object): """ Text conversion class @@ -77,6 +75,8 @@ SYNTAX_XHTML = _SYNTAX_XHTML SYNTAX_MARKDOWN = "markdown" SYNTAX_TEXT = "text" + syntaxes = {} + default_syntax = SYNTAX_XHTML params = """ <params> @@ -95,13 +95,12 @@ 'category_label': _(CATEGORY), 'name': NAME, 'label': _(NAME), - 'syntaxes': {}, + 'syntaxes': syntaxes, } def __init__(self, host): log.info(_("Text syntaxes plugin initialization")) self.host = host - self.syntaxes = {} self.addSyntax(self.SYNTAX_XHTML, lambda xhtml: defer.succeed(xhtml), lambda xhtml: defer.succeed(xhtml), TextSyntaxes.OPT_NO_THREAD) # TODO: text => XHTML should add <a/> to url like in frontends @@ -120,10 +119,12 @@ log.info(u"You can download/install them from https://pythonhosted.org/Markdown/ and https://github.com/Alir3z4/html2text/") host.bridge.addMethod("syntaxConvert", ".plugin", in_sign='sssbs', out_sign='s', async=True, method=self.convert) + host.bridge.addMethod("syntaxGet", ".plugin", in_sign='s', out_sign='s', + method=self.getSyntax) def _updateParamOptions(self): - data_synt = TextSyntaxes.params_data['syntaxes'] - default_synt = TextSyntaxes.params_data.get('default', _SYNTAX_XHTML) + data_synt = TextSyntaxes.syntaxes + default_synt = TextSyntaxes.default_syntax syntaxes = [] for syntax in data_synt.keys(): @@ -216,13 +217,17 @@ if syntax_from == _SYNTAX_CURRENT: syntax_from = self.getCurrentSyntax(profile) + else: + syntax_from = syntax_from.lower().strip() if syntax_to == _SYNTAX_CURRENT: syntax_to = self.getCurrentSyntax(profile) - syntaxes = TextSyntaxes.params_data['syntaxes'] + else: + syntax_to = syntax_to.lower().strip() + syntaxes = TextSyntaxes.syntaxes if syntax_from not in syntaxes: - raise UnknownSyntax(syntax_from) + raise exceptions.NotFound(syntax_from) if syntax_to not in syntaxes: - raise UnknownSyntax(syntax_to) + raise exceptions.NotFound(syntax_to) d = None if TextSyntaxes.OPT_NO_THREAD in syntaxes[syntax_from]["flags"]: @@ -259,18 +264,29 @@ if TextSyntaxes.OPT_HIDDEN in flags and TextSyntaxes.OPT_DEFAULT in flags: raise ValueError(u"{} and {} are mutually exclusive".format(TextSyntaxes.OPT_HIDDEN, TextSyntaxes.OPT_DEFAULT)) - syntaxes = TextSyntaxes.params_data['syntaxes'] - if name in syntaxes: - raise exceptions.ConflictError(u"This syntax name already exists: {}".format(name)) - syntaxes[name] = {"to": to_xhtml_cb, "from": from_xhtml_cb, "flags": flags} + syntaxes = TextSyntaxes.syntaxes + key = name.lower().strip() + if key in syntaxes: + raise exceptions.ConflictError(u"This syntax key already exists: {}".format(key)) + syntaxes[key] = {"name": name, "to": to_xhtml_cb, "from": from_xhtml_cb, "flags": flags} if TextSyntaxes.OPT_DEFAULT in flags: - TextSyntaxes.params_data['default'] = name + TextSyntaxes.default_syntaxe = key self._updateParamOptions() - def _removeMarkups(self, xhtml): + def getSyntax(self, name): + """get syntax key corresponding to a name + + @raise exceptions.NotFound: syntax doesn't exist """ - Remove XHTML markups from the given string. + key = name.lower().strip() + if key in self.syntaxes: + return key + raise exceptions.NotFound + + def _removeMarkups(self, xhtml): + """Remove XHTML markups from the given string. + @param xhtml: the XHTML string to be cleaned @return: the cleaned string """