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 # for template variables - ${temp_var}
27 charfmt4
= PyQt4
.QtGui
.QTextCharFormat ()
28 charfmt4
.setForeground (PyQt4
.QtCore
.Qt
.darkGray
)
29 charfmt4
.setFontItalic (True)
31 # matching regular expressions
32 htmltagexps
= [ (PyQt4
.QtCore
.QRegExp ("<[^<>]+>"), charfmt1
),
33 (PyQt4
.QtCore
.QRegExp ("\"[^\"]+\""), charfmt2
),
34 (PyQt4
.QtCore
.QRegExp ("&[^;]+;"), charfmt3
),
35 (PyQt4
.QtCore
.QRegExp ("\$\{[^\}]+\}"), charfmt4
)
38 # run through the list of regular expressions to highlight
39 for htmltagexp
, charfmt
in htmltagexps
:
40 index
= htmltagexp
.indexIn (text
)
42 length
= htmltagexp
.matchedLength ()
43 # self.setFormat is actual code highlighting
44 self
.setFormat (index
, length
, charfmt
)
45 index
= htmltagexp
.indexIn (text
, index
+ length
)