Source

syntaxhighlighter / html2.py

from highlighter import PartitionScanner, Token, Scanner, SyntaxHighlighter

formats = [
  ("comment", ("#a0a0a0", False, True)),
  ("tag", ("blue", True)),
  ("attr", ("lightblue")),
  ("value", dict(color="lightblue", italic=True)),
  ("text", ("black",))
]

parts = [
  ("comment", "<!--", "-->", True),
  ("tag", "<", ">", True)
]


tag_scanner = [
  ("attr", "[A-Za-z_]\w*"),
  ("equals", "="),
  ("value", '"[^"]*"')
]



if __name__ == "__main__":
	from PyQt4.QtGui import QApplication, QPlainTextEdit
	app = QApplication([])
	edit = QPlainTextEdit(None)
	sh = SyntaxHighlighter(edit.document(), parts, {"tag":tag_scanner}, formats)
	edit.resize(800, 600)
	edit.setPlainText('<!-- comment--> text <tag attr="value">')
	edit.show()
	app.exec_()