##// END OF EJS Templates
Étiquette Working snapshot ajoutée à la révision 483cfe27e044
Étiquette Working snapshot ajoutée à la révision 483cfe27e044

File last commit:

r0:1aa783210b8e default
r6:068bddcaf0c7 default
Show More
66 lines | 1.9 KiB | text/x-python | PythonLexer
# -*- coding: utf-8 -*-
""" python-part of PygmentsHighlighter
code/ideas taken from IPython, but IPython doesn't work with PythonQt...
"Melven Zoellner" <melven@topen.org>
# allow to import pygments-stuff from current path
import sys
from __main__ import PYMODULES
# Third-party imports
import pygments
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
# PythonQt imports
from PythonQt import QtGui
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._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()
char_format = QtGui.QTextCursor(self._document).charFormat()
# cache result
self._format_cache[token] = char_format
return char_format
# create class instance, can be called with highlightCode(code)
highlightCode = PygmentsHighlighter()