Skip to content

Instantly share code, notes, and snippets.

@henryw374
Last active July 4, 2022 17:50
Show Gist options
  • Save henryw374/070845dbd8cfb4672a3c0d06cf8b00e4 to your computer and use it in GitHub Desktop.
Save henryw374/070845dbd8cfb4672a3c0d06cf8b00e4 to your computer and use it in GitHub Desktop.
encode and decode files as kroki url diagrams https://kroki.io/#try
#!/usr/local/bin/bb
(import '[java.io ByteArrayOutputStream])
(import '[java.util Base64]) ;
(import '[java.util.zip 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]
(->
(Base64/getUrlDecoder)
(.decode encoded)
uncompress))
; invoke with a kroki url. optional second arg can be file to print to
(let [[url out-file] *command-line-args*
contents
(decode (last (clojure.string/split url #"/")))]
(if out-file
(spit out-file contents)
(println contents)))
#!/usr/local/bin/bb
(import '[java.io ByteArrayOutputStream])
(import '[java.util Base64]) ;
(import '[java.util.zip Deflater]) ;
(defn compress [source]
(let [deflater (doto (Deflater. (Deflater/BEST_COMPRESSION))
(.setInput source)
(.finish))
buffer (byte-array 2048)
compressed-length (.deflate deflater buffer)
result (byte-array compressed-length)]
(System/arraycopy buffer 0 result 0 compressed-length)
result))
(defn encode [decoded]
(->
(Base64/getUrlEncoder)
(.encodeToString (compress (.getBytes decoded)))))
;e.g. kroki-url.bb test.puml c4plantuml
(println
(str "https://kroki.io/" (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