Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (78 sloc) 2.83 KB
(defn add-dependencies
"A helper function to lazily load dependencies using Pomegranate."
[& args]
(when-not (find-ns 'cemerick.pomegranate)
(require '[cemerick.pomegranate]))
(apply (resolve 'cemerick.pomegranate/add-dependencies)
(concat args
[:repositories (merge @(resolve 'cemerick.pomegranate.aether/maven-central) {"clojars" "https://clojars.org/repo"})])))
; EDN
; ====================
(defcmd from-edn [s]
(clojure.edn/read-string s))
(defcmd to-edn [& more]
(apply prn-str more))
; JSON
; ====================
(defcmd from-json [& args]
(if (= (count args) 1)
; Keywordize keys by default
(clojure.data.json/read-str (first args) :key-fn keyword)
(apply clojure.data.json/read-str args)))
(defcmd to-json [& args]
(apply clojure.data.json/write-str args))
; CSV
; ====================
; Maybe integrate csv-data->maps as an option, e.g. (from-csv input :maps true)
(defcmd from-csv [s]
(when-not (find-ns 'clojure.data.csv)
(add-dependencies :coordinates '[[org.clojure/data.csv "0.1.4"]])
(require '[clojure.data.csv]))
((resolve 'clojure.data.csv/read-csv) s))
(defcmd to-csv [x]
(when-not (find-ns 'clojure.data.csv)
(add-dependencies :coordinates '[[org.clojure/data.csv "0.1.4"]])
(require '[clojure.data.csv]))
(let [writer (java.io.StringWriter.)]
((resolve 'clojure.data.csv/write-csv) writer x)
(str writer)))
; From https://github.com/clojure/data.csv/blob/master/README.md
(defcmd csv-data->maps [csv-data]
(map zipmap
(->> (first csv-data) ;; First row is the header
(map keyword) ;; Drop if you want string keys instead
repeat)
(rest csv-data)))
; XML
; ====================
(defcmd from-xml [& args]
(when-not (find-ns 'clojure.data.xml)
(add-dependencies :coordinates '[[org.clojure/data.xml "0.0.8"]])
(require '[clojure.data.xml]))
(apply (resolve 'clojure.data.xml/parse-str) args))
(defcmd to-xml [& args]
(when-not (find-ns 'clojure.data.xml)
(add-dependencies :coordinates '[[org.clojure/data.xml "0.0.8"]])
(require '[clojure.data.xml]))
(apply (resolve 'clojure.data.xml/emit-str) args))
; html->hiccup
; ====================
(defcmd html->hiccup [s]
(when-not (find-ns 'hiccup-bridge.core)
(add-dependencies :coordinates '[[hiccup-bridge "1.0.1"]])
(require '[hiccup-bridge.core]))
((resolve 'hiccup-bridge.core/html->hiccup) s))
; (defcmd html->hiccup [s]
; (when-not (find-ns 'hickory.core)
; (add-dependencies :coordinates '[[hickory "0.7.1"]])
; (require '[hickory.core]))
; (-> s
; ((resolve 'hickory.core/parse))
; ((resolve 'hickory.core/as-hiccup))))
; Misc
; ====================
(defcmd to-slug [s]
(when-not (find-ns 'slugger.core)
(add-dependencies :coordinates '[[slugger "1.0.1"]])
(require '[slugger.core]))
((resolve 'slugger.core/->slug) s))