# 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

		# for tags
		charfmt1 = PyQt4.QtGui.QTextCharFormat ()
		charfmt1.setFontWeight (PyQt4.QtGui.QFont.Bold)
		charfmt1.setForeground (PyQt4.QtCore.Qt.blue)

		# for strings
		charfmt2 = PyQt4.QtGui.QTextCharFormat ()
		charfmt2.setForeground (PyQt4.QtCore.Qt.darkGreen)

		# for html entities
		charfmt3 = PyQt4.QtGui.QTextCharFormat ()
		charfmt3.setForeground (PyQt4.QtCore.Qt.magenta)

		# for template variables - ${temp_var}
		charfmt4 = PyQt4.QtGui.QTextCharFormat ()
		charfmt4.setForeground (PyQt4.QtCore.Qt.darkGray)
		charfmt4.setFontItalic (True)

		# matching regular expressions
		htmltagexps = [ (PyQt4.QtCore.QRegExp ("<[^<>]+>"), charfmt1),
						(PyQt4.QtCore.QRegExp ("\"[^\"]+\""), charfmt2),
						(PyQt4.QtCore.QRegExp ("&[^;]+;"), charfmt3),
						(PyQt4.QtCore.QRegExp ("\$\{[^\}]+\}"), charfmt4)
					]

		# 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)
