Skip to content

Instantly share code, notes, and snippets.

@rplevy
Last active February 23, 2018 01:06
Show Gist options
  • Save rplevy/79732dd67238ec53c35a21e8aa82897e to your computer and use it in GitHub Desktop.
Save rplevy/79732dd67238ec53c35a21e8aa82897e to your computer and use it in GitHub Desktop.
edn->json command-line script + emacs interactive function interface
#!/usr/local/bin/planck
(ns scripts.edn2json
(:require [cljs.reader :as r]
[planck.core :refer [slurp *command-line-args* *in* line-seq]]))
(defn last-n-chars [n s]
(apply str (reverse (take n (reverse s)))))
(let [s (first *command-line-args*)
edn (if (#{".edn" ".clj"} (last-n-chars 4 s))
(slurp s)
(or s
(apply str (line-seq *in*))))]
(->> edn
r/read-string
clj->js
(.stringify js/JSON)
println))
(defmacro defshellonreg (name cmd &optional replace)
`(defun ,name (beg end)
(interactive "r")
(shell-command-on-region beg end ,cmd
(if ,replace (current-buffer) nil)
,replace)))
;; ...
(defshellonreg edn->json "edn2json.cljs" t)
;; {:foo :bar, :baz 1} => {"foo":"bar","baz":1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment