Skip to content

Commit b0c9217

Browse files
authored
Merge pull request #413 from jamiepratt/codex/rewrite-vocabulary-tutorial-series
Publish new Bayes-to-Lexibench vocabulary-estimation series improvements.
2 parents 7bc22a4 + 52f69ba commit b0c9217

11 files changed

Lines changed: 2506 additions & 443 deletions

clay.edn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{:base-target-path "temp"
22
:base-source-path "src"
3+
:watch-dirs ["src"]
4+
:format [:quarto :html]
5+
:run-quarto true
6+
:browse false
37
:quarto-target-path "site"
48
:quarto ^:replace {}
59
:subdirs-to-sync ["src"]

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
:aliases
9292
{;; Build the site with `clojure -M:clay -A:markdown`
9393
;; Run Clay in watch mode with `clojure -M:clay`
94-
:clay {:main-opts ["-m" "scicloj.clay.v2.main"]
94+
:clay {:main-opts ["-m" "civitas.clay-main"]
9595
:jvm-opts ["-Dclojure.main.report=stderr"
9696
"--add-opens=java.base/java.nio=ALL-UNNAMED"]}
9797
;; When debugging libraries

src/civitas/clay_main.clj

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
(ns civitas.clay-main
2+
(:gen-class)
3+
(:require [clojure.java.io :as io]
4+
[clojure.string :as str]
5+
[ring.util.mime-type :as mime-type]
6+
[scicloj.clay.v2.main :as clay-main]
7+
[scicloj.clay.v2.server :as clay-server]
8+
[scicloj.clay.v2.server.state :as server-state])
9+
(:import (java.io File)))
10+
11+
(def default-preview-source
12+
"language_learning/vocabulary_estimation/bayes_theorem_simulations.clj")
13+
14+
(def preview-root "temp")
15+
(def published-root "site/_site")
16+
17+
(defn- regular-file-under [root uri]
18+
(when (and (string? uri) (str/starts-with? uri "/"))
19+
(let [root-file (.getCanonicalFile (io/file root))
20+
file (.getCanonicalFile (io/file root-file (subs uri 1)))
21+
root-prefix (str (.getPath root-file) File/separator)]
22+
(when (and (str/starts-with? (.getPath file) root-prefix)
23+
(.isFile file))
24+
file))))
25+
26+
(defn- current-preview-uri []
27+
(let [{:keys [base-target-path full-target-path]}
28+
(:last-rendered-spec @server-state/*state)]
29+
(when (and base-target-path full-target-path)
30+
(str "/" (clay-server/relative-url-path base-target-path
31+
full-target-path)))))
32+
33+
(defn preview-fallback-handler [{:keys [request-method uri]}]
34+
(let [preview-file (regular-file-under preview-root uri)
35+
linked-html? (and (str/ends-with? uri ".html")
36+
(not= uri (current-preview-uri)))]
37+
(when (and (= :get request-method)
38+
(or (nil? preview-file) linked-html?))
39+
(when-let [file (regular-file-under published-root uri)]
40+
{:status 200
41+
:headers {"Content-Type"
42+
(or (mime-type/ext-mime-type uri)
43+
"application/octet-stream")}
44+
:body (if (str/ends-with? uri ".html")
45+
(clay-server/wrap-html (slurp file) @server-state/*state)
46+
file)}))))
47+
48+
(defn args-with-default-source [args]
49+
(if (seq args)
50+
args
51+
[default-preview-source]))
52+
53+
(defn -main [& args]
54+
(clay-server/install-handler! #'preview-fallback-handler)
55+
(apply clay-main/-main (args-with-default-source args)))

src/civitas/db.clj

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
(:require [clojure.edn :as edn]
44
[clojure.pprint :as pprint]
55
[clojure.walk :as walk]
6+
[scicloj.kindly.v4.api :as kindly]
67
[tablecloth.api :as tc]
78
[clj-yaml.core :as yaml]))
89

910
(defn quarto []
1011
(walk/postwalk
11-
(fn [x]
12-
(cond (map? x) (into {} x)
13-
(seq? x) (into [] x)
14-
:else x))
15-
(yaml/parse-string (slurp "site/_quarto.yml"))))
12+
(fn [x]
13+
(cond (map? x) (into {} x)
14+
(seq? x) (into [] x)
15+
:else x))
16+
(yaml/parse-string (slurp "site/_quarto.yml"))))
1617

1718
(def db-file "site/db.edn")
1819

@@ -28,10 +29,10 @@
2829
"Return a map where a key is (f item) and a value is item."
2930
[f coll]
3031
(persistent!
31-
(reduce
32-
(fn [ret x]
33-
(assoc! ret (f x) x))
34-
(transient {}) coll)))
32+
(reduce
33+
(fn [ret x]
34+
(assoc! ret (f x) x))
35+
(transient {}) coll)))
3536

3637
(defn author-replacements
3738
"Creates a map of author and affiliation keys to their full definition"
@@ -40,10 +41,29 @@
4041
(->> (index-by :id))
4142
(update-vals #(dissoc % :id))))
4243

44+
(defn- ns-clay-config [ns-form]
45+
(let [[_ sym ?doc ?attr] ns-form]
46+
(kindly/deep-merge
47+
(some-> ns-form meta :clay)
48+
(some-> sym meta :clay)
49+
(:clay (cond
50+
(map? ?doc) ?doc
51+
(map? ?attr) ?attr)))))
52+
53+
(defn- restore-replaced-quarto-config [config]
54+
(let [quarto (:quarto config)
55+
ns-quarto (some-> config :ns-form ns-clay-config :quarto)]
56+
(if (and ns-quarto (-> quarto meta :replace))
57+
(assoc config :quarto
58+
(kindly/deep-merge ns-quarto (with-meta quarto nil)))
59+
config)))
60+
4361
(defn expand-authors
4462
"Hook for Clay to update ns metadata configuration"
4563
[config]
46-
(update config :quarto #(walk/prewalk-replace (author-replacements) %)))
64+
(-> config
65+
restore-replaced-quarto-config
66+
(update :quarto #(walk/prewalk-replace (author-replacements) %))))
4767

4868
;; TODO: what if the front matter doesn't match existing?
4969

0 commit comments

Comments
 (0)