pluging text syntaxes: various minor improvments:
- use tuple instead of list for STYLES_WHITELIST
- specify where to get python makdown and html2text
- renamed clean_xhtml to cleanXHTML for consistency
- check name for conflict in addSyntax
This commit is contained in:
parent
7a6fe618d9
commit
ee6688f800
1 changed files with 13 additions and 7 deletions
|
|
@ -39,7 +39,8 @@ _SYNTAX_XHTML = "XHTML"
|
|||
_SYNTAX_CURRENT = "@CURRENT@"
|
||||
|
||||
# TODO: check/adapt following list
|
||||
STYLES_WHITELIST = ["azimuth", "background-color", "border-bottom-color", "border-collapse", "border-color", "border-left-color", "border-right-color", "border-top-color", "clear", "color", "cursor", "direction", "display", "elevation", "float", "font", "font-family", "font-size", "font-style", "font-variant", "font-weight", "height", "letter-spacing", "line-height", "overflow", "pause", "pause-after", "pause-before", "pitch", "pitch-range", "richness", "speak", "speak-header", "speak-numeral", "speak-punctuation", "speech-rate", "stress", "text-align", "text-decoration", "text-indent", "unicode-bidi", "vertical-align", "voice-family", "volume", "white-space", "width"] # based on feedparser list (http://pythonhosted.org/feedparser/html-sanitization.html)
|
||||
# list initialy based on feedparser list (http://pythonhosted.org/feedparser/html-sanitization.html)
|
||||
STYLES_WHITELIST = ("azimuth", "background-color", "border-bottom-color", "border-collapse", "border-color", "border-left-color", "border-right-color", "border-top-color", "clear", "color", "cursor", "direction", "display", "elevation", "float", "font", "font-family", "font-size", "font-style", "font-variant", "font-weight", "height", "letter-spacing", "line-height", "overflow", "pause", "pause-after", "pause-before", "pitch", "pitch-range", "richness", "speak", "speak-header", "speak-numeral", "speak-punctuation", "speech-rate", "stress", "text-align", "text-decoration", "text-indent", "unicode-bidi", "vertical-align", "voice-family", "volume", "white-space", "width")
|
||||
|
||||
SAFE_ATTRS = html.defs.safe_attrs.union(('style',))
|
||||
STYLES_VALUES_REGEX = r'^(' + '|'.join(['([a-z-]+)', # alphabetical names
|
||||
|
|
@ -112,7 +113,8 @@ class TextSyntaxes(object):
|
|||
return h.handle(html)
|
||||
self.addSyntax(self.SYNTAX_MARKDOWN, markdown.markdown, _html2text, [TextSyntaxes.OPT_DEFAULT])
|
||||
except ImportError:
|
||||
log.warning("markdown or html2text not found, can't use Markdown syntax")
|
||||
log.warning(u"markdown or html2text not found, can't use Markdown syntax")
|
||||
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)
|
||||
|
||||
|
|
@ -125,7 +127,7 @@ class TextSyntaxes(object):
|
|||
if TextSyntaxes.OPT_HIDDEN not in flags:
|
||||
syntaxes.append(syntax)
|
||||
|
||||
syntaxes.sort(key=unicode.lower)
|
||||
syntaxes.sort(key=lambda synt: synt.lower())
|
||||
options = []
|
||||
|
||||
for syntax in syntaxes:
|
||||
|
|
@ -143,7 +145,7 @@ class TextSyntaxes(object):
|
|||
"""
|
||||
return self.host.memory.getParamA(NAME, CATEGORY , profile_key=profile)
|
||||
|
||||
def clean_xhtml(self, xhtml):
|
||||
def cleanXHTML(self, xhtml):
|
||||
""" Clean XHTML text by removing potentially dangerous/malicious parts
|
||||
@param xhtml: raw xhtml text to clean (or lxml's HtmlElement)
|
||||
"""
|
||||
|
|
@ -198,6 +200,9 @@ class TextSyntaxes(object):
|
|||
@param profile: needed only when syntax_from or syntax_to is set to _SYNTAX_CURRENT
|
||||
@return(unicode): converted text
|
||||
"""
|
||||
# FIXME: convert should be abled to handle domish.Element directly
|
||||
# when dealing with XHTML
|
||||
# TODO: a way for parser to return parsing errors/warnings
|
||||
|
||||
if syntax_from == _SYNTAX_CURRENT:
|
||||
syntax_from = self.getCurrentSyntax(profile)
|
||||
|
|
@ -218,7 +223,7 @@ class TextSyntaxes(object):
|
|||
#TODO: keep only body element and change it to a div here ?
|
||||
|
||||
if safe:
|
||||
d.addCallback(self.clean_xhtml)
|
||||
d.addCallback(self.cleanXHTML)
|
||||
|
||||
if TextSyntaxes.OPT_NO_THREAD in syntaxes[syntax_to]["flags"]:
|
||||
d.addCallback(syntaxes[syntax_to]["from"])
|
||||
|
|
@ -240,12 +245,13 @@ class TextSyntaxes(object):
|
|||
TextSyntaxes.OPT_HIDDEN: do not show in parameters
|
||||
TextSyntaxes.OPT_NO_THREAD: do not defer to thread when converting (the callback may then return a deferred)
|
||||
"""
|
||||
name = unicode(name)
|
||||
flags = flags or []
|
||||
flags = flags if flags is not None else []
|
||||
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.ConflitError(u"This syntax name already exists: {}".format(name))
|
||||
syntaxes[name] = {"to": to_xhtml_cb, "from": from_xhtml_cb, "flags": flags}
|
||||
if TextSyntaxes.OPT_DEFAULT in flags:
|
||||
syntaxes = TextSyntaxes.params_data['default'] = name
|
||||
|
|
|
|||
Loading…
Reference in a new issue