(structurize-hiccup flat-hiccup)
(structurize-hiccup flat-hiccup structured-hiccup)
Takes a flat list of Hiccup-compatible data and adds structure to it.
Spec
(clojure.spec.alpha/fspec
:args
(clojure.spec.alpha/alt
:one-arg
(clojure.spec.alpha/cat
:flat-hiccup
(clojure.spec.alpha/coll-of clojure.core/any?))
:two-args
(clojure.spec.alpha/cat
:flat-hiccup
(clojure.spec.alpha/coll-of clojure.core/any?)
:structured-hiccup
(clojure.spec.alpha/coll-of clojure.core/any?)))
:ret
(clojure.spec.alpha/coll-of clojure.core/any?)
:fn
nil)
Source
(defn structurize-hiccup
"Takes a flat list of Hiccup-compatible data and adds structure to it."
([flat-hiccup]
(second (structurize-hiccup flat-hiccup [:span])))
([flat-hiccup structured-hiccup]
(loop [flat-hiccup flat-hiccup
structured-hiccup structured-hiccup]
(if-let [token (first flat-hiccup)]
(cond
(string? token)
(recur (rest flat-hiccup) (conj structured-hiccup token))
(vector? token)
(let [[flat structured] (structurize-hiccup (rest flat-hiccup) token)]
(recur flat (conj structured-hiccup structured))))
[(rest flat-hiccup) structured-hiccup]))))