Add simple syntax highlighting

This commit is contained in:
Dan Holmsand 2014-01-02 17:45:31 +01:00
parent 3aa5f57c05
commit 1bb8f7e3e9
1 changed files with 20 additions and 1 deletions

View File

@ -33,8 +33,27 @@
(string/join "\n"
(map #(% defs) names))))
(def builtins ["def" "defn" "ns" "atom" "let" "if" "when"
"cond" "merge"])
(defn syntaxify [src]
(let [str-p "\"[^\"]*\""
keyw-p ":[^\\][(){} \\t\\n]+"
res-p (string/join "\\b|" builtins)
any-p ".|\\n"
patt (re-pattern (str "("
(string/join ")|(" [str-p keyw-p res-p any-p])
")"))]
(apply vector :pre
(for [[s str keyw res] (re-seq patt src)]
(cond
str [:span {:style {:color "green"}} str]
keyw [:span {:style {:color "blue"}} keyw]
res [:b res]
:else s)))))
(defn src-for [{:keys [defs]}]
[:pre (src-for-names defs)])
[:pre (syntaxify (src-for-names defs))])
(defn demo-component [props]
[:div.example