Created
October 16, 2020 16:39
-
-
Save ertugrulcetin/cc43a5c6a47fd1f501aafa5f6923449d to your computer and use it in GitHub Desktop.
Manipulate Xmind file in Clojure
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
(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