Skip to content

Commit

Permalink
Add more examples to load in playground (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
katauber committed Dec 2, 2021
1 parent 1eb5bd8 commit b72b378
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 90 deletions.
1 change: 1 addition & 0 deletions resources/examples/JSON_to_JSON
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://test.metafacture.org/playground/?flux=%22https%3A//lobid.org/gnd/11942150X%22%0A%7C+open-http%28accept%3D%22application/json%22%29%0A%7C+as-lines%0A%7C+decode-json%0A%7C+fix%0A%7C+encode-json%28prettyPrinting%3D%22true%22%29%0A%7C+print%3B&fix=copy_field%28%22preferredName%22%2C+%22name%22%29%0Acopy_field%28%22affiliation%5B%5D.1.label%22%2C+%22placeOfWork%22%29%0Aretain%28%22name%22%2C+%22placeOfWork%22%29&active-editor=fix
1 change: 1 addition & 0 deletions resources/examples/MarcXML__to_JSON
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://test.lobid.org/playground/process?flux=%22https%3A//d-nb.info/1106253078/about/marcxml%22%0A%7C+open-http%28accept%3D%22application/xml%22%29%0A%7C+decode-xml%0A%7C+handle-marcxml%0A%7C+encode-json%28prettyPrinting%3D%22true%22%29%0A%7C+print%0A%3B
1 change: 1 addition & 0 deletions resources/examples/OAI-PMH_MODS_to_JSON
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://test.metafacture.org/playground/?flux=%22https%3A//duepublico2.uni-due.de/oer/oai%22%0A%7C+open-oaipmh%28metadataPrefix%3D%22mods%22%29%0A%7C+decode-xml%0A%7C+handle-generic-xml%0A%7C+encode-json%28prettyPrinting%3D%22true%22%29%0A%7C+print%3B&fix=copy_field%28%22preferredName%22%2C+%22name%22%29%0Acopy_field%28%22affiliation%5B%5D.1.label%22%2C+%22placeOfWork%22%29%0Aretain%28%22name%22%2C+%22placeOfWork%22%29&active-editor=fix
1 change: 1 addition & 0 deletions resources/examples/PG-DATA_formeta_to_JSON_(fix)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost:8280/?data=1%7Ba%3A+Faust%2C+b+%7Bn%3A+Goethe%2C+v%3A+JW%7D%2C+c%3A+Weimar%7D%0A2%7Ba%3A+R%C3%A4uber%2C+b+%7Bn%3A+Schiller%2C+v%3A+F%7D%2C+c%3A+Weimar%7D&flux=PG_DATA%0A%7Cas-lines%0A%7Cdecode-formeta%0A%7Cfix%0A%7Cencode-xml%28rootTag%3D%22collection%22%29%0A%7Cprint%0A%3B&fix=move_field%28_id%2C+id%29%0Amove_field%28a%2C+title%29%0Apaste%28author%2C+b.v%2C+b.n%2C+%27~aus%27%2C+c%29%0Aretain%28id%2C+title%2C+author%29&morph=&active-editor=fix
1 change: 1 addition & 0 deletions resources/examples/PG-DATA_formeta_to_JSON_(morph)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost:8280/?data=1%7Ba%3A+Faust%2C+b+%7Bn%3A+Goethe%2C+v%3A+JW%7D%2C+c%3A+Weimar%7D%0A2%7Ba%3A+R%C3%A4uber%2C+b+%7Bn%3A+Schiller%2C+v%3A+F%7D%2C+c%3A+Weimar%7D&flux=PG_DATA%0A%7Cas-lines%0A%7Cdecode-formeta%0A%7Cmorph%0A%7Cencode-xml%28rootTag%3D%22collection%22%29%0A%7Cprint%0A%3B&fix=&morph=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3Cmetamorph+xmlns%3D%22http%3A//www.culturegraph.org/metamorph%22+xmlns%3Axsi%3D%22http%3A//www.w3.org/2001/XMLSchema-instance%22%0A%09version%3D%221%22%3E%0A%09%3Crules%3E%0A%09%09%3Cdata+source%3D%22_id%22+name%3D%22id%22/%3E%0A%09%09%3Cdata+source%3D%22a%22+name%3D%22title%22/%3E%0A%09%09%3Ccombine+value%3D%22%24%7Bfirst%7D+%24%7Blast%7D+aus+%24%7Bplace%7D%22+name%3D%22author%22%3E%0A%09%09%09%3Cdata+source%3D%22b.v%22+name%3D%22first%22+/%3E%0A%09%09%09%3Cdata+source%3D%22b.n%22+name%3D%22last%22+/%3E%0A%09%09%09%3Cdata+source%3D%22c%22+name%3D%22place%22+/%3E%0A%09%09%3C/combine%3E%0A%09%3C/rules%3E%0A%3C/metamorph%3E%0A&active-editor=morph
16 changes: 15 additions & 1 deletion src/clj/metafacture_playground/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[ring.middleware.params :refer [wrap-params]]
[ring.util.request :refer [body-string]]
[clojure.data.json :as json]
[clojure.stacktrace :as st]))
[clojure.stacktrace :as st]
[clojure.java.io :as io]))

