Skip to content

Instantly share code, notes, and snippets.

@jimmyhmiller
Last active May 9, 2020 20:50
Show Gist options
  • Save jimmyhmiller/b4141216f4c176ad9f5a57d66918c38c to your computer and use it in GitHub Desktop.
Save jimmyhmiller/b4141216f4c176ad9f5a57d66918c38c to your computer and use it in GitHub Desktop.
(require '[clojure.string :as string]
'[meander.match.delta :as r.match]
'[meander.strategy.delta :as r]
'[meander.substitute.delta :as sub])
(def html
[:html {:lang "en"}
[:head
[:meta {:charset "UTF-8"}]
[:meta {:name "viewport"
:content "width=device-width, initial-scale=1"}]
[:title]
[:link {:href "https://unpkg.com/[email protected]/css/tachyons.min.css"
:rel "stylesheet"}]]
[:body
(for [color ["blue" "dark-blue" "light-blue"]]
[:div
[:p {:class color} color]
[:br]])]])
(def void-tags #{:area :base :br :col :embed :hr :img :input :link :meta :param :source :track :wbr})
(defn build-attrs [attrs]
(->>
(for [[attr-name attr-value] attrs]
(str " " (name attr-name) "=" "\"" (name attr-value) "\""))
(string/join "")))
(defn hiccup->html [data]
(let [rec (partial trampoline hiccup->html)]
(r.match/match data
(or [(pred void-tags ?tag-name) {:as !attrs} . _ ...]
[(pred void-tags ?tag-name) . _ ...])
(let [tag (name ?tag-name)]
(str "<"
tag
(build-attrs (first !attrs))
" />" ))
(or [?tag-name {:as !attrs} . !content ...]
[?tag-name . !content ...])
(let [tag (name ?tag-name)]
(str "<"
tag
(build-attrs (first !attrs))
">" (string/join "" (map rec !content)) "</" tag ">"))
(!xs ...)
(string/join "" (map rec !xs))
;; Everything else.
?x
?x)))
(hiccup->html html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment