Skip to content

Commit

Permalink
Add support of full flux input (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
katauber committed Nov 19, 2021
1 parent 7f41ba9 commit 28f388a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 36 deletions.
17 changes: 11 additions & 6 deletions src/clj/metafacture_playground/process.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@
(content->tempfile-path morph ".morph")
"\")|"))

(defn- flux->flux-content [flux fix morph]
(defn- flux->flux-content [flux fix morph output]
(-> flux
(clj-str/replace #"\n*\|" "|")
(clj-str/replace #"\s*\|\s*" "|")
(clj-str/replace #"\n*;" ";")
(clj-str/replace #"\s*;\s*" ";")
(clj-str/replace "PG_DATA|" "")
(clj-str/replace "|fix|" fix)
(clj-str/replace "|morph|" morph)))
(clj-str/replace "|morph|" morph)
(clj-str/replace #"\|write\(\".*\"\);" output)
(clj-str/replace "|print;" output)))

(defn- flux-output []
(let [temp-file-path (content->tempfile-path "" ".txt")]
Expand All @@ -52,12 +57,12 @@
(defn- ->flux-content [data flux fix morph]
(let [fix (fix->flux-content fix)
morph (morph->flux-content morph)
[out-path output] (flux-output)]
[out-path output] (flux-output)
data-via-playground? (re-find #"PG_DATA" flux)]
[out-path
(str
(data->flux-content data)
(flux->flux-content flux fix morph)
output)]))
(when data-via-playground? (data->flux-content data))
(flux->flux-content flux fix morph output))]))

(defn- process-flux [file-path out-path]
(Flux/main (into-array [file-path]))
Expand Down
22 changes: 14 additions & 8 deletions src/cljs/metafacture_playground/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

; TODO: we should extract the samples here and in process_test.clj to files
(def sample-data {:data "1{a: Faust, b {n: Goethe, v: JW}, c: Weimar}\n2{a: Räuber, b {n: Schiller, v: F}, c: Weimar}"
:flux-with-fix "as-lines\n|decode-formeta\n|fix\n|encode-xml(rootTag=\"collection\")"
:flux-with-morph "as-lines\n|decode-formeta\n|morph\n|encode-xml(rootTag=\"collection\")"
:flux-with-fix "PG_DATA\n|as-lines\n|decode-formeta\n|fix\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:flux-with-morph "PG_DATA\n|as-lines\n|decode-formeta\n|morph\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:fix "move_field(_id, id)\nmove_field(a, title)\npaste(author, b.v, b.n, '~aus', c)\nretain(id, title, author)"
:morph (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<metamorph xmlns=\"http://www.culturegraph.org/metamorph\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
Expand Down Expand Up @@ -32,12 +32,15 @@
(def default-db
{:input-fields {:data {:content nil
:collapsed? false
:width nil}
:width nil
:disabled? true}
:flux {:content nil
:collapsed? false
:width nil}
:fix {:content nil}
:morph {:content nil}
:fix {:content nil
:disabled? true}
:morph {:content nil
:disabled? true}
:switch {:collapsed? false
:active :fix
:width nil}}
Expand All @@ -60,12 +63,15 @@
(def db-parse-fns
{:input-fields {:data {:content str
:collapsed? parseBoolean
:width int}
:width int
:disabled? parseBoolean}
:flux {:content str
:collapsed? parseBoolean
:width int}
:fix {:content str}
:morph {:content str}
:fix {:content str
:disabled? parseBoolean}
:morph {:content str
:disabled? parseBoolean}
:switch {:collapsed? parseBoolean
:active keyword
:width int}}
Expand Down
62 changes: 45 additions & 17 deletions src/cljs/metafacture_playground/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,30 @@

(defn edit-value
[{db :db} [_ field-name new-value]]
(let [db-path [:input-fields field-name :content]]
{:db (assoc-in db db-path new-value)
(let [db-path [:input-fields field-name :content]
disable-editors (when (= field-name :flux)
(let [val (-> new-value
(clj-str/replace #"\n*\|" "|")
(clj-str/replace #"\s*\|\s*" "|"))
data-used? (boolean (re-find #"PG_DATA" val))
morph-used? (boolean (re-find #"\|morph\|" val))
fix-used? (boolean (re-find #"\|fix\|" val))]
{[:input-fields :data :disabled?] (not data-used?)
[:input-fields :morph :disabled?] (not morph-used?)
[:input-fields :fix :disabled?] (not fix-used?)}))]
{:db (-> (reduce
(fn [db [path v]]
(assoc-in db path v))
db
disable-editors)
(assoc-in db-path new-value))
:storage/set {:session? true
:name (->storage-key db-path) :value new-value}
:pairs (conj
(mapv
(fn [[db-path v]]
{:name (->storage-key db-path) :value v})
disable-editors)
{:name (->storage-key db-path) :value new-value})}
:dispatch [::update-width field-name new-value]}))

(re-frame/reg-event-fx
Expand Down Expand Up @@ -157,26 +177,34 @@

(defn- clear-db [db paths]
(reduce
(fn [db path]
(assoc-in db path nil))
(fn [db [path v]]
(assoc-in db path v))
db
paths))

(defn clear-all
[{db :db} _]
(let [storage-paths [[:input-fields :data :content]
[:input-fields :flux :content]
[:input-fields :fix :content]
[:input-fields :morph :content]
[:input-fields :data :width]
[:input-fields :flux :width]
[:input-fields :switch :width]]
other-paths [[:result :content]
[:links :api-call]
[:links :workflow]]]
{:db (clear-db db (concat storage-paths other-paths))
(let [storage-remove [[[:input-fields :data :content] nil]
[[:input-fields :flux :content] nil]
[[:input-fields :fix :content] nil]
[[:input-fields :morph :content] nil]
[[:input-fields :data :width] nil]
[[:input-fields :flux :width] nil]
[[:input-fields :switch :width] nil]]
storage-set [[[:input-fields :data :disabled?] true]
[[:input-fields :fix :disabled?] true]
[[:input-fields :morph :disabled?] true]]
other [[[:result :content] nil]
[[:links :api-call] nil]
[[:links :workflow] nil]]]
{:db (clear-db db (concat storage-remove other storage-set))
:storage/remove {:session? true
:names (mapv ->storage-key storage-paths)}}))
:names (mapv #(->storage-key (first %)) storage-remove)}
:storage/set {:session? true
:pairs (mapv (fn [[path v]]
{:name (->storage-key path)
:value v})
storage-set)}}))

(re-frame/reg-event-fx
::clear-all
Expand Down
5 changes: 5 additions & 0 deletions src/cljs/metafacture_playground/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
(fn [db [_ path]]
(get-in db (conj path :collapsed?))))

(re-frame/reg-sub
::disabled?
(fn [db [_ editor]]
(get-in db [:input-fields editor :disabled?])))

(re-frame/reg-sub
::active-editor
(fn [db _]
Expand Down
8 changes: 6 additions & 2 deletions src/cljs/metafacture_playground/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@
(let [editor-name (-> config :name keyword)
path [:input-fields editor-name]
collapsed? (re-frame/subscribe [::subs/collapsed? path])
disabled? (re-frame/subscribe [::subs/disabled? editor-name])
width (re-frame/subscribe [::subs/editor-width editor-name])]
[:> grid-column {:width (or @width (:width config))}
[:> segment {:raised true}
[:> segment {:raised true
:disabled @disabled?}
[:> menu
{:color color
:stackable true}
Expand Down Expand Up @@ -317,11 +319,13 @@
(let [path [:input-fields :switch]
collapsed? (re-frame/subscribe [::subs/collapsed? path])
current-editor (re-frame/subscribe [::subs/active-editor])
disabled? (re-frame/subscribe [::subs/disabled? @current-editor])
editor-config (merge (get-in config [:different @current-editor])
(:common config))
width (re-frame/subscribe [::subs/editor-width :switch])]
[:> grid-column {:width (or @width (-> config :common :width))}
[:> segment {:raised true}
[:> segment {:raised true
:disabled @disabled?}
[:> menu
{:color color
:stackable true}
Expand Down
4 changes: 2 additions & 2 deletions test/clj/metafacture_playground/process_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

; TODO: we should extract the samples here and in db.cljs to files
(def sample-data {:data "1{a: Faust, b {n: Goethe, v: JW}, c: Weimar}\n2{a: Räuber, b {n: Schiller, v: F}, c: Weimar}"
:flux-with-fix "as-lines\n|decode-formeta\n|fix\n|encode-xml(rootTag=\"collection\")"
:flux-with-morph "as-lines\n|decode-formeta\n|morph\n|encode-xml(rootTag=\"collection\")"
:flux-with-fix "PG_DATA\n|as-lines\n|decode-formeta\n|fix\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:flux-with-morph "PG_DATA\n|as-lines\n|decode-formeta\n|morph\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:fix "move_field(_id, id)\nmove_field(a, title)\npaste(author, b.v, b.n, '~aus', c)\nretain(id, title, author)"
:morph (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<metamorph xmlns=\"http://www.culturegraph.org/metamorph\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
Expand Down
18 changes: 17 additions & 1 deletion test/cljs/metafacture_playground/event_handler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,23 @@
(dissoc :storage/set))]
(and (is (not= db' empty-db))
(is (= (get-in db' [:db :input-fields :fix :content])
new-value))))))
new-value))
(is (true? (get-in db' [:db :input-fields :data :disabled?])))
(is (true? (get-in db' [:db :input-fields :fix :disabled?])))
(is (true? (get-in db' [:db :input-fields :morph :disabled?]))))))

(testing "Test disabling editor depending on editing values")
(let [new-value "I use the input PG_DATA and a | morph | "
db' (-> empty-db
(events/edit-value [:edit-input-value :flux new-value])
(update-in [:db :input-fields] dissoc :result)
(dissoc :storage/set))]
(and (is (not= db' empty-db))
(is (= (get-in db' [:db :input-fields :flux :content])
new-value))
(is (false? (get-in db' [:db :input-fields :data :disabled?])))
(is (true? (get-in db' [:db :input-fields :fix :disabled?])))
(is (false? (get-in db' [:db :input-fields :morph :disabled?]))))))

(deftest load-sample-test
(testing "Test loading sample with all fields empty."
Expand Down

0 comments on commit 28f388a

Please sign in to comment.