Last active
February 23, 2018 01:06
-
-
Save rplevy/79732dd67238ec53c35a21e8aa82897e to your computer and use it in GitHub Desktop.
edn->json command-line script + emacs interactive function interface
This file contains 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
#!/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)) | |
This file contains 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
(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