Skip to content

Commit

Permalink
q inside show-sci, remove babashka dep (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie authored Apr 17, 2023
1 parent c4558be commit 9da2761
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pom.xml.asc
.lein-*
.nrepl-*
.DS_Store
.cljs_node_repl

.hgignore
.hg/
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## [unreleased]

## [0.5.0]

- #28:

- Removes the `babashka` dependency from `mentat.clerk-utils.build`

- `mentat.clerk-utils.show/q` now applies to the forms passed to `show-sci`,
allowing for value-splicing, namespace resolution etc inside of `show-sci`.

- #24 renames the clj-kondo hooks to have extension `.clj_kondo`, for the same
reason as #23.

Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
;; ## Variables

(def lib 'org.mentat/clerk-utils)
(def version "0.4.1")
(def version "0.5.0")
(def pom-deps
{'io.github.nextjournal/clerk
{:mvn/version "0.12.707"
Expand Down
14 changes: 14 additions & 0 deletions dev/clerk_utils/notebook.clj
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,20 @@ clojure -Sdeps '{:deps {io.github.mentat-collective/clerk-utils {:git/sha \"%s\"
(show-sci
[:pre (exclaim "Still here")])

;; `show-sci` makes use of the [`q` macro](#q-macro-for-viewers) internally, so
;; you can do thing like splice values in from your Clojure environment:

(let [defaults {:key "value"}
entries [1 2 3]]
(show-sci
[nextjournal.clerk.viewer/inspect
;; The form passed to `inspect` uses values spliced in from the CLJ-side
;; `let` form above.
[~defaults
[~@entries 4]]]))

;; See the [`q` macro section](#q-macro-for-viewers) for more detail.

;; ### Client / Server Example
;;
;; Annotate a `var` definition bound to an atom with `^{::clerk/sync true}` to
Expand Down
8 changes: 5 additions & 3 deletions src/mentat/clerk_utils/build.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
(ns mentat.clerk-utils.build
"Versions of `nextjournal.clerk/{build!,serve!,halt!} that support custom CLJS
compilation.`"
(:require [babashka.fs :as fs]
(:require [clojure.java.io :as io]
[mentat.clerk-utils.docs :refer [git-sha]]
[mentat.clerk-utils.build.shadow :as shadow]
[nextjournal.clerk :as clerk]
[nextjournal.clerk.config :as config]
[nextjournal.clerk.view]
[nextjournal.clerk.viewer :as cv]))
[nextjournal.clerk.viewer :as cv])
(:import (java.nio.file Files)))

;; ## Viewer JS Utilities

Expand Down Expand Up @@ -125,7 +126,8 @@
(if-not (seq cljs-namespaces)
@!build
(let [js-path (shadow/release! cljs-namespaces)
cas (->> (fs/read-all-bytes js-path)
cas (->> (.toPath (io/file js-path))
(Files/readAllBytes)
(cv/store+get-cas-url!
{:out-path out-path
:ext "js"}))]
Expand Down
26 changes: 15 additions & 11 deletions src/mentat/clerk_utils/show.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"Show utilities for Clerk."
(:require [applied-science.js-interop :as j]
[clojure.walk :as walk]
#?(:clj [mentat.clerk-utils.viewers :refer [q]])
[nextjournal.clerk #?(:clj :as :cljs :as-alias) clerk])
#?(:cljs
(:require-macros mentat.clerk-utils.show)))
Expand All @@ -17,18 +18,21 @@
Works in both `clj` and `cljs` contexts; in `cljs` this is equivalent to
`clojure.core/comment`."
[& exprs]
[& #?(:clj exprs :cljs _exprs)]
(when-not (:ns &env)
`(clerk/with-viewer
{:transform-fn clerk/mark-presented
:render-fn
'(fn [_#]
(let [result# (do ~@exprs)]
(nextjournal.clerk.viewer/html
(if (vector? result#)
result#
[nextjournal.clerk.render/inspect result#]))))}
{})))
#?(:clj
`(clerk/with-viewer
{:transform-fn clerk/mark-presented
:render-fn
(q
(fn [_#]
(let [result# (do ~@exprs)]
(nextjournal.clerk.viewer/html
(if (vector? result#)
result#
[nextjournal.clerk.render/inspect result#])))))}
{})
:cljs nil)))

;; ## show-cljs macro
;;
Expand Down

0 comments on commit 9da2761

Please sign in to comment.