Skip to content

Instantly share code, notes, and snippets.



Last active Dec 5, 2016
What would you like to do?
(ns render.format)
(defn standardize [form]
(vector? form)
(let [[k m & rest] form]
(if (map? m)
(vec (list* k m (map standardize rest)))
(vec (list* k {} (standardize m) (map standardize rest)))))
(seq? form)
(map standardize form)
:default form))
(def inline-tags #{"span" "pre" "a"})
(defn render
([form] (render 0 false (standardize form)))
([indent inline? form]
(let [
indent-str (if inline? "" (apply str (repeat indent " ")))
newline (if inline? "" "\r\n")
(vector? form)
(let [[k m & rest] form
k (name k)
inline? (or inline? (inline-tags k))
attrs (apply str (map (fn [[k v]] (str " " (name k) "=\"" v "\"")) m))
subforms (apply str (map #(render (inc indent) inline? %) rest))
(inline-tags k) (format "%s<%s%s>%s</%s>\r\n" indent-str k attrs subforms k)
inline? (format "<%s%s>%s</%s>" k attrs subforms k)
:default (format "%s<%s%s>\r\n%s%s</%s>\r\n" indent-str k attrs subforms indent-str k)))
(seq? form) (apply str (map #(render indent inline? %) form))
(string? form)
(apply str (map #(str indent-str (.trim %) newline) (.split (.trim form) "\n")))
:default (str indent-str form newline)))))
(defn html5 [& forms]
(str "<!DOCTYPE html>\r\n" (render [:html forms])))
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.