cripslock

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit cf0640087f279ec4e01d8e2ad6d3b83842bdf6fd
parent 71b07a12dc9ee87fa1b56e2085b2776d39524cd9
Author: Daniel Barlow <dan@telent.net>
Date:   Wed,  6 Aug 2025 22:42:48 +0100

remove file-scoped post-index, pass collection to fns that need it

Diffstat:
Mmain.fnl | 81+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 40 insertions(+), 41 deletions(-)

diff --git a/main.fnl b/main.fnl @@ -13,8 +13,6 @@ (local { : view } (require :fennel)) -(local post-index (site.for-pathname "data/blogs/telent.blog/posts/")) - (fn route-request [method path handlers] (accumulate [matched nil _ pred (ipairs handlers) @@ -139,10 +137,10 @@ topic]]) (table.insert 1 :div))])) -(fn sidebar [site stream] +(fn sidebar [stream collection] (let [s (stream-from-cq-stream stream) - topics (site:weighted-topics) - ladder (all-months site)] + topics (collection:weighted-topics) + ladder (all-months collection)] (stream:write_chunk "<aside> <div> <p> There are many more Daniel Barlows on the internet than there were when I started writing this, This one is a computer @@ -162,33 +160,33 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a> (stream:write_chunk "</ul></aside>"))) -(fn send-post [stream post excerpt?] +(fn send-post [stream collection post excerpt?] (let [path (. post :full-path) s (stream-from-cq-stream stream)] (print :path path) - (post:body post-index s))) + (post:body collection s))) -(fn send-search-results [stream format results] +(fn send-search-results [stream format collection results] (each [_ p (ipairs results)] - (send-post stream p (= format :excerpts)))) + (send-post stream collection p (= format :excerpts)))) -(fn send-home-page [stream] +(fn send-home-page [stream collection] (server.headers stream { }) (page-header stream "Recently") (let [posts - (post-index:search + (collection:search { :filter nil :order {:date :descending} :offset 0 :limit 5 })] - (send-search-results stream :posts posts)) - (sidebar post-index stream) + (send-search-results stream :posts collection posts)) + (sidebar stream collection) (page-footer stream) (stream:write_chunk "" true )) -(fn send-month-page [stream year month] +(fn send-month-page [stream collection year month] (server.headers stream { }) (let [h1 (string.format "%s %d" (month-name month) year) after (os.time {: year : month :day -2 }) @@ -204,10 +202,10 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a> :order {:date :ascending} :offset 0 } - posts (post-index:search params) + posts (collection:search params) s (stream-from-cq-stream stream)] (page-header stream h1) - (send-search-results stream :posts posts) + (send-search-results stream :posts collection posts) (to-html [[:div {:class "nav"}] [:span @@ -219,28 +217,28 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a> (month-name nextmonth.m) " " nextmonth.y " ⟫"]]] s) - (sidebar post-index stream) + (sidebar stream collection) (page-footer stream) (stream:write_chunk "" true ))) -(fn send-post-page [stream slug] - (let [post (post-index:get slug) +(fn send-post-page [stream collection slug] + (let [post (collection:get slug) path (. post :full-path)] (server.headers stream { }) (page-header stream post.title) - (send-post stream post) - (sidebar post-index stream) + (send-post stream collection post) + (sidebar stream collection) (page-footer stream) (stream:write_chunk "" true))) -(fn send-topic-page [stream slug] - (let [post (post-index:get slug) - results (post-index:search {:filter {:topic slug}}) +(fn send-topic-page [stream collection slug] + (let [post (collection:get slug) + results (collection:search {:filter {:topic slug}}) s (stream-from-cq-stream stream)] (server.headers stream { }) (page-header stream (.. "Topic: " slug)) (if post - (send-post stream post) + (send-post stream collection post) (to-html [:h1 slug] s)) (to-html @@ -250,13 +248,13 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a> (icollect [_ p (ipairs results)] [:li [[:a { :href p.url-path}] p.title]]))] s) - (sidebar post-index stream) + (sidebar stream collection) (page-footer stream) (stream:write_chunk "" true))) -(fn send-rss [stream] +(fn send-rss [stream collection] (let [posts - (post-index:search + (collection:search { :filter nil :order {:date :descending} @@ -265,50 +263,51 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a> })] (server.headers stream { :content-type "application/rss+xml; charset=utf-8" }) - (rss.feed stream "https://ww.telent.net" post-index posts))) + (rss.feed stream "https://ww.telent.net" collection posts))) -(fn dispatch [route stream] +(fn dispatch [route stream collection] (print (view route)) (case route [:show { : slug }] - (send-post-page stream slug) + (send-post-page stream collection slug) [:rss] - (send-rss stream) + (send-rss stream collection) [:refresh _] (do (print "refreshing") - (post-index:refresh)) + (collection:refresh)) [:month { : month : year }] - (send-month-page stream year month) + (send-month-page stream collection year month) [:static { : path }] - (let [realpath (post-index:static-file path)] + (let [realpath (collection:static-file path)] (with-open [f (io.open realpath :r)] (server.headers stream { :content-type (content-type-for-path realpath) }) (stream:write_body_from_file f))) [:home { : limit }] - (send-home-page stream) + (send-home-page stream collection) [:topic { : slug }] - (send-topic-page stream slug) + (send-topic-page stream collection slug) )) -(fn http-handler [_ stream] +(fn http-handler [collection _ stream] (let [req-headers (assert (stream:get_headers)) path (req-headers:get ":path") method (req-headers:get ":method") route (route-request method path handlers)] (server.log-request req-headers stream) (if route - (dispatch route stream) + (dispatch route stream collection) (server.headers stream { ":status" :404 } true)))) (fn run [] (let [event-loop (cqueues.new) - myserver (server.new { :onstream http-handler } )] + collection (site.for-pathname "data/blogs/telent.blog/posts/") + myserver (server.new { :onstream (partial http-handler collection) })] (event-loop:wrap #(myserver:loop)) - (event-loop:wrap #(post-index:watch)) + (event-loop:wrap #(collection:watch)) (event-loop:loop)))