Improve syntax highlighting a little

This commit is contained in:
Dan Holmsand 2014-01-05 14:47:06 +01:00
parent 62322c474f
commit 5031284e81
1 changed files with 48 additions and 18 deletions

View File

@ -33,27 +33,57 @@
(string/join "\n" (string/join "\n"
(map #(% defs) names)))) (map #(% defs) names))))
(def builtins ["def" "defn" "ns" "atom" "let" "if" "when" (def builtins #{"def" "defn" "ns" "atom" "let" "if" "when"
"cond" "merge" "assoc" "swap!" "reset!" "for" "cond" "merge" "assoc" "swap!" "reset!" "for"
"range" "nil\\?" "int" "or" "->" "%"]) "range" "nil?" "int" "or" "->" "%" "fn"})
(defn tokenize [src]
(let [ws " \\t\\n"
open "[({"
close ")\\]}"
str-p "\"[^\"]*\""
sep (str ws open close)
open-p (str "[" open "]")
close-p (str "[" close "]")
iden-p (str "[^" sep "]+")
any-p ".|\\n"
patt (re-pattern (str "("
(string/join ")|(" [str-p open-p close-p
iden-p any-p])
")"))
keyw-re #"^:"]
(for [[s str-litt open close iden any] (re-seq patt src)]
(cond
str-litt [:str-litt s]
open [:open s]
close [:close s]
iden (cond
(re-find keyw-re s) [:keyw s]
(builtins s) [:builtin s]
:else [:iden s])
any [:other s]))))
(defn syntaxify [src] (defn syntaxify [src]
;; quick and (very) dirty syntax coloring (let [def-re #"^def|^ns\b"]
(let [sep "\\][(){} \\t\\n" (loop [tokens (tokenize src)
str-p "\"[^\"]*\"" prev nil
keyw-p (str ":[^" sep "]+") res []]
res-p (string/join "|" (map #(str "\\b" % "(?=[" sep "])") builtins)) (let [[kind val] (first tokens)
any-p (str "[^" sep "]+|.|\\n") part (case kind
patt (re-pattern (str "(" :str-litt [:span {:style {:color "green"}} val]
(string/join ")|(" [str-p keyw-p res-p any-p]) :keyw [:span {:style {:color "blue"}} val]
")"))] :builtin [:span {:style {:font-weight "bold"}} val]
(apply vector :pre :iden (if (and prev (re-find def-re prev))
(for [[s str keyw res] (re-seq patt src)] [:span {:style {:color "#55c"
(cond :font-weight "bold"}} val]
str [:span {:style {:color "green"}} str] val)
keyw [:span {:style {:color "blue"}} keyw] val)
res [:b res] remain (rest tokens)]
:else s))))) (if-not (empty? remain)
(recur remain
(if (= kind :other) prev val)
(conj res part))
(apply vector :pre res))))))
(defn src-for [defs] (defn src-for [defs]
[:pre (syntaxify (src-for-names defs))]) [:pre (syntaxify (src-for-names defs))])