When you want to create a webpage with clojure but you're too lazy to use hiccup and garden
Last active
February 2, 2016 19:26
-
-
Save lildata/e54a7821caea11308c20 to your computer and use it in GitHub Desktop.
When you want to create a webpage with clojure but you're too lazy to use hiccup and garden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; Simple CSS generation using nested maps in clojure (no order, I don't care) | |
| ; sample css | |
| (def css1 {"body" {:background-color :#d0e4fe} | |
| "h1" {:color :orange | |
| :text-align :center} | |
| "p" {:font-family "Times New Roman" | |
| :font-size :20px}}) | |
| ;a bit more complicated | |
| (def color-theme { :blue [:#ecf2f9 :#c6d9ec :#9fbfdf :#79a6d2 :#538cc6 :#3973ac :#2d5986 :#204060 :#132639 :#060d13] | |
| :red [:#1a0000 :#4d0000 :#800000 :#b30000 :#e60000 :#ff1a1a :#ff4d4d :#ff8080 :#ffb3b3 :#ffe5e5]}) | |
| (def bordered { :border-bottom "1px solid #000" | |
| :padding :2.8em }) | |
| (def css2 {"body" {:background-color :#d0e4fe} | |
| "table" (merge | |
| {:color (-> color-theme :blue (get 2)) | |
| :text-align :center} | |
| bordered) | |
| "p" {:color (-> color-theme :blue (get 5)) | |
| :font-family "Times New Roman" | |
| :font-size :20px}}) | |
| (defn format-pv [[property value]] | |
| (str (name property) " : " (if (keyword? value) (name value) (str "\"" value "\"")) ";")) | |
| (defn nmap->css [nmap] | |
| (clojure.string/join | |
| "\n" | |
| (map | |
| (fn [[k v]] | |
| (str k " { " (clojure.string/join "\n" (map format-pv v)) " }")) | |
| nmap))) | |
| ; test | |
| (nmap->css css1) | |
| ;"body { background-color : #d0e4fe; } | |
| ; h1 { color : orange;\ntext-align : center; } | |
| ; p { font-family : \"Times New Roman\";\nfont-size : 20px; }" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns xxx.xxx | |
| (:use | |
| ;[clojure.zip :as zip] | |
| ;[clojure.data.zip.xml :as zx] | |
| [clojure.data.xml :rename {element e}])) | |
| (defn html [title css & body] | |
| (e :html { :lang "en"} | |
| (e :head {} | |
| (e :meta {:charset "utf-8"} | |
| (e :title {} title) | |
| (e :style {} css)) | |
| (e :body {} body)))) | |
| ;(defn p | |
| ; ([i] (e :p {} ( )))) | |
| (defn span | |
| ([i] (e :span {} i)) | |
| ([class i] (e :span {:class class} i))) | |
| (defn a | |
| ([i href] (e :a {:href href} i)) | |
| ([class i href] (e :a {:class class :href href} i))) | |
| (defn tr | |
| ([tds] (e :tr {} (for [y tds] (e :td {} y))))) | |
| (defn table | |
| ([trs] (e :table {} (for [x trs] (tr x))))) | |
| (spit "test1.html" | |
| (emit-str (html "A test for an html page" (nmap->css css1) | |
| (e :p {} "Hello " (a "clojure" "http://www.clojure.org")) | |
| (table [["1" "2" "3"]["hello" "good" "morning"]]) | |
| ))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment