diff options
Diffstat (limited to 'src/dompa')
| -rw-r--r-- | src/dompa/nodes.clj | 26 | ||||
| -rw-r--r-- | src/dompa/nodes.cljs | 24 | ||||
| -rw-r--r-- | src/dompa/nodes/shared.cljc (renamed from src/dompa/nodes.cljc) | 43 |
3 files changed, 52 insertions, 41 deletions
diff --git a/src/dompa/nodes.clj b/src/dompa/nodes.clj new file mode 100644 index 0000000..53020ba --- /dev/null +++ b/src/dompa/nodes.clj @@ -0,0 +1,26 @@ +(ns dompa.nodes) + +(defmacro $ + "Creates a new node. Particularly useful + where you need compile-time composition over run-time, like when + combined with the `defhtml` macro to create HTML string outputs. + + Usage: + + ```clojure + ($ :div + ($ \"hello world\" )) + ```" + [name & opts] + `(if (string? ~name) + {:node/name :dompa/text + :node/value (str ~name ~@opts)} + (let [opts# (list ~@opts) + first-opt# (first opts#) + attrs?# (and (map? first-opt#) + (not (contains? first-opt# :node/name))) + attrs# (if attrs?# first-opt# {}) + children# (if attrs?# (rest opts#) opts#)] + (cond-> {:node/name ~name} + attrs?# (assoc :node/attrs attrs#) + (seq children#) (assoc :node/children children#))))) diff --git a/src/dompa/nodes.cljs b/src/dompa/nodes.cljs new file mode 100644 index 0000000..8e19e2f --- /dev/null +++ b/src/dompa/nodes.cljs @@ -0,0 +1,24 @@ +(ns dompa.nodes) + +(defn $ + "Creates a new node + + Usage: + + ```clojure + ($ :div + ($ \"hello world\" )) + ```" + [name & opts] + (if (string? name) + {:node/name :dompa/text + :node/value (apply str name opts)} + (let [first-opt (first opts) + attrs? (and (map? first-opt) + (not (contains? first-opt :node/name))) + attrs (if attrs? first-opt {}) + children (if attrs? (rest opts) opts)] + (cond-> {:node/name name} + attrs? (assoc :node/attrs attrs) + (seq children) (assoc :node/children children))))) + diff --git a/src/dompa/nodes.cljc b/src/dompa/nodes/shared.cljc index 2453b88..b876048 100644 --- a/src/dompa/nodes.cljc +++ b/src/dompa/nodes/shared.cljc @@ -1,4 +1,4 @@ -(ns dompa.nodes) +(ns dompa.nodes.shared) (def ^:private default-void-nodes #{:!doctype :area :base :br :col :embed :hr :img :input @@ -92,43 +92,4 @@ [name & args-and-elements] (let [[args & elements] args-and-elements] `(defn ~name ~args - (->html (vector ~@elements))))) - -(defn $->flat-xf [] - (fn [rf] - (letfn [(step [result input] - (if (sequential? input) - (reduce step result input) - (rf result input)))] - (fn - ([] (rf)) - ([result] (rf result)) - ([result input] (step result input)))))) - -(defn $->flat [children] - (into [] ($->flat-xf) children)) - -(defmacro $ - "Creates a new node. Particularly useful - where you need compile-time composition over run-time, like when - combined with the `defhtml` macro to create HTML string outputs. - - Usage: - - ```clojure - ($ :div - ($ \"hello world\" )) - ```" - [name & opts] - `(if (string? ~name) - {:node/name :dompa/text - :node/value (str ~name ~@opts)} - (let [opts# (list ~@opts) - first-opt# (first opts#) - attrs?# (and (map? first-opt#) - (not (contains? first-opt# :node/name))) - attrs# (if attrs?# first-opt# {}) - children# (if attrs?# (rest opts#) opts#)] - (cond-> {:node/name ~name} - attrs?# (assoc :node/attrs attrs#) - (seq children#) (assoc :node/children ($->flat children#)))))) + (->html [~@elements])))) |
