Reformatted code base using black.
This commit is contained in:
parent
56319cbad3
commit
53fdaa9d09
1 changed files with 12604 additions and 8966 deletions
|
|
@ -115,16 +115,80 @@ VOID_ELEMENTS = (
|
|||
"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 @@ PLUGIN_INFO = {
|
|||
|
||||
|
||||
class TextSyntaxes(object):
|
||||
""" Text conversion class
|
||||
"""Text conversion class
|
||||
XHTML utf-8 is used as intermediate language for conversions
|
||||
"""
|
||||
|
||||
|
|
@ -170,7 +234,6 @@ class TextSyntaxes(object):
|
|||
# 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 @@ class TextSyntaxes(object):
|
|||
# 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 @@ class TextSyntaxes(object):
|
|||
|
||||
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 @@ class TextSyntaxes(object):
|
|||
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 @@ class TextSyntaxes(object):
|
|||
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 @@ class TextSyntaxes(object):
|
|||
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 @@ class TextSyntaxes(object):
|
|||
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 @@ class TextSyntaxes(object):
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue