summaryrefslogtreecommitdiff
path: root/src/dompa/nodes.cljc
diff options
context:
space:
mode:
Diffstat (limited to 'src/dompa/nodes.cljc')
-rw-r--r--src/dompa/nodes.cljc52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/dompa/nodes.cljc b/src/dompa/nodes.cljc
index d72aa75..24d446e 100644
--- a/src/dompa/nodes.cljc
+++ b/src/dompa/nodes.cljc
@@ -73,4 +73,54 @@
(->html nodes {:void-nodes default-void-nodes}))
([nodes {:keys [void-nodes]}]
(-> (node->html-reducer-fn void-nodes ->html)
- (reduce "" nodes)))) \ No newline at end of file
+ (reduce "" nodes))))
+
+(defmacro defhtml
+ "Creates a new function with `name` that outputs HTML.
+
+ Example usage:
+
+ ```clojure
+ (defhtml about-page [who]
+ ($ :div
+ ($ \"hello \" who)))
+
+ (about-page \"world\")
+ ```
+ "
+ [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 $
+ "A helper that simplifies node creation. Particularly useful
+ where you need compile-time composition over run-time, like when
+ combined with the `defhtml` macro."
+ [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#))))))