/
helper.cljc
68 lines (53 loc) · 1.54 KB
/
helper.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns pinkgorilla.encoding.helper
(:require
[clojure.string :as str]
[cognitect.transit :as t]))
;; Cognitec TRANSIT WRITER
;; make json encoding indepenent of clj/cljs
#?(:clj (import '[java.io ByteArrayInputStream ByteArrayOutputStream]))
(defn create-writer []
#?(:clj (let [out (ByteArrayOutputStream. (* 4096 1024))
writer (t/writer out :json)]
{:writer writer :out out})
:cljs (t/writer :json)))
(defn to-json [writer o]
#?(:clj (do (t/write (:writer writer) o)
(String. (.toByteArray (:out writer))))
:cljs (t/write writer o)))
(defn from-json [s]
#?(:clj
(do
(def in (ByteArrayInputStream. (.getBytes s "UTF-8")))
(def reader (t/reader in :json))
(t/read reader))
:cljs (t/read (t/reader :json) s)))
;; COMMENT ENCODING / DECODING
(defn make-clojure-comment
[code]
#_(println "make-clojure-comment for: " code)
(if (nil? code)
";;; "
(->> (str/split-lines code)
(map #(str ";;; " %))
(str/join "\n"))))
(defn unmake-line [l]
(if (nil? l)
(do (println "unmake-line called with nil.")
"")
(let [c (count l)]
(if (< c 4)
(do (println "unmake-line called with less than 4 characters: " l)
l)
(subs l 4 c)))))
(defn unmake-clojure-comment
[code]
(if (nil? code)
""
(->> (str/split-lines code)
(map unmake-line)
(str/join "\n"))))
;(defn unmake-clojure-comment
; [code]
; (try
; (unmake-clojure-comment-x code)
; (catch Exception e code)))