Create a gist now

Instantly share code, notes, and snippets.

Embed
Parsing with clojure.spec
(require '[clojure.spec.alpha :as s])
[:h1 {} "0" 1 [:span]]
(s/def :hiccup/form
(s/or
:string string?
:number number?
:element :hiccup/element))
(s/def :hiccup/element
(s/cat
:tag keyword?
:attrs (s/? map?)
:children (s/* :hiccup/form)))
(defn parse-hiccup [hiccup]
(s/conform :hiccup/form hiccup))
(defmulti html first)
(defmethod html :string [[_ v]]
v)
(defmethod html :number [[_ v]]
v)
(defmethod html :element [[_ {:keys [tag attrs children]}]]
(str "<" (name tag) ">"
(->> (map html children)
(clojure.string/join ""))
"</" (name tag) ">"))
(html (parse-hiccup [:h1 {} "Hello" [:span "1" 2]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment