3213755526947638d6f15ab25563914ba85e35cf
1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # HTML highlighter class
6 class SimpleHtmlHighlighter (PyQt4
.QtGui
.QSyntaxHighlighter
):
7 def __init__ (self
, document
):
8 PyQt4
.QtGui
.QSyntaxHighlighter
.__init
__ (self
, document
)
10 def highlightBlock (self
, text
):
11 # define the character highlight formats
14 charfmt1
= PyQt4
.QtGui
.QTextCharFormat ()
15 charfmt1
.setFontWeight (PyQt4
.QtGui
.QFont
.Bold
)
16 charfmt1
.setForeground (PyQt4
.QtCore
.Qt
.blue
)
19 charfmt2
= PyQt4
.QtGui
.QTextCharFormat ()
20 charfmt2
.setForeground (PyQt4
.QtCore
.Qt
.darkGreen
)
23 charfmt3
= PyQt4
.QtGui
.QTextCharFormat ()
24 charfmt3
.setForeground (PyQt4
.QtCore
.Qt
.magenta
)
26 # matching regular expressions
27 htmltagexps
= [ (PyQt4
.QtCore
.QRegExp ("<[^<>]+>"), charfmt1
),
28 (PyQt4
.QtCore
.QRegExp ("\"[^\"]+\""), charfmt2
),
29 (PyQt4
.QtCore
.QRegExp ("&[^;]+;"), charfmt3
)
32 # run through the list of regular expressions to highlight
33 for htmltagexp
, charfmt
in htmltagexps
:
34 index
= htmltagexp
.indexIn (text
)
36 length
= htmltagexp
.matchedLength ()
37 # self.setFormat is actual code highlighting
38 self
.setFormat (index
, length
, charfmt
)
39 index
= htmltagexp
.indexIn (text
, index
+ length
)