3213755526947638d6f15ab25563914ba85e35cf
[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
13 # for tags
14 charfmt1 = PyQt4.QtGui.QTextCharFormat ()
15 charfmt1.setFontWeight (PyQt4.QtGui.QFont.Bold)
16 charfmt1.setForeground (PyQt4.QtCore.Qt.blue)
17
18 # for strings
19 charfmt2 = PyQt4.QtGui.QTextCharFormat ()
20 charfmt2.setForeground (PyQt4.QtCore.Qt.darkGreen)
21
22 # for html entities
23 charfmt3 = PyQt4.QtGui.QTextCharFormat ()
24 charfmt3.setForeground (PyQt4.QtCore.Qt.magenta)
25
26 # matching regular expressions
27 htmltagexps = [ (PyQt4.QtCore.QRegExp ("<[^<>]+>"), charfmt1),
28 (PyQt4.QtCore.QRegExp ("\"[^\"]+\""), charfmt2),
29 (PyQt4.QtCore.QRegExp ("&[^;]+;"), charfmt3)
30 ]
31
32 # run through the list of regular expressions to highlight
33 for htmltagexp, charfmt in htmltagexps:
34 index = htmltagexp.indexIn (text)
35 while index >= 0:
36 length = htmltagexp.matchedLength ()
37 # self.setFormat is actual code highlighting
38 self.setFormat (index, length, charfmt)
39 index = htmltagexp.indexIn (text, index + length)