Skip to content

Instantly share code, notes, and snippets.

@roman01la roman01la/core.clj
Created Feb 4, 2018

What would you like to do?
Parsing with clojure.spec
(require '[clojure.spec.alpha :as s])
[:h1 {} "0" 1 [:span]]
(s/def :hiccup/form
:string string?
:number number?
:element :hiccup/element))
(s/def :hiccup/element
: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]]
(defmethod html :number [[_ 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
You can’t perform that action at this time.