Skip to content

Clerk in Babashka #232

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

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ff2ef75
First sketch of Clerk in Babashka
zampino Oct 18, 2022
36ba59c
Fix bb task
zampino Oct 18, 2022
066161f
Require latest babashka
zampino Oct 18, 2022
ed9b560
clean bb deps
zampino Oct 19, 2022
baa9660
Rely on latest bb for namespace instance check
zampino Oct 19, 2022
4fc3526
Move stubs to bb files
zampino Oct 19, 2022
1493d7b
Pass cli args to bb task
zampino Oct 19, 2022
8406591
Fix add-viewers! and auto-resolve of ::alias keywords
zampino Oct 19, 2022
e5ce7ab
Extract var from def
zampino Oct 19, 2022
a4b3738
Fix static builder in bb runtime
zampino Oct 19, 2022
a642887
Fix markdown viewer for cljs consumption
zampino Oct 19, 2022
b6bd4c4
Add build bb task
zampino Oct 19, 2022
7c95f89
Implement no-cache?
zampino Oct 20, 2022
995e264
Parse markdown via quickjs
zampino Oct 20, 2022
865c088
Check quickjs is available
zampino Oct 20, 2022
38997c8
Lazily copy markdown js module
zampino Oct 20, 2022
10e95c4
More escapes
zampino Oct 20, 2022
a5312a1
Merge branch 'main' into bb-clerk
borkdude May 6, 2025
ef77ecc
bypass editscript
borkdude May 6, 2025
b39c34b
dejavu
borkdude May 6, 2025
6ce1ddb
bypass nippy
borkdude May 6, 2025
9b711cd
wip [skip ci]
borkdude May 6, 2025
b219f09
more stuff
borkdude May 6, 2025
86e0d5d
more
borkdude May 6, 2025
e34c6bd
more
borkdude May 6, 2025
1fe803b
wip
borkdude May 6, 2025
6cc60fc
always-array-map is challenge
borkdude May 6, 2025
48dd946
wip
borkdude May 6, 2025
92c6676
kondo
borkdude May 6, 2025
e1f15d2
wip [skip ci]
borkdude May 12, 2025
0a907a0
wip
borkdude May 12, 2025
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
3 changes: 2 additions & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
taoensso.encore/defonce clojure.core/defonce
taoensso.encore/defalias clojure.core/def
promesa.core/let clojure.core/let
shadow.cljs.modern/defclass clojure.core/defprotocol}
shadow.cljs.modern/defclass clojure.core/defprotocol
nextjournal.clerk.utils/if-bb clojure.core/if}
:linters {:clojure-lsp/unused-public-var {:level :off}
:consistent-alias {:aliases {datomic.api datomic
clojure.string str
Expand Down
31 changes: 31 additions & 0 deletions bb-runtime.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{:min-bb-version "1.12.200"
:paths ["src" "notebooks" "resources"]
:deps {io.github.nextjournal/dejavu {:git/sha "4980e0cc18c9b09fb220874ace94ba6b57a749ca"}
io.github.nextjournal/cas-client {:git/sha "d9f838937ebc8b645fe5764949e72a6df8e344de"}
mvxcvi/multiformats {:git/url "https://github.com/greglook/clj-multiformats"
:git/sha "1189f1fb26db180cd8dcfd50518cdf553c0ff9e1"}
hiccup/hiccup {:mvn/version "2.0.0-alpha2"}
io.github.nextjournal/markdown {:mvn/version "0.6.157"}
io.github.nextjournal/clerk-slideshow {:git/sha "562f634494a1e1a9149ed78d5d39fd9486cc00ba"}
weavejester/dependency {:mvn/version "0.2.1"}
io.github.babashka/sci.nrepl {:mvn/version "0.0.2"}}
:tasks
{dev
{:requires ([babashka.fs :as fs]
[babashka.nrepl.server :as srv]
[nextjournal.clerk :as clerk]
[babashka.cli :as cli])
:task (do (srv/start-server! {:host "localhost" :port 1339})
(spit ".nrepl-port" "1339")
(nextjournal.clerk/serve! (cli/parse-opts *command-line-args*))
(-> (Runtime/getRuntime)
(.addShutdownHook (Thread. (fn []
(nextjournal.clerk/halt!)
(fs/delete ".nrepl-port")))))
(deref (promise)))}

build
{:requires ([nextjournal.clerk :as clerk]
[babashka.cli :as cli])
:task (let [spec (-> (resolve 'nextjournal.clerk/build!) meta :org.babashka/cli)]
(clerk/build! (cli/parse-opts *command-line-args* spec)))}}}
5 changes: 5 additions & 0 deletions src/nextjournal/beholder.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns nextjournal.beholder
"Babashka runtime no-op stubs")

(defn watch [cb & args] nil)
(defn stop [w] nil)
105 changes: 55 additions & 50 deletions src/nextjournal/clerk/always_array_map.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns nextjournal.clerk.always-array-map
"A persistent data structure that is based on array-map, but doesn't turn into a hash-map by using assoc etc.
Prints like a normal Clojure map in the order of insertion.")
Prints like a normal Clojure map in the order of insertion."
(:require [nextjournal.clerk.utils :as utils]))

(set! *warn-on-reflection* true)

Expand All @@ -9,72 +10,76 @@

(declare ->AlwaysArrayMap)

(deftype AlwaysArrayMap [^clojure.lang.PersistentArrayMap the-map]
clojure.lang.ILookup
(valAt [_ k]
(get the-map k))
(utils/if-bb
nil
(deftype AlwaysArrayMap [^clojure.lang.PersistentArrayMap the-map]
clojure.lang.ILookup
(valAt [_ k]
(get the-map k))

clojure.lang.Seqable
(seq [_]
(seq the-map))
clojure.lang.Seqable
(seq [_]
(seq the-map))

clojure.lang.IPersistentMap
(assoc [_ k v]
(if (< (count the-map) 8)
(->AlwaysArrayMap (assoc the-map k v))
(->AlwaysArrayMap (assoc-after the-map k v))))
clojure.lang.IPersistentMap
(assoc [_ k v]
(if (< (count the-map) 8)
(->AlwaysArrayMap (assoc the-map k v))
(->AlwaysArrayMap (assoc-after the-map k v))))

(assocEx [_ _k _v]
(throw (ex-info "Not implemented" {})))
(assocEx [_ _k _v]
(throw (ex-info "Not implemented" {})))

(without [_ k]
(->AlwaysArrayMap (dissoc the-map k)))
(without [_ k]
(->AlwaysArrayMap (dissoc the-map k)))

clojure.lang.Associative
(containsKey [_ k]
(contains? the-map k))
clojure.lang.Associative
(containsKey [_ k]
(contains? the-map k))

clojure.lang.IPersistentCollection
(equiv [_ other]
(= the-map other))
(count [_]
(count the-map))
clojure.lang.IPersistentCollection
(equiv [_ other]
(= the-map other))
(count [_]
(count the-map))

java.lang.Iterable
(iterator [_]
(.iterator the-map))
java.lang.Iterable
(iterator [_]
(.iterator the-map))

clojure.lang.IMeta
(meta [_]
(meta the-map))
clojure.lang.IMeta
(meta [_]
(meta the-map))

clojure.lang.IObj
(withMeta [_ meta]
(->AlwaysArrayMap (with-meta the-map meta)))
clojure.lang.IObj
(withMeta [_ meta]
(->AlwaysArrayMap (with-meta the-map meta)))

Object
(toString [_]
"<always-array-map>"))
Object
(toString [_]
"<always-array-map>")))

(defn assoc-before [aam k v]
(->AlwaysArrayMap (apply array-map (list* k v (interleave (keys aam) (vals aam))))))

(defn always-array-map [& kvs]
(->AlwaysArrayMap (apply array-map kvs)))

(defmethod print-method AlwaysArrayMap
[v ^java.io.Writer writer]
(.write writer "{")
(let [write-kv! (fn [k v]
(.write writer (pr-str k))
(.write writer " ")
(.write writer (pr-str v)))]
(doseq [[k v] (butlast v)]
(write-kv! k v)
(.write writer ", "))
(let [[k v] (last v)]
(write-kv! k v)))
(.write writer "}"))
(utils/if-bb
nil
(defmethod print-method AlwaysArrayMap
[v ^java.io.Writer writer]
(.write writer "{")
(let [write-kv! (fn [k v]
(.write writer (pr-str k))
(.write writer " ")
(.write writer (pr-str v)))]
(doseq [[k v] (butlast v)]
(write-kv! k v)
(.write writer ", "))
(let [[k v] (last v)]
(write-kv! k v)))
(.write writer "}")))

(comment
(pr-str (always-array-map 1 2))
Expand Down
6 changes: 6 additions & 0 deletions src/nextjournal/clerk/analyzer.bbold
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns nextjournal.clerk.analyzer
"Babashka runtime no-op stubs")

;; TODO: consider using this in eval
(defn valuehash [val] "valuehash")
(defn ->hash-str [val] (valuehash val))
15 changes: 10 additions & 5 deletions src/nextjournal/clerk/analyzer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
[nextjournal.clerk.classpath :as cp]
[nextjournal.clerk.config :as config]
[nextjournal.clerk.parser :as parser]
[nextjournal.clerk.utils :as utils]
[nextjournal.clerk.walk :as walk]
[taoensso.nippy :as nippy]
[weavejester.dependency :as dep]))

(when-not utils/bb?
(require '[taoensso.nippy :as nippy]))

(set! *warn-on-reflection* true)

(defn deref? [form]
Expand Down Expand Up @@ -646,10 +649,12 @@
(let [digest-fn (case hash-type
:sha1 sha1-base58
:sha512 sha2-base58)]
(binding [nippy/*incl-metadata?* false]
(-> value
nippy/fast-freeze
digest-fn)))))
(utils/if-bb (-> value digest-fn)
#_{:clj-kondo/ignore [:unresolved-namespace]}
(binding [nippy/*incl-metadata?* false]
(-> value
nippy/fast-freeze
digest-fn))))))

#_(valuehash (range 100))
#_(valuehash :sha1 (range 100))
Expand Down
3 changes: 1 addition & 2 deletions src/nextjournal/clerk/builder.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[clojure.java.browse :as browse]
[clojure.java.io :as io]
[clojure.string :as str]
[nextjournal.clerk.analyzer :as analyzer]
[nextjournal.clerk.builder-ui :as builder-ui]
[nextjournal.clerk.config :as config]
[nextjournal.clerk.eval :as eval]
Expand Down Expand Up @@ -301,7 +300,7 @@
{state :result duration :time-ms} (eval/time-ms (mapv (comp (partial parser/parse-file {:doc? true}) :file) state))
_ (report-fn {:stage :parsed :state state :duration duration})
{state :result duration :time-ms} (eval/time-ms (reduce (fn [state doc]
(try (conj state (-> doc analyzer/build-graph analyzer/hash))
(try (conj state (eval/analyze-doc doc))
(catch Exception e
(reduced {:error e :file (:file doc)}))))
[]
Expand Down
Loading
Loading