Skip to content

Instantly share code, notes, and snippets.

@ertugrulcetin
Created October 16, 2020 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ertugrulcetin/cc43a5c6a47fd1f501aafa5f6923449d to your computer and use it in GitHub Desktop.
Save ertugrulcetin/cc43a5c6a47fd1f501aafa5f6923449d to your computer and use it in GitHub Desktop.
Manipulate Xmind file in Clojure
(defn update-xmind
"Manipulates Xmind data and saves as a binary format. Usage:
This example updates all \"Report\" titles with \"HEY!\"
(update-xmind \"~/xmind-files/report.xmind\"
\"~/xmind-files/report-new.xmind\"
(fn [data]
(o/transform data
(o/and
(d/query data ?path :title ?title)
(d/== ?title \"Report\")
(o/update ?path :title))
(fn [x] \"HEY!\"))))"
[src target update-fn]
(let [zip-file (ZipFile. src)
input (-> src io/file io/input-stream (ZipInputStream.))
zip-entries (->> (repeatedly #(.getNextEntry input))
(take-while some?)
(remove #(= "content.json" (.getName %))))
data (-> src io/file io/input-stream input-stream->json)
json-str (json/write-str (update-fn data))]
(with-open [fos (FileOutputStream. target)
bos (BufferedOutputStream. fos)
zos (ZipOutputStream. bos)]
(doseq [z zip-entries]
(.putNextEntry zos (ZipEntry. z))
(.write zos (.readAllBytes (.getInputStream zip-file (.getEntry zip-file (.getName z)))))
(.closeEntry zos))
(.putNextEntry zos (ZipEntry. "content.json"))
(.write zos (.getBytes json-str))
(.closeEntry zos))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment