commit 226f324a1d631cf1ef0e92b61bc8a2805b31ea18 parent a0d36ba5d28b8c2aab88b69a5d3c68230dbd391e Author: Daniel Barlow <dan@telent.net> Date: Sat, 19 Jul 2025 23:17:05 +0100 read the files on startup Diffstat:
| M | main.fnl | | | 41 | +++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 41 insertions(+), 0 deletions(-)
diff --git a/main.fnl b/main.fnl @@ -1 +1,42 @@ +(import-macros { : define-tests : expect : expect= } :assert) +(local lfs (require :lfs)) + +(local { : view } (require :fennel)) + +(fn frontmatter [stream] + (var bail? false) + (collect [l (stream:lines) + &until (or (= l "") bail?)] + (let [(k v) (string.match l "(.-): *(.+)")] + (if k + (values (string.lower k) v) + (set bail? true))))) + +(define-tests :frontmatter + (with-open [stream (io.open "tests/post-with-headers.textile" :r)] + (let [headers (frontmatter stream)] + (expect= headers.title "What I miss most about Lisp") + (expect= headers.author "Daniel Barlow") + ;; headers names are downcased + (expect= headers.x-parrot "norweigan blue") + (expect= headers.date "Sat Apr 30 23:23:33 2011") + ;; leaves stream position at first body line + (expect= (stream:read "l") "first content line")))) + + +(fn index-posts [path] + (collect [name (lfs.dir path)] + (let [fullname (.. path "/" name) + (attr err) (lfs.attributes fullname)] + (when (not attr) (print :err err)) + (if (and attr + (= attr.mode :file) + (or + (string.match name "%.textile$") + (string.match name "%.md$"))) + (with-open [f (io.open fullname)] + (values name (frontmatter f))))))) + +;; (print (view (index-posts "data/blogs/telent.blog/posts/"))) + {}