Commits

Henning Schröder committed b9d441e

added html descriptive demo

  • Participants
  • Parent commits bb535ce

Comments (0)

Files changed (2)

File highlighter.py

         for i, p in enumerate(partitions):
             if isinstance(p, (tuple,list)):
                 p = Partition(*p)
+            elif isinstance(p, dict):
+                p = Partitition(**p)
+            else:
+                assert isinstance(p, Partition), "Partition expected, got %r" % p
             self.partitions.append(p)
             start_groups.append("(?P<g%s_%s>%s)" % (i, p.name, p.start))
         start_pat = "|".join(start_groups)
             
         """
         QSyntaxHighlighter.__init__(self, parent)
-        parent.setDefaultFont(default_font)
+        if default_font:
+            parent.setDefaultFont(default_font)
+        
+        if isinstance(partition_scanner, (list,tuple)):
+            partition_scanner = PartitionScanner(partition_scanner)
+        else:
+            assert isinstance(partition_scanner, PartitionScanner), "PartitionScanner expected, got %r" % partition_scanner
         self.partition_scanner = partition_scanner
+        
         self.scanner = scanner
+        for inside_part, inside_scanner in scanner.items():
+            if isinstance(inside_scanner, (list,tuple)):
+                inside_scanner = Scanner(inside_scanner)
+                self.scanner[inside_part] = inside_scanner
+            else:
+                assert isinstance(inside_scanner, Scanner), "Scanner expected, got %r" % f
+
 
         self.formats = {}
         for f in formats:
             if isinstance(f, tuple):
                 fname, f = f
             else:
-                assert isinstance(f, Format)
+                assert isinstance(f, (Format,dict)), "Format expected, got %r" % f
             if isinstance(f, basestring):
                 f = (f,) # only color specified
             if isinstance(f, (tuple,list)):
             elif isinstance(f, dict):
                 f = Format(**dict(name=fname, **f))
             else:
-                assert isinstance(f, Format), "Format expected, %r found" % f
+                assert isinstance(f, Format), "Format expected, got %r" % f
             f.tcf.setFontFamily(parent.defaultFont().family())
             self.formats[f.name] = f.tcf
             
 
+        # reduce name look-ups for better speed
         scan_inside = {}
         for inside_part, inside_scanner in self.scanner.items():
             scan_inside[inside_part] = inside_scanner.scan
-        # reduce name look-ups for better speed
         self.get_scanner = scan_inside.get
         self.scan_partitions = partition_scanner.scan
         self.get_format = self.formats.get
+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_()