Created
October 15, 2014 15:07
-
-
Save aaronj1335/ebc3385edb693313c06c to your computer and use it in GitHub Desktop.
because @vtho always wants us to over engineer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns rdp.core | |
(:require [om.core :as om :include-macros true] | |
[om.dom :as dom :include-macros true] | |
[cljs.core :refer [clj->js]] | |
)) | |
(defonce app-state (atom {:templates ["something {x.y.(\"foo\")} something else" | |
"a {b.c.(\"d } e\")} f {c.g.(\"h\")}" | |
":{ }:"]})) | |
(defn children [parent s] | |
(loop [so-far [] | |
temp-accum "" | |
s s] | |
(case (first s) | |
nil (if (empty? temp-accum) | |
[so-far s] | |
[(conj so-far {:type :text :value temp-accum}) s]) | |
"{" (let [new-node {:type :block} | |
rest-of-s (apply str (rest s)) | |
[grand-children remaining] (children new-node rest-of-s) | |
new-node-with-children (assoc new-node :children grand-children) | |
so-far-with-text (if (seq temp-accum) | |
(conj so-far {:type :text :value temp-accum}) | |
so-far)] | |
(recur (conj so-far-with-text new-node-with-children) "" remaining)) | |
"}" (if (= (:type parent) :block) | |
(let [so-far-with-text (if (seq temp-accum) | |
(conj so-far {:type :text :value temp-accum}) | |
so-far)] | |
[so-far-with-text (apply str (rest s))]) | |
(recur so-far (str temp-accum "}") (apply str (rest s)))) | |
"\"" (if (= (:type parent) :string) | |
[(conj so-far {:type :text :value temp-accum}) (apply str (rest s))] | |
(let [new-node {:type :string} | |
rest-of-s (apply str (rest s)) | |
[grand-children remaining] (children new-node rest-of-s) | |
new-node-with-children (assoc new-node :children grand-children) | |
so-far-with-text (if (empty? temp-accum) | |
so-far | |
(conj so-far {:type :text :value temp-accum}))] | |
(recur (conj so-far-with-text new-node-with-children) "" remaining))) | |
(recur so-far (str temp-accum (first s)) (apply str (rest s)))))) | |
(defn parse-view [template owner] | |
(reify | |
om/IRender | |
(render [_] | |
(dom/li nil | |
(dom/p nil (dom/pre nil template)) | |
(dom/p nil (dom/pre nil | |
(let [[tree _] (children {:type :root} template)] | |
(JSON/stringify (clj->js tree) nil 2)))))))) | |
(defn main [] | |
(om/root | |
(fn [app owner] | |
(reify | |
om/IRender | |
(render [_] | |
(apply dom/ul nil (om/build-all parse-view (:templates app)))))) | |
app-state | |
{:target (. js/document (getElementById "app"))})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
something {x.y.("foo")} something else | |
[ | |
{ | |
"type": "text", | |
"value": "something " | |
}, | |
{ | |
"type": "block", | |
"children": [ | |
{ | |
"type": "text", | |
"value": "x.y.(" | |
}, | |
{ | |
"type": "string", | |
"children": [ | |
{ | |
"type": "text", | |
"value": "foo" | |
} | |
] | |
}, | |
{ | |
"type": "text", | |
"value": ")" | |
} | |
] | |
}, | |
{ | |
"type": "text", | |
"value": " something else" | |
} | |
] | |
a {b.c.("d } e")} f {c.g.("h")} | |
[ | |
{ | |
"type": "text", | |
"value": "a " | |
}, | |
{ | |
"type": "block", | |
"children": [ | |
{ | |
"type": "text", | |
"value": "b.c.(" | |
}, | |
{ | |
"type": "string", | |
"children": [ | |
{ | |
"type": "text", | |
"value": "d } e" | |
} | |
] | |
}, | |
{ | |
"type": "text", | |
"value": ")" | |
} | |
] | |
}, | |
{ | |
"type": "text", | |
"value": " f " | |
}, | |
{ | |
"type": "block", | |
"children": [ | |
{ | |
"type": "text", | |
"value": "c.g.(" | |
}, | |
{ | |
"type": "string", | |
"children": [ | |
{ | |
"type": "text", | |
"value": "h" | |
} | |
] | |
}, | |
{ | |
"type": "text", | |
"value": ")" | |
} | |
] | |
} | |
] | |
:{ }: | |
[ | |
{ | |
"type": "text", | |
"value": ":" | |
}, | |
{ | |
"type": "block", | |
"children": [ | |
{ | |
"type": "text", | |
"value": " " | |
} | |
] | |
}, | |
{ | |
"type": "text", | |
"value": ":" | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment