X-Git-Url: https://harishankar.org/repos/?p=biaweb_qt.git;a=blobdiff_plain;f=highlighter.py;fp=highlighter.py;h=95cfa90c09c83567a0eb671ad89ac95264feb993;hp=0000000000000000000000000000000000000000;hb=a17e899ab18f129ecc6e894e8b2ddc3322aef6f8;hpb=3b4711c41851510b83c2c442bd6a00ee387c1bfb diff --git a/highlighter.py b/highlighter.py new file mode 100644 index 0000000..95cfa90 --- /dev/null +++ b/highlighter.py @@ -0,0 +1,31 @@ +# BiaWeb Website content manager (c) 2010 V.Harishankar +# HTML highlighter class + +import PyQt4 + +class SimpleHtmlHighlighter (PyQt4.QtGui.QSyntaxHighlighter): + def __init__ (self, document): + PyQt4.QtGui.QSyntaxHighlighter.__init__ (self, document) + + def highlightBlock (self, text): + # define the character highlight formats + charfmt1 = PyQt4.QtGui.QTextCharFormat () + charfmt1.setFontWeight (PyQt4.QtGui.QFont.Bold) + charfmt1.setForeground (PyQt4.QtCore.Qt.blue) + + charfmt2 = PyQt4.QtGui.QTextCharFormat () + charfmt2.setForeground (PyQt4.QtCore.Qt.darkGreen) + + # matching regular expressions + htmltagexps = [ (PyQt4.QtCore.QRegExp ("<[^<>]+>"), charfmt1), + (PyQt4.QtCore.QRegExp ("\"[^\"]+\""), charfmt2) + ] + + # run through the list of regular expressions to highlight + for htmltagexp, charfmt in htmltagexps: + index = htmltagexp.indexIn (text) + while index >= 0: + length = htmltagexp.matchedLength () + # self.setFormat is actual code highlighting + self.setFormat (index, length, charfmt) + index = htmltagexp.indexIn (text, index + length)