(defn wrap-body-string [handler]
(fn [request]
Expand All @@ -34,6 +35,19 @@
(GET "/" [] (resource-response "index.html" {:root "public"}))
(GET "/process" [data flux fix morph uri]
(process-request data flux fix morph uri))
(GET "/examples" request
(try
(let [files (-> (io/file "resources/examples/")
file-seq
rest)
files-content (reduce
(fn [result file]
(assoc result (.getName file) (slurp file)))
{}
files)]
(response (json/write-str files-content)))
(catch Exception e
(exception-handler e (:uri request)))))
(POST "/process" request
(let [body (-> request :body (json/read-str :key-fn keyword))
{:keys [data flux fix morph]} body]
Expand Down
29 changes: 0 additions & 29 deletions src/cljs/metafacture_playground/db.cljs
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
(ns metafacture-playground.db)

; 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 "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"
"\tversion=\"1\">\n"
"\t<rules>\n"
"\t\t<data source=\"_id\" name=\"id\"/>\n"
"\t\t<data source=\"a\" name=\"title\"/>\n"
"\t\t<combine value=\"${first} ${last} aus ${place}\" name=\"author\">\n"
"\t\t\t<data source=\"b.v\" name=\"first\" />\n"
"\t\t\t<data source=\"b.n\" name=\"last\" />\n"
"\t\t\t<data source=\"c\" name=\"place\" />\n"
"\t\t</combine>\n"
"\t</rules>\n"
"</metamorph>\n")})

(def sample-fields
{:data {:content (:data sample-data)
:collapsed? false}
:flux {:content (:flux-with-fix sample-data)
:collapsed? false}
:fix {:content (:fix sample-data)}
:morph {:content (:morph sample-data)}
:switch {:collapsed? false
:active :fix}})

(def default-db
{:input-fields {:data {:content nil
:collapsed? false
Expand Down
78 changes: 50 additions & 28 deletions src/cljs/metafacture_playground/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
(:require
[re-frame.core :as re-frame]
[day8.re-frame.fetch-fx]
[lambdaisland.uri :refer [uri join assoc-query* query-string->map query-encode]]
[lambdaisland.uri :refer [uri join assoc-query* query-encode]]
[metafacture-playground.db :as db]
[metafacture-playground.effects :as effects]
[metafacture-playground.utils :as utils]
[com.degel.re-frame.storage]
[clojure.string :as clj-str]
[cognitect.transit :as transit]))
Expand Down Expand Up @@ -153,23 +154,17 @@
::edit-input-value
edit-value)

(defn- add-sample [db sample]
(reduce
(fn [db [k sample-v]]
(assoc-in db [:input-fields k] sample-v))
db
sample))

(defn load-sample
[{db :db} [_ sample]]
{:db (add-sample db sample)
:storage/set {:session? true
:pairs (-> {:input-fields (update-in sample [:switch :active] name)}
generate-pairs)}
:dispatch-n (mapv
(fn [editor]
[::update-width editor (get-in sample [editor :content])])
[:data :flux :fix])})
(let [active-editor (when (:active-editor sample)
(-> sample :active-editor keyword))]
{:db db
:dispatch-n (conj
(mapv
(fn [editor]
[::edit-input-value editor (get sample editor "")])
[:data :flux :fix :morph])
[::switch-editor active-editor])}))

(re-frame/reg-event-fx
::load-sample
Expand Down Expand Up @@ -212,11 +207,13 @@

(defn switch-editor
[{db :db} [_ editor]]
{:db (assoc-in db [:input-fields :switch :active] editor)
:dispatch [::update-width editor (get-in db [:input-fields editor :content])]
:storage/set {:session? true
:name (->storage-key [:input-fields :switch :active])
:value (name editor)}})
(merge
{:db (assoc-in db [:input-fields :switch :active] editor)
:storage/set {:session? true
:name (->storage-key [:input-fields :switch :active])
:value (when editor (name editor))}}
(when editor
{:dispatch [::update-width editor (get-in db [:input-fields editor :content])]})))

(re-frame/reg-event-fx
::switch-editor
Expand Down Expand Up @@ -373,6 +370,29 @@

;;; Initialize-db

(defn examples-response
[{db :db} [_ {:keys [body]}]]
(let [body (transit/read (transit/reader :json) body)]
{:db (assoc db :examples body)}))

(re-frame/reg-event-fx
::examples-response
examples-response)

(defn load-samples
[{db :db} _]
{:db db
:fetch {:method :get
:url "examples"
:timeout 10000
:response-content-types {#"application/.*json" :json}
:on-success [::examples-response]
:on-failure [::bad-response]}})

(re-frame/reg-event-fx
::load-samples
load-samples)

(defn deep-merge [a & maps]
(if (map? a)
(apply merge-with deep-merge a maps)
Expand All @@ -389,19 +409,21 @@
(defn initialize-db
[{[_ href window-height] :event
web-storage :storage/all}]
(let [query-params (-> href uri :query query-string->map)]
(let [query-params (utils/parse-url href)]
(if (empty? query-params)
{:db (deep-merge
db/default-db
(restore-db web-storage)
{:ui {:height window-height}})}
{:ui {:height window-height}})
:dispatch [::load-samples]}
{:db (-> db/default-db
(assoc-query-params query-params)
(assoc-in [:ui :height] window-height))
:dispatch-n (mapv
(fn [editor]
[::update-width editor (get query-params editor)])
[:data :flux (-> query-params :active-editor keyword)])
:dispatch-n (conj
(mapv
(fn [editor]
[::edit-input-value editor (get query-params editor "")])
[:data :flux :fix :morph])
[::load-samples])
:storage/set {:session? true
:pairs (-> (assoc-query-params {} query-params)
generate-pairs)}
Expand Down
15 changes: 14 additions & 1 deletion src/cljs/metafacture_playground/subs.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns metafacture-playground.subs
(:require
[re-frame.core :as re-frame]
[clojure.string :as clj-str]))
[clojure.string :as clj-str]
[metafacture-playground.utils :as utils]))

(defn- expand-indentation [details]
(when details
Expand All @@ -20,6 +21,18 @@
(fn [db _]
(get-in db [:message :show-details?])))

(defn- display-name [str]
(clj-str/replace str "_" " "))

(re-frame/reg-sub
::examples
(fn [db _]
(into {}
(map (fn [[k v]]
{k {:display-name (display-name k)
:value (utils/parse-url v)}}))
(get db :examples))))

(re-frame/reg-sub
::field-value
(fn [db [_ field-name]]
Expand Down
6 changes: 6 additions & 0 deletions src/cljs/metafacture_playground/utils.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns metafacture-playground.utils
(:require
[lambdaisland.uri :refer [uri query-string->map]]))

(defn parse-url [href]
(-> href uri :query query-string->map))
36 changes: 30 additions & 6 deletions src/cljs/metafacture_playground/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[re-frame.core :as re-frame]
[metafacture-playground.subs :as subs]
[metafacture-playground.events :as events]
[metafacture-playground.db :as db]
[clojure.string :as clj-str]
[lambdaisland.uri :refer [uri]]
[cljsjs.semantic-ui-react]
Expand All @@ -26,6 +25,7 @@
(apply g/getValueByKeys semantic-ui k ks)
(goog.object/get semantic-ui k)))

(def button-group (component "Button" "Group"))
(def button (component "Button"))
(def header (component "Header"))
(def container (component "Container"))
Expand All @@ -44,6 +44,9 @@
(def message (component "Message"))
(def menu (component "Menu"))
(def menu-item (component "Menu" "Item"))
(def dropdown (component "Dropdown"))
(def dropdown-menu (component "Dropdown" "Menu"))
(def dropdown-item (component "Dropdown" "Item"))

;;; Using monaco editor react component

Expand Down Expand Up @@ -107,14 +110,16 @@
:for for}
(clj-str/capitalize name)])

(defn simple-button [{:keys [content dispatch-fn icon-name fluid]}]
(defn simple-button [{:keys [content dispatch-fn icon-name fluid style]}]
[:> button
(merge {:id (-> content (clj-str/replace " " "-") (str "-button"))
:basic basic-buttons?
:color color
:fluid fluid}
(when dispatch-fn
{:onClick #(re-frame/dispatch dispatch-fn)}))
{:onClick #(re-frame/dispatch dispatch-fn)})
(when style
{:style style}))
(when icon-name
[:> icon {:name icon-name}])
content])
Expand Down Expand Up @@ -177,6 +182,22 @@

;;; Control Panel

(defn examples-dropdown []
[:> dropdown
{;:className "icon"
:button true
;:icon "code"
;:labeled true
:text "Load example"}
[:> dropdown-menu
(for [[k {:keys [display-name value]}] @(re-frame/subscribe [::subs/examples])]
^{:key k}
[:> dropdown-item
{:key k
:text display-name
:value display-name
:on-click #(re-frame/dispatch [::events/load-sample value])}])]])

(defn process-button []
(let [data (re-frame/subscribe [::subs/field-value :data])
flux (re-frame/subscribe [::subs/field-value :flux])
Expand All @@ -190,7 +211,8 @@
:on "hover"
:trigger (reagent/as-element (simple-button {:content "Process"
:dispatch-fn [::events/process @data @flux @fix @morph @active-editor]
:icon-name "play"}))
:icon-name "play"
:style {:margin-left "0.2em"}}))
:position "bottom left"}]))

(defn share-link [link-type label-text]
Expand Down Expand Up @@ -234,8 +256,10 @@

(defn control-panel []
[:> segment {:raised true}
[simple-button {:content "Load sample" :dispatch-fn [::events/load-sample db/sample-fields] :icon-name "code"}]
[simple-button {:content "Clear all" :dispatch-fn [::events/clear-all] :icon-name "erase"}]
[:> button-group {:color color
:basic true}
[examples-dropdown]
[simple-button {:content "Clear" :dispatch-fn [::events/clear-all] :icon-name "erase"}]]
[process-button]
[share-button]])

Expand Down
Loading

0 comments on commit b72b378

Please sign in to comment.