This commit is contained in:
2026-03-21 19:37:06 -06:00
parent 34f2a41ba0
commit ce1fd5dce5
5 changed files with 58 additions and 29 deletions

View File

@@ -0,0 +1,24 @@
(ns net.deertopia.publisher.cached-file
(:require [babashka.fs :as fs]))
(defn newer-than?
"Return `true` if fs `file₁` was last modified sooner or at the same
time as `file₂`, or if `file₂` does not exist."
[file file]
(or (not (fs/exists? file))
(<= 0 (compare (fs/last-modified-time file)
(fs/last-modified-time file)))))
(def ^:dynamic *use-cache?*
"Bind to `false` to disable caching for debugging purposes."
true)
(defn cached-content
"Return a file path after potentially regenerating the file by
calling `compute` with no arguments only if stale? is logical true."
[& {:keys [file stale? compute]}]
(when (or (not *use-cache?*) stale?)
(let [r (compute)]
(assert (string? r))
(spit file r)))
file)

View File

@@ -9,7 +9,8 @@
(io/resource "net/deertopia/publisher/elisp/grammar")) (io/resource "net/deertopia/publisher/elisp/grammar"))
(defn- transform-string [s] (defn- transform-string [s]
(letfn [(go [s acc] (let [s* (loop [s (seq s)
acc ""]
(match s (match s
([\\ c & cs] :seq) ([\\ c & cs] :seq)
(recur (recur
@@ -24,7 +25,7 @@
(throw (ex-info "IDK!" {:char c}))))) (throw (ex-info "IDK!" {:char c})))))
([c & cs] :seq) (recur cs (str acc c)) ([c & cs] :seq) (recur cs (str acc c))
([] :seq) acc))] ([] :seq) acc))]
[:string (apply str (go (seq s) ""))])) [:string (apply str s*)]))
(defn- transform-integer [s] (defn- transform-integer [s]
[:integer (parse-long s)]) [:integer (parse-long s)])
@@ -107,7 +108,9 @@
;; TODO: this is really not how it should be done lol. at the ;; TODO: this is really not how it should be done lol. at the
;; moment, `print` is only used in `net.deertopia.publisher.roam` ;; moment, `print` is only used in `net.deertopia.publisher.roam`
;; and only to serialise uuids, so it's not a /massive/ priority. ;; and only to serialise uuids, so it's not a /massive/ priority.
(cond (string? x) (str \" s \"))) (cond (string? x) (str \" x \")
:else (throw (ex-info "`print` is unimplemented lol"
{:x x}))))
(comment (comment
(do (ip/defparser parse* (io/resource "elisp-grammar")) (do (ip/defparser parse* (io/resource "elisp-grammar"))

View File

@@ -49,13 +49,12 @@
(defn org-file [node] (defn org-file [node]
(fetch-with-cache (fetch-with-cache
node node :org-file
:org-file
(fn [node] (fn [node]
(when-some [r (sql/execute-one! (when-some [r (sql/execute-one!
ds ds
["select file from nodes where id = ?" ["select file from nodes where id = ?"
(elisp/print (:id node))])] (-> node :id str elisp/print)])]
(-> r :nodes/file elisp/read-string))))) (-> r :nodes/file elisp/read-string)))))
(defprotocol GetNode (defprotocol GetNode

View File

@@ -4,6 +4,7 @@
[hiccup2.core :as hiccup] [hiccup2.core :as hiccup]
[net.deertopia.doerg.html :as doerg-html] [net.deertopia.doerg.html :as doerg-html]
[net.deertopia.publisher.slug :as slug] [net.deertopia.publisher.slug :as slug]
[net.deertopia.publisher.roam :as roam]
[org.httpkit.server :as http] [org.httpkit.server :as http]
[reitit.coercion] [reitit.coercion]
[reitit.coercion.spec] [reitit.coercion.spec]
@@ -15,7 +16,8 @@
[spec-tools.spell] [spec-tools.spell]
[reitit.spec] [reitit.spec]
[reitit.dev.pretty] [reitit.dev.pretty]
[clojure.spec.alpha :as s])) [clojure.spec.alpha :as s]
[net.deertopia.doerg.render :as doerg-render]))
;;; Routes ;;; Routes
@@ -35,18 +37,19 @@
response/response response/response
(response/content-type "text/html"))) (response/content-type "text/html")))
(defn node-by-slug [req] (defn node-by-slug [{{{:keys [slug]} :path} :parameters}]
(-> (hiccup/html {} (-> (hiccup/html {}
[:html [:html
[:head [:head
[:title "node-by-slug"] [:title "node by sluggg"]
doerg-html/charset doerg-html/charset
doerg-html/viewport] doerg-html/viewport]
[:body [:body
[:h1 "node by slug"] [:h1 "node by slug"]
[:pre [:pre
(with-out-str (with-out-str
(pprint (:parameters req)))]]]) (pprint (-> slug slug/from-string roam/get-node
roam/org-file)))]]])
str str
response/response response/response
(response/content-type "text/html"))) (response/content-type "text/html")))
@@ -58,10 +61,10 @@
(reitit.ring/router (reitit.ring/router
#{["/" {:get hello}] #{["/" {:get hello}]
["/n/:slug" ["/n/:slug"
{:get {:handler node-by-slug {:get {:handler #'node-by-slug
:parameters :parameters
{:path {:slug ::slug/slug}}}}] {:path {:slug ::slug/slug}}}}]
["/id/:id" {:get node-by-id}]} ["/id/:id" {:get #'node-by-id}]}
{:validate reitit.spec/validate {:validate reitit.spec/validate
:exception reitit.dev.pretty/exception :exception reitit.dev.pretty/exception
:spec (s/merge :reitit.spec/default-data) :spec (s/merge :reitit.spec/default-data)