Source

syntaxhighlighter / html.py

from highlighter import Format, Partition, PartitionScanner, Token, Scanner, SyntaxHighlighter

comment_fmt = Format("comment", color="#a0a0a0", italic=True)
tag_fmt     = Format("tag", color="blue", bold=True)
attr_fmt    = Format("attr", color="lightblue")
value_fmt   = Format("value", color="lightblue", italic=True)
text_fmt    = Format("text", color="black")

comment_part = Partition("comment", "<!--", "-->", is_multiline=True)
tag_part     = Partition("tag", "<", ">", is_multiline=True)

ps = PartitionScanner([comment_part, tag_part])

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


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