Skip to content

Commit

Permalink
feat: .csv export
Browse files Browse the repository at this point in the history
  • Loading branch information
motform committed Oct 6, 2024
1 parent 530adab commit c3a6252
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/org/motform/multiverse/components/library.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
(str "multiverse-library-export-" (nano-id) ".md")
"text/markdown"))

(defn export-csv []
(util/download-file
(->> @(rf/subscribe [:db/stories])
(map story/->csv)
(str/join "\n")
(str story/csv-header "\n"))
(str "multiverse-library-export-" (nano-id) ".csv")
"text/csv"))

(defn open-story [id]
(rf/dispatch [:story/active id])
(rf/dispatch [:page/active :page/story])
Expand Down Expand Up @@ -49,16 +58,17 @@
[:section.h-stack.spaced.centered
[:section.h-stack.gap-half
[:button.button-secondary.rounded.shadow-medium
{:on-pointer-down #(export-markdown)}
{:on-pointer-down export-markdown}
"export as Markdown"]

[:button.button-secondary.rounded.shadow-medium
{:on-pointer-down #(export-markdown)}
{:on-pointer-down export-csv}
"export as CSV"]]

[:button.shadow-medium.button-secondary.rounded
{:on-pointer-down #(when (.confirm js/window "Do you really want to empty the library? This deletes all stories and can not be undone!")
(rf/dispatch [:library/clear]))}
{:on-pointer-down
#(when (.confirm js/window "Do you really want to empty the library? This deletes all stories and can not be undone!")
(rf/dispatch [:library/clear]))}
"Delete all stories"]])

(defn Empty []
Expand Down
16 changes: 14 additions & 2 deletions src/org/motform/multiverse/story.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,27 @@
children-texts (map #(get-in sentences [% :sentence/text]) children)]
(str/join "\n" (cons (:sentence/text base-sentence) children-texts))))

;; TODO: Respect newline formatting in export
(defn ->md [story]
(let [meta (:story/meta story)]
(str
"# " (:story/title meta) "\n"
"---\n"
"id:" (:story/id meta) "\n"
"model:" (name (:story/model meta)) "\n"
"prompt:" (:story/prompt meta) "\n"
"prompt-version:" (:story/prompt-version meta) "\n"
"date:" (util/format-date (:story/updated meta)) "\n"
"---\n"
(longest-sentence story))))


(def csv-header
"id,model,prompt-version,date,sentences")

(defn ->csv [story]
(let [meta (:story/meta story)]
(str
"\"" (:story/id meta) "\","
"\"" (name (:story/model meta)) "\","
"\"" (:story/prompt-version meta) "\","
"\"" (util/format-date (:story/updated meta)) "\","
"\"" (str/replace (longest-sentence story) #"\"" "'") "\"")))

0 comments on commit c3a6252

Please sign in to comment.