def _format_lines(self, tokensource): buf = [] for ttype, value in apply_filters(tokensource, [StripDocTestFilter()]): if ttype in Token.Sql: for t, v in HtmlFormatter._format_lines(self, iter(buf)): yield t, v buf = [] if ttype is Token.Sql: yield 1, "<div class='show_sql'>%s</div>" % \ re.sub( r'(?:{stop}|\n+)$', '', filters.html_escape(value)) elif ttype is Token.Sql.Link: yield 1, "<a href='#' class='sql_link'>sql</a>" elif ttype is Token.Sql.Popup: yield 1, "<div class='popup_sql'>%s</div>" % \ re.sub( r'(?:{stop}|\n+)$', '', filters.html_escape(value)) else: buf.append((ttype, value)) for t, v in _strip_trailing_whitespace( HtmlFormatter._format_lines(self, iter(buf))): yield t, v
class PygmentsHighlighter(object): """ highlight python code with a QSyntaxHighlighter, callable class (e.g. function with a state) to """ def __init__(self): """ constructor """ self._lexer = PythonLexer() self._formatter = HtmlFormatter() self._document = QtGui.QTextDocument() self._document.setDefaultStyleSheet(self._formatter.get_style_defs()) self._format_cache = dict() def __call__(self, code): """ makes this class callable, actually do the highlightning """ index = 0 for token, text in self._lexer.get_tokens(code): length = len(text) char_format = self._get_format(token) pygmentsHighlighter._setFormat(index, length, char_format) index += length def _get_format(self, token): """ get the QTextCharFormat for a token """ if token in self._format_cache: return self._format_cache[token] # get format from document code, html = self._formatter._format_lines([(token, u'dummy')]).next() self._document.setHtml(html) char_format = QtGui.QTextCursor(self._document).charFormat() # cache result self._format_cache[token] = char_format return char_format
class PygmentsHighlighter(object): """ highlight python code with a QSyntaxHighlighter, callable class (e.g. function with a state) to """ def __init__(self): """ constructor """ self._lexer = PythonLexer() self._formatter = HtmlFormatter() self._document = QtGui.QTextDocument() self._document.setDefaultStyleSheet(self._formatter.get_style_defs()) self._format_cache = dict() def __call__(self, code): """ makes this class callable, actually do the highlightning """ index = 0 for token, text in self._lexer.get_tokens(code): length = len(text) char_format = self._get_format(token) pygmentsHighlighter._setFormat(index, length, char_format) index += length def _get_format(self, token): """ get the QTextCharFormat for a token """ if token in self._format_cache: return self._format_cache[token] # get format from document code, html = next(self._formatter._format_lines([(token, u"dummy")])) self._document.setHtml(html) char_format = QtGui.QTextCursor(self._document).charFormat() # cache result self._format_cache[token] = char_format return char_format
def _format_lines(self, tokensource): # resolve a bug that on oneliners we get a newline at the end that looks # kinda ugly... tuples = list(HtmlFormatter._format_lines(self, tokensource)) last_tuple = tuples[-1] last_tuple = [(last_tuple[0], last_tuple[1].rstrip('\n'))] tuples = tuples[:-1] + last_tuple for tuple in tuples: yield tuple
def _format_lines(self, tokensource): buf = [] for ttype, value in apply_filters(tokensource, [StripDocTestFilter()]): if ttype in Token.Sql: for t, v in HtmlFormatter._format_lines(self, iter(buf)): yield t, v buf = [] if ttype is Token.Sql: yield 1, "<div class='show_sql'>%s</div>" % re.sub(r"(?:[{stop}|\n]*)$", "", value) elif ttype is Token.Sql.Link: yield 1, "<a href='#' class='sql_link'>sql</a>" elif ttype is Token.Sql.Popup: yield 1, "<div class='popup_sql'>%s</div>" % re.sub(r"(?:[{stop}|\n]*)$", "", value) else: buf.append((ttype, value)) for t, v in _strip_trailing_whitespace(HtmlFormatter._format_lines(self, iter(buf))): yield t, v
def _format_lines(self, tokensource): sql_lexer = SqlLexer() formatter = HtmlFormatter(nowrap=True) buf = [] for ttype, value in apply_filters(tokensource, [StripDocTestFilter()]): if ttype in Token.Sql: for t, v in HtmlFormatter._format_lines(self, iter(buf)): yield t, v buf = [] if ttype is Token.Sql: yield ( 1, "<div class='show_sql'>%s</div>" % pygments.highlight( re.sub(r"(?:{stop}|\n+)\s*$", "", value), sql_lexer, formatter, ), ) elif ttype is Token.Sql.Link: yield 1, "<a href='#' class='sql_link'>sql</a>" elif ttype is Token.Sql.Popup: yield ( 1, "<div class='popup_sql'>%s</div>" % pygments.highlight( re.sub(r"(?:{stop}|\n+)$", "", value), sql_lexer, formatter, ), ) else: buf.append((ttype, value)) for t, v in _strip_trailing_whitespace( HtmlFormatter._format_lines(self, iter(buf)) ): yield t, v
def _format_lines(self, tokensource): for tag, line in HtmlFormatter._format_lines(self, tokensource): if tag == 1: # sourcecode line line = '<span class=line>%s</span>' % line yield tag, line
def _format_lines(self, tokensource): for tag, line in HtmlFormatter._format_lines(self, tokensource): if tag == 1: # sourcecode line line = '<span class=line>%s</span>' % line yield tag, line