diff --git a/deps.edn b/deps.edn index 2129b15..558b1c3 100644 --- a/deps.edn +++ b/deps.edn @@ -34,7 +34,7 @@ ;; `env CLOJARS_USERNAME= CLOJARS_PASSWORD= clojure -T:build deploy` {:deps {io.github.clojure/tools.build {:git/tag "v0.10.4" :git/sha "31388ff"} slipset/deps-deploy {:mvn/version "0.2.2"} - org.scicloj/clay {:mvn/version "2-beta15"} + org.scicloj/clay {:mvn/version "2-beta17"} ; for the build ;; We need wolframite + JLink to render Wolf code in the docs org.scicloj/wolframite {:local/root "."} wolfram/jlink {:local/root "./symlink-jlink.jar"}} ; FYI jlink only needed for build-site diff --git a/src/wolframite/core.clj b/src/wolframite/core.clj index dded77b..3f082ee 100644 --- a/src/wolframite/core.clj +++ b/src/wolframite/core.clj @@ -41,6 +41,7 @@ [wolframite.base.package :as package] [wolframite.base.parse :as parse] [wolframite.impl.jlink-instance :as jlink-instance] + [wolframite.impl.kindly-support :as kindly-support] [wolframite.impl.protocols :as proto] [wolframite.runtime.defaults :as defaults] [wolframite.runtime.jlink :as jlink] @@ -192,7 +193,8 @@ (:opts jlink-inst) eval-opts) expr' (if (string? expr) (express/express expr with-eval-opts) expr)] - (cep/cep expr' with-eval-opts)) + (some-> (cep/cep expr' with-eval-opts) + kindly-support/maybe-add-kindly-meta)) (throw (IllegalStateException. "Not initialized, call start! first"))))) ;; TODO Should we expose this, or will just folks shoot themselves in the foot with it? diff --git a/src/wolframite/impl/kindly_support.clj b/src/wolframite/impl/kindly_support.clj new file mode 100644 index 0000000..1c092c4 --- /dev/null +++ b/src/wolframite/impl/kindly_support.clj @@ -0,0 +1,20 @@ +(ns wolframite.impl.kindly-support + "Add Kindly annotations to eval results to improve their display in compatible tools, such as Clay") + +(defn- head [expr] + (when (list? expr) + (first expr))) + +(defn- video->url + "Extract the url from `(Video \"url..\" ...)`" + [expr] + {:pre [(= (head expr) 'Video)]} + (second expr)) + +(defn maybe-add-kindly-meta [expr] + {:pre [expr]} + (->> (cond + (= (head expr) 'Video) + {:kind/video true + :kindly/options {:kindly/f video->url}}) + (with-meta expr))) \ No newline at end of file diff --git a/test/wolframite/core_test.clj b/test/wolframite/core_test.clj index 680efff..17b9017 100644 --- a/test/wolframite/core_test.clj +++ b/test/wolframite/core_test.clj @@ -124,6 +124,18 @@ (wl/eval "Map[Function[{x}, x + 1],{1,3}]") (wl/eval (w/Map (w/fn [x] (w/+ x 1)) [1 3])))))) +(deftest kindly-support + (wl/start!) + (let [res (wl/eval '(Video "file:///my-fake.mps")) + {:keys [kindly/options] :as m} + (meta res) + view-fn (:kindly/f options)] + (is (= 'Video (first res)) "Sanity check: expect Wolfram to return (Video ..)") + (is (:kind/video m)) + (is (= "file:///my-fake.mps" (view-fn res)) + "Correct metadata is attached, including view fn to extract the video url"))) + + (comment (wl/->wl (w/Map (w/fn [] (w/+ (w/Slot 1) 1)) [1 3])) (wl/eval (w/Map (w/fn [x] (w/Plus x 1)) [1 2 3]))