Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify & clean up viewers #53

Merged
merged 10 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions notebooks/dice.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

;; Here, we define a viewer using hiccup that will the dice as well as a button. Note that this button has an `:on-click` event handler that uses `v/clerk-eval` to tell Clerk to evaluate the argument, in this cases `(roll!)` when clicked.
^::clerk/no-cache
(clerk/with-viewer (fn [side]
(v/html [:div.text-center
(when side
[:div.mt-2 {:style {:font-size "6em"}} side])
[:button.bg-blue-500.hover:bg-blue-700.text-white.font-bold.py-2.px-4.rounded
{:on-click (fn [e] (v/clerk-eval '(roll!)))} "Roll 🎲!"]]))
(clerk/with-viewer '(fn [side]
(v/html [:div.text-center
(when side
[:div.mt-2 {:style {:font-size "6em"}} side])
[:button.bg-blue-500.hover:bg-blue-700.text-white.font-bold.py-2.px-4.rounded
{:on-click (fn [e] (v/clerk-eval '(roll!)))} "Roll 🎲!"]]))
@dice)

;; Our roll! function `resets!` our `dice` with a random side and prints and says the result. Finally it updates the notebook.
Expand Down
8 changes: 4 additions & 4 deletions notebooks/rule_30.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
(:require [nextjournal.clerk :as clerk]))

(clerk/set-viewers!
[{:pred number? :render-fn #(v/html [:div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}
{:pred list? :render-fn #(v/html (into [:div.flex.flex-col] (v/inspect-children %2) %1))}
{:pred #(and (vector? %) (not (map-entry? %))) :render-fn #(v/html (into [:div.flex.inline-flex] (v/inspect-children %2) %1))}])
[{:pred number? :render-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}
{:pred list? :render-fn '#(v/html (into [:div.flex.flex-col] (v/inspect-children %2) %1))}
{:pred #(and (vector? %) (not (map-entry? %))) :render-fn '#(v/html (into [:div.flex.inline-flex] (v/inspect-children %2) %1))}])

0

Expand Down
20 changes: 10 additions & 10 deletions notebooks/viewer_api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
expression-2)))

;; ## 🚀 Extensibility
(clerk/with-viewer #(v/html [:div "Greetings to " [:strong %] "!"])
"James Maxwell Clerk")
(clerk/with-viewer '#(v/html [:div "Greetings to " [:strong %] "!"])
"James Clerk Maxwell")

(clerk/with-viewers [{:pred number? :render-fn #(v/html [:div.inline-block [(keyword (str "h" %)) (str "Heading " %)]])}]
(clerk/with-viewers [{:pred number? :render-fn '#(v/html [:div.inline-block [(keyword (str "h" %)) (str "Heading " %)]])}]
[1 2 3 4 5])

^::clerk/no-cache
(clerk/with-viewers [{:pred number? :render-fn #(v/html [:div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}]
(clerk/with-viewers [{:pred number? :render-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-black")}])}]
(take 10 (repeatedly #(rand-int 2))))

(clerk/with-viewers
Expand All @@ -67,11 +67,11 @@
(str "(?i)"
"(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|"
"(rgb|hsl)a?\\((-?\\d+%?[,\\s]+){2,3}\\s*[\\d\\.]+%?\\))")) %))
:render-fn #(v/html [:div.inline-block.rounded-sm.shadow
{:style {:width 16
:height 16
:border "1px solid rgba(0,0,0,.2)"
:background-color %}}])}]
:render-fn '#(v/html [:div.inline-block.rounded-sm.shadow
{:style {:width 16
:height 16
:border "1px solid rgba(0,0,0,.2)"
:background-color %}}])}]
["#571845"
"rgb(144,12,62)"
"rgba(199,0,57,1.0)"
Expand Down
2 changes: 1 addition & 1 deletion notebooks/viewers/image.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(clerk/set-viewers! [{:pred bytes?
:fetch-fn (fn [_ bytes] {:nextjournal/content-type "image/png"
:nextjournal/value bytes})
:render-fn (fn [blob] (v/html [:img {:src (v/url-for blob)}]))}])
:render-fn '(fn [blob] (v/html [:img {:src (v/url-for blob)}]))}])

(.. (HttpClient/newHttpClient)
(send (.build (HttpRequest/newBuilder (URI. "https://upload.wikimedia.org/wikipedia/commons/5/57/James_Clerk_Maxwell.png")))
Expand Down
26 changes: 4 additions & 22 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -239,28 +239,10 @@
(def table #'v/table)
(def use-headers #'v/use-headers)
(def hide-result #'v/hide-result)
(defn doc-url [path] (v/->SCIEval (list 'v/doc-url path)))

(defmacro with-viewer
[viewer x]
(let [viewer# (v/->Form viewer)]
`(v/with-viewer* ~viewer# ~x)))

#_(macroexpand '(with-viewer #(v/html [:div %]) 1))

(defmacro with-viewers
[viewers x]
(let [viewers# (v/process-fns viewers)]
`(v/with-viewers* ~viewers# ~x)))

#_(macroexpand '(with-viewers [{:pred number? :render-fn #(v/html [:div %])}] 1))


(defmacro set-viewers!
([viewers] (v/set-viewers!* *ns* viewers))
([scope viewers] (v/set-viewers!* scope viewers)))

#_(set-viewers! [])
(def doc-url #'v/doc-url)
(def with-viewer #'v/with-viewer)
(def with-viewers #'v/with-viewers)
(def set-viewers! #'v/set-viewers!)

(defn file->viewer
"Evaluates the given `file` and returns it's viewer representation."
Expand Down
22 changes: 11 additions & 11 deletions src/nextjournal/clerk/sci_viewer.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[edamame.core :as edamame]
[goog.object]
[goog.string :as gstring]
[nextjournal.clerk.viewer :as viewer :refer [code html md plotly tex vl with-viewer* with-viewers*] :rename {with-viewer* with-viewer with-viewers* with-viewers}]
[nextjournal.clerk.viewer :as viewer :refer [code html md plotly tex vl with-viewer with-viewers]]
[nextjournal.devcards :as dc]
[nextjournal.markdown.transform :as md.transform]
[nextjournal.viewer.code :as code]
Expand Down Expand Up @@ -95,8 +95,8 @@
:read-cond :allow
:readers {'file (partial with-viewer :file)
'object (partial with-viewer :object)
'function+ viewer/form->fn+form
'sci-eval viewer/sci-eval}
'viewer-fn viewer/->viewer-fn
'viewer-eval *eval*}
:features #{:clj}}))

(defn ^:export read-string [s]
Expand Down Expand Up @@ -415,7 +415,7 @@

(defn render-with-viewer [{:as opts :keys [viewers]} viewer value]
#_(js/console.log :render-with-viewer {:value value :viewer viewer #_#_ :opts opts})
(cond (or (fn? viewer) (viewer/fn+form? viewer))
(cond (or (fn? viewer) (viewer/viewer-fn? viewer))
(viewer value opts)

(and (map? viewer) (:render-fn viewer))
Expand Down Expand Up @@ -491,7 +491,7 @@
id (get id))))

(defn error-viewer [e]
(viewer/with-viewer* :code (pr-str e)))
(viewer/with-viewer :code (pr-str e)))


(dc/defcard inspect-values
Expand Down Expand Up @@ -633,7 +633,7 @@

(dc/defcard eval-viewer
"Viewers that are lists are evaluated using sci."
[inspect (with-viewer (viewer/form->fn+form '(fn [x] (v/html [:h3 "Ohai, " x "! 👋"]))) "Hans")])
[inspect (with-viewer (viewer/->viewer-fn '(fn [x] (v/html [:h3 "Ohai, " x "! 👋"]))) "Hans")])


(dc/defcard notebook
Expand Down Expand Up @@ -662,11 +662,11 @@
[inspect {:path []
:viewers
[{:pred number?
:render-fn (viewer/form->fn+form '#(v/html [:div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-
:render-fn (viewer/->viewer-fn '#(v/html [:div.inline-block {:style {:width 16 :height 16}
:class (if (pos? %) "bg-black" "bg-white border-solid border-2 border-
black")}]))}
{:pred vector? :render-fn (viewer/form->fn+form '#(v/html (into [:div.flex.inline-flex] (v/inspect-children %2) %1)))}
{:pred list? :render-fn (viewer/form->fn+form '#(v/html (into [:div.flex.flex-col] (v/inspect-children %2) %1)))}]}
{:pred vector? :render-fn (viewer/->viewer-fn '#(v/html (into [:div.flex.inline-flex] (v/inspect-children %2) %1)))}
{:pred list? :render-fn (viewer/->viewer-fn '#(v/html (into [:div.flex.flex-col] (v/inspect-children %2) %1)))}]}
'([0 1 0] [1 0 1])])

(dc/defcard clj-long
Expand Down Expand Up @@ -971,5 +971,5 @@ black")}]))}
(set! *eval* eval-form)

(swap! viewer/!viewers (fn [viewers]
(-> (into {} (map (juxt key (comp viewer/process-fns val))) viewers)
(-> (into {} (map (juxt key (comp #(into [] (map viewer/process-render-fn) %) val))) viewers)
(update :root concat js-viewers))))
Loading