comparison src/plugins/plugin_misc_text_syntaxes.py @ 744:312a2842b2b8

plugins text-syntaxes: added a default value to use the current user syntax in convert
author souliane <souliane@mailoo.org>
date Mon, 09 Dec 2013 15:31:07 +0100
parents 6c8a119dcc94
children bfabeedbf32e
comparison
equal deleted inserted replaced
743:5a131930348d 744:312a2842b2b8
29 29
30 30
31 CATEGORY = "Composition" 31 CATEGORY = "Composition"
32 NAME = "Syntax" 32 NAME = "Syntax"
33 _SYNTAX_XHTML = "XHTML" 33 _SYNTAX_XHTML = "XHTML"
34 _SYNTAX_CURRENT = "@CURRENT@"
35
34 # TODO: check/adapt following list 36 # TODO: check/adapt following list
35 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) 37 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)
36 38
37 SAFE_ATTRS = html.defs.safe_attrs.union(('style',)) 39 SAFE_ATTRS = html.defs.safe_attrs.union(('style',))
38 STYLES_VALUES_REGEX = r'^(' + '|'.join(['([a-z-]+)', # alphabetical names 40 STYLES_VALUES_REGEX = r'^(' + '|'.join(['([a-z-]+)', # alphabetical names
99 try: 101 try:
100 import markdown, html2text 102 import markdown, html2text
101 self.addSyntax(self.SYNTAX_MARKDOWN, markdown.markdown, html2text.html2text, [TextSyntaxes.OPT_DEFAULT]) 103 self.addSyntax(self.SYNTAX_MARKDOWN, markdown.markdown, html2text.html2text, [TextSyntaxes.OPT_DEFAULT])
102 except ImportError: 104 except ImportError:
103 warning("markdown or html2text not found, can't use Markdown syntax") 105 warning("markdown or html2text not found, can't use Markdown syntax")
104 host.bridge.addMethod("syntaxConvert", ".plugin", in_sign='sssb', out_sign='s', 106 host.bridge.addMethod("syntaxConvert", ".plugin", in_sign='sssbs', out_sign='s',
105 async=True, method=self.convert) 107 async=True, method=self.convert)
106 108
107 def _updateParamOptions(self): 109 def _updateParamOptions(self):
108 data_synt = TextSyntaxes.params_data['syntaxes'] 110 data_synt = TextSyntaxes.params_data['syntaxes']
109 syntaxes = [] 111 syntaxes = []
173 elt.set("style", clean_style(elt.get('style'))) 175 elt.set("style", clean_style(elt.get('style')))
174 return html.tostring(xhtml_elt, method='xml') 176 return html.tostring(xhtml_elt, method='xml')
175 177
176 return deferToThread(blocking_cleaning, xhtml) 178 return deferToThread(blocking_cleaning, xhtml)
177 179
178 def convert(self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True): 180 def convert(self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True, profile=None):
179 """ Convert a text between two syntaxes 181 """ Convert a text between two syntaxes
180 @param text: text to convert 182 @param text: text to convert
181 @param syntax_from: source syntax (e.g. "markdown") 183 @param syntax_from: source syntax (e.g. "markdown")
182 @param syntax_to: dest syntax (e.g.: "XHTML") 184 @param syntax_to: dest syntax (e.g.: "XHTML")
183 @param safe: clean resulting XHTML to avoid malicious code if True 185 @param safe: clean resulting XHTML to avoid malicious code if True
186 @param profile: needed only when syntax_from or syntax_to is set to _SYNTAX_CURRENT
184 @return: converted text """ 187 @return: converted text """
185 188
189 if syntax_from == _SYNTAX_CURRENT:
190 syntax_from = self.getCurrentSyntax(profile)
191 if syntax_to == _SYNTAX_CURRENT:
192 syntax_to = self.getCurrentSyntax(profile)
186 syntaxes = TextSyntaxes.params_data['syntaxes'] 193 syntaxes = TextSyntaxes.params_data['syntaxes']
187 if syntax_from not in syntaxes: 194 if syntax_from not in syntaxes:
188 raise UnknownSyntax(syntax_from) 195 raise UnknownSyntax(syntax_from)
189 if syntax_to not in syntaxes: 196 if syntax_to not in syntaxes:
190 raise UnknownSyntax(syntax_to) 197 raise UnknownSyntax(syntax_to)