Source

syntaxhighlighter / create_python_hl.py

Full commit
# -*- coding: utf-8 -*-
from __future__ import print_function
from keyword import kwlist as python_keywords
python_builtins = [str(name) for name in dir(__builtins__) if not name.startswith('_') or name in ("__import__",)]



python_syntax = {
  "partitions": [
      # name, start, end, is_multiline
      ("comment", "#", "\n"),
      ("string", "[bruBRU]?'''", "'''", True),
      ("string", "[bruBRU]?'", "'"),
      ("string", '[bruBRU]?"""', '"""', True),
      ("string", '[bruBRU]?"', '"'),
  ],
  "scanner": {
      # parition-name (None=default): tokens
      None: [
         # (name, token-pattern-regexp, prefix, suffix)
         ("hexnumber", "(0x)([0-9a-fA-F])+?",),
         ("number", "\d+(\.\d*)?",),
         ("keyword", python_keywords, "(^|[\b\W])", "[\b\W]"),
         ("builtin", python_builtins, "(^|[^\.\w])", "[\b\W]"),
         ("ident", "[A-Za-z_][A-Za-z_0-9]*",)
      ]
  },
  "formats": {
     #name: value
     "keyword": "%(syntax_keyword)s",
     "builtin": "%(syntax_builtin)s",
     "comment": "%(syntax_comment)s",
     "string":  "%(syntax_string)s",
     "hexnumber":  "%(syntax_number)s",
     "number":  "%(syntax_number)s",
  }
}


import pprint
print("syntax = ", pprint.pformat(python_syntax))