From 1bb8f7e3e99206f220fb3cfdf2ff8dbda23d4e18 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Thu, 2 Jan 2014 17:45:31 +0100 Subject: [PATCH] Add simple syntax highlighting --- test/demo.cljs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/demo.cljs b/test/demo.cljs index 30381c0..5e5f740 100644 --- a/test/demo.cljs +++ b/test/demo.cljs @@ -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