[lisppaste-requests] Python colorization for lisppaste

Ignas Mikalajunas ignas.mikalajunas at gmail.com
Mon Jun 5 13:28:30 UTC 2006


I am using colorize in my blogging application and have added python
colorization support for it, it would be nice to have it in lisppaste
too, so here you go:

(defvar *python-reserver-words*
  '("and"       "assert"        "break"         "class"         "continue"
    "def"       "del"           "elif"          "else"          "except"
    "exec"      "finally"       "for"           "from"          "global"
    "if"        "import"        "in"            "is"            "lambda"
    "not"       "or"            "pass"          "print"         "raise"
    "return"    "try"           "while"         "yield"))

(define-coloring-type :python "Python"
    :modes (:def :decorator)
    :parent :basic-c
    :transitions
    ((:normal
      ((scan #\#)
       (set-mode :comment
                 :until (scan-any '(#\return #\newline))))
      ((scan "\"\"\"")
       (set-mode :string
                 :until (scan "\"\"\"")))
      ((scan "'''")
       (set-mode :string
                 :until (scan "'''")))
      ((scan #\')
       (set-mode :string
                 :until (scan #\')))
      ((scan "@")
       (set-mode :decorator
                 :until (scan-any *non-constituent*)
                 :advancing nil))
      ((scan "def")
       (set-mode :def
                 :until (scan-any '(#\: #\())
                 :advancing nil))
      ((scan "class")
       (set-mode :def
                 :until (scan-any '(#\: #\())
                 :advancing nil))))
  :formatters
  ((:def
       (lambda (type s)
         (declare (ignore type))
         (format nil "<span class=\"special\">~A</span><span
class=\"keyword\">~A</span>"
                 (subseq s 0 (position #\Space s))
                 (subseq s (position #\Space s)))))
   (:decorator
    (lambda (type s)
      (declare (ignore type))
      (format nil "<span class=\"symbol\">~A</span>" s)))
   (:word-ish
    (lambda (type s)
      (declare (ignore type))
      (if (member s *python-reserver-words* :test #'string=)
          (format nil "<span class=\"symbol\">~A</span>"
                  s)
          s)))))

--
Ignas Mikalajūnas


More information about the lisppaste-requests mailing list