def apply_filter(self, message, symbols={}, filter=None, ftag=None): def get_tr(message, prefix, filter): s = self.get_t(message, prefix) return filter(s) if filter else self.filter(s) if filter: prefix = '@' + (ftag or 'userdef') + '\x01' else: prefix = '@' + self.ftag + '\x01' message = get_from_cache( self.cache, prefix + message, lambda: get_tr(message, prefix, filter)) if symbols or symbols == 0 or symbols == "": if isinstance(symbols, dict): symbols.update( (key, xmlescape(value).translate(ttab_in)) for key, value in iteritems(symbols) if not isinstance(value, NUMBERS)) else: if not isinstance(symbols, tuple): symbols = (symbols,) symbols = tuple( value if isinstance(value, NUMBERS) else to_native(xmlescape(value)).translate(ttab_in) for value in symbols) message = self.params_substitution(message, symbols) return to_native(XML(message.translate(ttab_out)).xml())
def c_tokenizer( self, token, match, style, ): """ Callback for C specific highlighting. """ value = xmlescape(match.group(), quote=False) self.change_style(token, style) self.output.append(value)
def html_tokenizer( self, token, match, style, ): """ Callback for HTML specific highlighting. """ value = xmlescape(match.group(), quote=False) self.change_style(token, style) self.output.append(value) if token == 'GOTOPYTHON': return 'PYTHON' return None
def python_tokenizer( self, token, match, style, ): """ Callback for python specific highlighting. """ value = xmlescape(match.group(), quote=False) if token == 'MULTILINESTRING': self.change_style(token, style) self.output.append(value) self.strMultilineString = match.group(1) return 'PYTHONMultilineString' elif token == 'ENDMULTILINESTRING': if match.group(1) == self.strMultilineString: self.output.append(value) self.strMultilineString = '' return 'PYTHON' if style and style[:5] == 'link:': self.change_style(None, None) (url, style) = style[5:].split(';', 1) if url == 'None' or url == '': self.output.append('<span style="%s">%s</span>' % (style, value)) else: self.output.append('<a href="%s%s" style="%s">%s</a>' % (url, value, style, value)) else: self.change_style(token, style) self.output.append(value) if token == 'GOTOHTML': return 'HTML' return None
def highlight( code, language, link='/examples/globals/vars/', counter=1, styles=None, highlight_line=None, context_lines=None, attributes=None, ): styles = styles or {} attributes = attributes or {} code_style = styles.get('CODE', None) or ''' font-size: 11px; font-family: Bitstream Vera Sans Mono,monospace; background-color: transparent; margin: 0; padding: 5px; border: none; overflow: auto; white-space: pre !important; ''' linenumbers_style = styles.get('LINENUMBERS', None) or ''' font-size: 11px; font-family: Bitstream Vera Sans Mono,monospace; background-color: transparent; margin: 0; padding: 5px; border: none; color: #A0A0A0; ''' linehighlight_style = styles.get('LINEHIGHLIGHT', None) or \ 'background-color: #EBDDE2;' if language and language.upper() in [ 'PYTHON', 'C', 'CPP', 'HTML', 'WEB2PY' ]: code = Highlighter(language, link, styles).highlight(code) else: code = xmlescape(code, quote=False) lines = code.split('\n') if counter is None: linenumbers = [''] * len(lines) elif isinstance(counter, str): linenumbers = [xmlescape(counter, quote=False)] * len(lines) else: linenumbers = [str(i + counter) + '.' for i in xrange(len(lines))] if highlight_line: if counter and not isinstance(counter, str): lineno = highlight_line - counter else: lineno = highlight_line if lineno < len(lines): lines[lineno] = '<span style="%s">%s</span>' % ( linehighlight_style, lines[lineno]) linenumbers[lineno] = '<span style="%s">%s</span>' % ( linehighlight_style, linenumbers[lineno]) if context_lines: if lineno + context_lines < len(lines): delslice = slice(lineno + context_lines + 1, len(lines)) del lines[delslice] del linenumbers[delslice] if lineno - context_lines > 0: delslice = slice(0, lineno - context_lines) del lines[delslice] del linenumbers[delslice] code = '<br/>'.join(lines) numbers = '<br/>'.join(linenumbers) items = attributes.items() fa = ' '.join([ key[1:].lower() for (key, value) in items if key[:1] == '_' and value is None ] + [ '%s="%s"' % (key[1:].lower(), str(value).replace('"', "'")) for (key, value) in items if key[:1] == '_' and value ]) if fa: fa = ' ' + fa return '<table%s><tr style="vertical-align:top;">' \ '<td style="min-width:40px; text-align: right;"><pre style="%s">%s</pre></td>' \ '<td><pre style="%s">%s</pre></td></tr></table>' % (fa, linenumbers_style, numbers, code_style, code)
def highlight( code, language, link='/examples/globals/vars/', counter=1, styles=None, highlight_line=None, context_lines=None, attributes=None, ): styles = styles or {} attributes = attributes or {} code_style = styles.get('CODE', None) or ''' font-size: 11px; font-family: Bitstream Vera Sans Mono,monospace; background-color: transparent; margin: 0; padding: 5px; border: none; overflow: auto; white-space: pre !important; ''' linenumbers_style = styles.get('LINENUMBERS', None) or ''' font-size: 11px; font-family: Bitstream Vera Sans Mono,monospace; background-color: transparent; margin: 0; padding: 5px; border: none; color: #A0A0A0; ''' linehighlight_style = styles.get('LINEHIGHLIGHT', None) or \ 'background-color: #EBDDE2;' if language and language.upper() in ['PYTHON', 'C', 'CPP', 'HTML', 'WEB2PY']: code = Highlighter(language, link, styles).highlight(code) else: code = xmlescape(code, quote=False) lines = code.split('\n') if counter is None: linenumbers = [''] * len(lines) elif isinstance(counter, str): linenumbers = [xmlescape(counter, quote=False)] * len(lines) else: linenumbers = [str(i + counter) + '.' for i in xrange(len(lines))] if highlight_line: if counter and not isinstance(counter, str): lineno = highlight_line - counter else: lineno = highlight_line if lineno < len(lines): lines[lineno] = '<span style="%s">%s</span>' % ( linehighlight_style, lines[lineno]) linenumbers[lineno] = '<span style="%s">%s</span>' % ( linehighlight_style, linenumbers[lineno]) if context_lines: if lineno + context_lines < len(lines): delslice = slice(lineno + context_lines + 1, len(lines)) del lines[delslice] del linenumbers[delslice] if lineno - context_lines > 0: delslice = slice(0, lineno - context_lines) del lines[delslice] del linenumbers[delslice] code = '<br/>'.join(lines) numbers = '<br/>'.join(linenumbers) items = attributes.items() fa = ' '.join([key[1:].lower() for (key, value) in items if key[:1] == '_' and value is None] + ['%s="%s"' % (key[1:].lower(), str(value).replace('"', "'")) for (key, value) in items if key[:1] == '_' and value]) if fa: fa = ' ' + fa return '<table%s><tr style="vertical-align:top;">' \ '<td style="min-width:40px; text-align: right;"><pre style="%s">%s</pre></td>' \ '<td><pre style="%s">%s</pre></td></tr></table>' % (fa, linenumbers_style, numbers, code_style, code)
def xml(self): return str(self) if self.M else xmlescape(str(self), quote=False)