Skip to content

Commit

Permalink
Allow to specify an eval mode via ':nextjournal.clerk/eval' setting
Browse files Browse the repository at this point in the history
with values :skip, :sci, :jvm (default)
  • Loading branch information
zampino committed Jan 12, 2024
1 parent 464d8fb commit b12aad2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/nextjournal/clerk/eval.clj
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@
{:as evaluated-doc :keys [blob-ids]}
(reduce (fn [state cell]
(when (and (parser/code? cell) set-status-fn)
(set-status-fn (->eval-status analyzed-doc (count (filter parser/code? (:blocks state))) cell)))
(set-status-fn (->eval-status analyzed-doc (count (filter parser/eval? (:blocks state))) cell)))
(let [state-with-deref-deps-evaluated (analyzer/hash-deref-deps state cell)
{:as result :nextjournal/keys [blob-id]} (when (parser/code? cell)
(read+eval-cached state-with-deref-deps-evaluated cell))]
{:as result :nextjournal/keys [blob-id]}
(cond
(parser/eval? cell)
(read+eval-cached state-with-deref-deps-evaluated cell)
(parser/sci-eval? cell)
{:nextjournal/value (v/->viewer-eval (:form cell))})]
(cond-> (update state-with-deref-deps-evaluated :blocks conj (cond-> cell result (assoc :result result)))
blob-id (update :blob-ids conj blob-id)
blob-id (assoc-in [:blob->result blob-id] result))))
Expand Down
9 changes: 9 additions & 0 deletions src/nextjournal/clerk/parser.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#{:nextjournal.clerk/auto-expand-results?
:nextjournal.clerk/budget
:nextjournal.clerk/css-class
:nextjournal.clerk/eval
:nextjournal.clerk/render-opts
:nextjournal.clerk/page-size
:nextjournal.clerk/render-evaluator
Expand Down Expand Up @@ -200,6 +201,14 @@
(defn code? [{:as block :keys [type]}]
(contains? #{:code} type))

(def eval?
(every-pred code?
(complement (comp #{:sci :skip} :nextjournal.clerk/eval :settings))))

(def sci-eval?
(every-pred code?
(comp #{:sci} :nextjournal.clerk/eval :settings)))

(defn merge-settings [current-settings new-settings]
(-> (merge-with merge current-settings (select-keys new-settings [:nextjournal.clerk/visibility]))
(merge (dissoc new-settings :nextjournal.clerk/visibility))))
Expand Down
18 changes: 9 additions & 9 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,18 @@
#_(ensure-wrapped 123)
#_(ensure-wrapped {:nextjournal/value 456})

(defn get-safe
([key] #(get-safe % key))
([map key]
(when (map? map)
(try (get map key) ;; can throw for e.g. sorted-map
(catch #?(:clj Exception :cljs js/Error) _e nil)))))

(defn ->value
"Takes `x` and returns the `:nextjournal/value` from it, or otherwise `x` unmodified."
[x]
(if (wrapped-value? x)
(:nextjournal/value x)
(get-safe x :nextjournal/value)
x))

#_(->value (with-viewer `code-viewer '(+ 1 2 3)))
Expand Down Expand Up @@ -241,13 +248,6 @@

#_(->> "x^2" (with-viewer `latex-viewer) (with-viewers [{:name `latex-viewer :render-fn `mathjax-viewer}]))

(defn get-safe
([key] #(get-safe % key))
([map key]
(when (map? map)
(try (get map key) ;; can throw for e.g. sorted-map
(catch #?(:clj Exception :cljs js/Error) _e nil)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; table viewer normalization

Expand Down Expand Up @@ -563,7 +563,7 @@
{:code? (not= :hide code)
:result? (and (:result cell)
(or (not= :hide result)
(-> cell :result :nextjournal/value (get-safe :nextjournal/value) viewer-eval?)))}))
(-> cell :result ->value ->value viewer-eval?)))}))

(defn hidden-viewer-eval-result? [{:keys [settings result]}]
(and (= :hide (-> settings :nextjournal.clerk/visibility :result))
Expand Down

0 comments on commit b12aad2

Please sign in to comment.