diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/dompa/nodes.cljc | 24 | ||||
| -rw-r--r-- | src/dompa/utils.cljc | 47 |
2 files changed, 46 insertions, 25 deletions
diff --git a/src/dompa/nodes.cljc b/src/dompa/nodes.cljc index d035116..cf79e21 100644 --- a/src/dompa/nodes.cljc +++ b/src/dompa/nodes.cljc @@ -4,19 +4,27 @@ #{:!doctype :area :base :br :col :embed :hr :img :input :link :meta :source :track :wbr}) +(defn- node-attrs-reducer [attrs k v] + (let [attr-name (-> k name)] + (if (true? v) + (str attrs " " attr-name) + (str attrs " " attr-name "=\"" v "\"")))) + (defn- node->html-reducer-fn [void-nodes nodes->html-fn] (fn [html node] - (cond - (= (-> node :name) :dompa/text) - (str html (-> node :value)) + (let [node-name (-> node :name name) + node-attrs (reduce-kv node-attrs-reducer "" (-> node :attrs))] + (cond + (= (-> node :name) :dompa/text) + (str html (-> node :value)) - (contains? void-nodes (-> node :name)) - (str "<" (-> node :name) ">") + (contains? void-nodes (-> node :name)) + (str html "<" node-name node-attrs">") - :else - (let [value (nodes->html-fn (-> node :children))] - (str "<" (-> node :name) ">" value "</" (-> node :name) ">"))))) + :else + (let [value (nodes->html-fn (-> node :children))] + (str html "<" node-name node-attrs ">" value "</" node-name ">")))))) (defn traverse "Recursively traverses given tree of `nodes` with a `traverser-fn` diff --git a/src/dompa/utils.cljc b/src/dompa/utils.cljc index 1a6bff7..285566d 100644 --- a/src/dompa/utils.cljc +++ b/src/dompa/utils.cljc @@ -1,21 +1,34 @@ -(ns dompa.utils) +(ns dompa.utils + (:require [dompa.nodes :as nodes])) (defmacro $ [name & opts] - (let [node# {:name (keyword name)} - attrs? (map? (first opts)) - attrs (if attrs? (first opts) {}) - children (if attrs? (rest opts) opts)] - (merge - node# - (when attrs? - {:attrs attrs}) - (when-not (empty? children) - {:children (into [] children)})))) + (if (string? name) + {:name :dompa/text + :value name} + (let [node {:name name} + attrs? (map? (first opts)) + attrs (if attrs? (first opts) {}) + children (if attrs? (rest opts) opts)] + (merge + node + (when attrs? + {:attrs attrs}) + (when-not (empty? children) + {:children (into [] children)}))))) -(comment +(defn- page [] (list - ($ doctype {:html true}) - ($ head) - ($ body - ($ span {:class "test"} - ($ span {:class "test2"})))))
\ No newline at end of file + ($ :!doctype {:html true}) + ($ :html {:lang "en"} + ($ :head + ($ :meta {:charset "utf-8"}) + ($ :link {:rel "stylesheet" :href "style.css"})) + ($ :body + ($ :span {:class "test"} + ($ :span {:class "test2"} + ($ "hello, world."))))))) + +(comment + ($ "asdasd") + (page) + (nodes/->html (page)))
\ No newline at end of file |
