Commits

Henning Schröder committed bb535ce

new demo

  • Participants
  • Parent commits 346dc64

Comments (0)

Files changed (3)

File doc/index.rst

 accepted by the scanner. By keeping track of the previous state it is
 possible to quickly retokenize only small parts of a text.
 
+The code is highly optimized for speed in order to prevent blocking
+the GUI.
+
 
 Contents:
 

File highlighter.py

         self.scanner = scanner
 
         self.formats = {}
-        for fname, f in formats:
+        for f in formats:
+            if isinstance(f, tuple):
+                fname, f = f
+            else:
+                assert isinstance(f, Format)
             if isinstance(f, basestring):
                 f = (f,) # only color specified
             if isinstance(f, (tuple,list)):
         formats.append((fname,fstyle))
 
     return partition_scanner, scanners, formats
+
+
+__all__ = [
+  "Format", "Partition", "PartitionScanner", "Token", "Scanner", "SynxtaxHighlighter", "load_syntax"
+]
+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_()