Skip to content

Instantly share code, notes, and snippets.

Last active July 4, 2022 17:50
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
encode and decode files as kroki url diagrams
(import '[ ByteArrayOutputStream])
(import '[java.util Base64]) ;
(import '[ Inflater]) ;
(defn uncompress [source-bytes]
(let [inflater (doto (Inflater.)
(.setInput source-bytes))
outputStream (new ByteArrayOutputStream (count source-bytes))
buffer (byte-array 1024)]
(while (not (.finished inflater))
(let [c (.inflate inflater buffer)]
(.write outputStream buffer 0 c)))
(.close outputStream)
(str outputStream)))
(defn decode [encoded]
(.decode encoded)
; invoke with a kroki url. optional second arg can be file to print to
(let [[url out-file] *command-line-args*
(decode (last (clojure.string/split url #"/")))]
(if out-file
(spit out-file contents)
(println contents)))
(import '[ ByteArrayOutputStream])
(import '[java.util Base64]) ;
(import '[ Deflater]) ;
(defn compress [source]
(let [deflater (doto (Deflater. (Deflater/BEST_COMPRESSION))
(.setInput source)
buffer (byte-array 2048)
compressed-length (.deflate deflater buffer)
result (byte-array compressed-length)]
(System/arraycopy buffer 0 result 0 compressed-length)
(defn encode [decoded]
(.encodeToString (compress (.getBytes decoded)))))
;e.g. test.puml c4plantuml
(str "" (second *command-line-args*) "/svg/"
(encode (slurp (first *command-line-args*)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment