本文整理汇总了Python中pygments.formatters.HtmlFormatter._format_lines方法的典型用法代码示例。如果您正苦于以下问题:Python HtmlFormatter._format_lines方法的具体用法?Python HtmlFormatter._format_lines怎么用?Python HtmlFormatter._format_lines使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.formatters.HtmlFormatter
的用法示例。
在下文中一共展示了HtmlFormatter._format_lines方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PygmentsHighlighter
# 需要导入模块: from pygments.formatters import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.HtmlFormatter import _format_lines [as 别名]
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
示例2: _format_lines
# 需要导入模块: from pygments.formatters import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.HtmlFormatter import _format_lines [as 别名]
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
示例3: _format_lines
# 需要导入模块: from pygments.formatters import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.HtmlFormatter import _format_lines [as 别名]
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
示例4: _format_lines
# 需要导入模块: from pygments.formatters import HtmlFormatter [as 别名]
# 或者: from pygments.formatters.HtmlFormatter import _format_lines [as 别名]
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