Mercurial > libervia-backend
diff libervia/backend/plugins/plugin_misc_text_syntaxes.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | ba28ca268f4a |
children | 45662662a432 |
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_misc_text_syntaxes.py Tue Jun 18 12:06:45 2024 +0200 +++ b/libervia/backend/plugins/plugin_misc_text_syntaxes.py Wed Jun 19 18:44:57 2024 +0200 @@ -115,16 +115,80 @@ "param", "source", "track", - "wbr") + "wbr", +) SAFE_ATTRS = html.defs.safe_attrs.union({"style", "poster", "controls"}) - {"id"} SAFE_CLASSES = { # those classes are used for code highlighting - "bp", "c", "ch", "cm", "cp", "cpf", "cs", "dl", "err", "fm", "gd", "ge", "get", "gh", - "gi", "go", "gp", "gr", "gs", "gt", "gu", "highlight", "hll", "il", "k", "kc", "kd", - "kn", "kp", "kr", "kt", "m", "mb", "mf", "mh", "mi", "mo", "na", "nb", "nc", "nd", - "ne", "nf", "ni", "nl", "nn", "no", "nt", "nv", "o", "ow", "s", "sa", "sb", "sc", - "sd", "se", "sh", "si", "sr", "ss", "sx", "vc", "vg", "vi", "vm", "w", "write", + "bp", + "c", + "ch", + "cm", + "cp", + "cpf", + "cs", + "dl", + "err", + "fm", + "gd", + "ge", + "get", + "gh", + "gi", + "go", + "gp", + "gr", + "gs", + "gt", + "gu", + "highlight", + "hll", + "il", + "k", + "kc", + "kd", + "kn", + "kp", + "kr", + "kt", + "m", + "mb", + "mf", + "mh", + "mi", + "mo", + "na", + "nb", + "nc", + "nd", + "ne", + "nf", + "ni", + "nl", + "nn", + "no", + "nt", + "nv", + "o", + "ow", + "s", + "sa", + "sb", + "sc", + "sd", + "se", + "sh", + "si", + "sr", + "ss", + "sx", + "vc", + "vg", + "vi", + "vm", + "w", + "write", } STYLES_VALUES_REGEX = ( r"^(" @@ -157,7 +221,7 @@ class TextSyntaxes(object): - """ Text conversion class + """Text conversion class XHTML utf-8 is used as intermediate language for conversions """ @@ -170,7 +234,6 @@ # default_syntax must be lower case default_syntax = SYNTAX_XHTML - def __init__(self, host): log.info(_("Text syntaxes plugin initialization")) self.host = host @@ -218,8 +281,8 @@ # when the user is not aware of markdown and HTML class EscapeHTML(Extension): def extendMarkdown(self, md): - md.preprocessors.deregister('html_block') - md.inlinePatterns.deregister('html') + md.preprocessors.deregister("html_block") + md.inlinePatterns.deregister("html") def _html2text(html, baseurl=""): h = html2text.HTML2Text(baseurl=baseurl) @@ -228,20 +291,22 @@ self.add_syntax( self.SYNTAX_MARKDOWN, - partial(markdown.markdown, - extensions=[ - EscapeHTML(), - 'nl2br', - 'codehilite', - 'fenced_code', - 'sane_lists', - 'tables', - ], - extension_configs = { - "codehilite": { - "css_class": "highlight", - } - }), + partial( + markdown.markdown, + extensions=[ + EscapeHTML(), + "nl2br", + "codehilite", + "fenced_code", + "sane_lists", + "tables", + ], + extension_configs={ + "codehilite": { + "css_class": "highlight", + } + }, + ), _html2text, [TextSyntaxes.OPT_DEFAULT], ) @@ -287,7 +352,7 @@ self.host.memory.update_params(self.params % self.params_data) def get_current_syntax(self, profile): - """ Return the selected syntax for the given profile + """Return the selected syntax for the given profile @param profile: %(doc_profile)s @return: profile selected syntax @@ -301,7 +366,7 @@ return failure def clean_style(self, styles_raw: str) -> str: - """"Clean unsafe CSS styles + """ "Clean unsafe CSS styles Remove styles not in the whitelist, or where the value doesn't match the regex @param styles_raw: CSS styles @@ -323,9 +388,7 @@ if value == "none": continue cleaned_styles.append((key, value)) - return "; ".join( - ["%s: %s" % (key_, value_) for key_, value_ in cleaned_styles] - ) + return "; ".join(["%s: %s" % (key_, value_) for key_, value_ in cleaned_styles]) def clean_classes(self, classes_raw: str) -> str: """Remove any non whitelisted class @@ -355,9 +418,7 @@ else: log.error("Only strings and HtmlElements can be cleaned") raise exceptions.DataError - cleaner = clean.Cleaner( - style=False, add_nofollow=False, safe_attrs=SAFE_ATTRS - ) + cleaner = clean.Cleaner(style=False, add_nofollow=False, safe_attrs=SAFE_ATTRS) xhtml_elt = cleaner.clean_html(xhtml_elt) for elt in xhtml_elt.xpath("//*[@style]"): elt.set("style", self.clean_style(elt.get("style"))) @@ -369,11 +430,12 @@ if element.tag in VOID_ELEMENTS: element.text = None else: - element.text = '' + element.text = "" return html.tostring(xhtml_elt, encoding=str, method="xml") - def convert(self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True, - profile=None): + def convert( + self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True, profile=None + ): """Convert a text between two syntaxes @param text: text to convert