Code highlighting implemented
[biaweb_qt.git] / highlighter.py
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # HTML highlighter class
3
4 import PyQt4
5
6 class SimpleHtmlHighlighter (PyQt4.QtGui.QSyntaxHighlighter):
7 def __init__ (self, document):
8 PyQt4.QtGui.QSyntaxHighlighter.__init__ (self, document)
9
10 def highlightBlock (self, text):
11 # define the character highlight formats
12 charfmt1 = PyQt4.QtGui.QTextCharFormat ()
13 charfmt1.setFontWeight (PyQt4.QtGui.QFont.Bold)
14 charfmt1.setForeground (PyQt4.QtCore.Qt.blue)
15
16 charfmt2 = PyQt4.QtGui.QTextCharFormat ()
17 charfmt2.setForeground (PyQt4.QtCore.Qt.darkGreen)
18
19 # matching regular expressions
20 htmltagexps = [ (PyQt4.QtCore.QRegExp ("<[^<>]+>"), charfmt1),
21 (PyQt4.QtCore.QRegExp ("\"[^\"]+\""), charfmt2)
22 ]
23
24 # run through the list of regular expressions to highlight
25 for htmltagexp, charfmt in htmltagexps:
26 index = htmltagexp.indexIn (text)
27 while index >= 0:
28 length = htmltagexp.matchedLength ()
29 # self.setFormat is actual code highlighting
30 self.setFormat (index, length, charfmt)
31 index = htmltagexp.indexIn (text, index + length)