commit 6331c57a8591097b6502fb4776464c35e8f4c64f
parent 5d63f693df75b51109d8a33964d14ae926a977e6
Author: Daniel Barlow <dan@telent.net>
Date: Thu, 31 Jul 2025 18:22:11 +0100
new method post:body reads post and converts from markdown/textile
Diffstat:
| M | main.fnl | | | 51 | +++++++-------------------------------------------- |
| M | site.fnl | | | 47 | +++++++++++++++++++++++++++++++++++++++++++++-- |
2 files changed, 52 insertions(+), 46 deletions(-)
diff --git a/main.fnl b/main.fnl
@@ -83,17 +83,6 @@
<article>"
title)))
-(fn post-header [post stream]
- (stream:write_chunk
- (string.format
- "<div class=\"entry\">
-<h1 class=\"title\">%s<span class=\"permalink\"><a href=%q>#</a></span></h1>
-<h2 class=\"date\">%s</h2>
-"
- post.title
- post.url-path
- (os.date "%c" post.date)
- )))
(fn page-footer [stream]
(stream:write_chunk
@@ -124,11 +113,11 @@
]
links))))
-(fn cq-stream [stream]
- { :write #(stream:write_chunk $2) })
+(fn stream-from-cq-stream [stream]
+ { :write #(stream:write_chunk $2 false) })
(fn sidebar [site stream]
- (let [s (cq-stream stream)
+ (let [s (stream-from-cq-stream stream)
topics (site:weighted-topics)
ladder (all-months site)]
(stream:write_chunk "<aside>
@@ -161,37 +150,11 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a>
;; (define-tests :sidebar
;; (print (view (all-months post-index))))
-(fn translate-markdown [md]
- ;; lcmark/cmark doesn't have any extension points that allow us
- ;; to add actual new syntax so we're just going to postprocess
- (let [subst-links (fn [slug title]
- (string.format
- "<a href=%q class=internal>%s</a>"
- slug
- (or title (. (post-index:get slug) :title))))]
- (-> md
- (lcmark.convert "html" { :yaml_parser #($1) })
- (string.gsub "%[%[([%w_-]+)|(.-)%]%]" subst-links)
- (string.gsub "%[%[([%w_-]+)%]%]" subst-links))))
-
(fn send-post [post stream excerpt?]
(let [path (. post :full-path)
- s (cq-stream stream)]
+ s (stream-from-cq-stream stream)]
(print :path path)
- (with-open [f (io.open path )]
- (when f
- (f:seek :set post.body-offset)
- (post-header post stream)
- (if (string.match path "%.textile$")
- (each [_ el (ipairs (textile.parse f))
- &until (and excerpt? (> (f:seek :cur 0) 500))]
- (to-html el s))
- (stream:write_chunk
- (translate-markdown (f:read (if excerpt? 600 "*a")))
- false))
- (when (and excerpt? (= (f:read 0) ""))
- (to-html [[:div {:class :continuelink}] [:div [[:a { :href post.url-path}] "... continue reading"]]] s))))))
-
+ (post:body post-index s)))
(fn send-search-results [stream format results]
(each [_ p (ipairs results)]
@@ -230,7 +193,7 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a>
:offset 0
}
posts (post-index:search params)
- s (cq-stream stream)]
+ s (stream-from-cq-stream stream)]
(page-header stream h1)
(send-search-results stream :posts posts)
(to-html
@@ -261,7 +224,7 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a>
(fn send-topic-page [stream slug]
(let [post (post-index:get slug)
results (post-index:search {:filter {:topic slug}})
- s (cq-stream stream)]
+ s (stream-from-cq-stream stream)]
(server.headers stream { })
(page-header stream (.. "Topic: " slug))
(if post
diff --git a/site.fnl b/site.fnl
@@ -3,6 +3,10 @@
(local { : realpath } (require :posix.stdlib))
(local { : view } (require :fennel))
+(local textile (require :textile))
+(local lcmark (require :lcmark))
+(local { : to-html } (require :html))
+
;; there are two date formats used in most of the extant posts:
;; "Fri Jun 29 14:17:56 2018" (emacs current-time-string)
;; and "Fri, 18 Oct 2013 10:18:34 +0100" (*rfc2822")
@@ -65,6 +69,46 @@
(expect= (parse-timestamp "Wed Aug 8 12:51:52 2018") 1533729112)
)
+(fn translate-markdown [site md]
+ ;; lcmark/cmark doesn't have any extension points that allow us
+ ;; to add actual new syntax so we're just going to postprocess
+ (let [subst-links (fn [slug title]
+ (string.format
+ "<a href=%q class=internal>%s</a>"
+ slug
+ (or title (. (site:get slug) :title))))]
+ (-> md
+ (lcmark.convert "html" { :yaml_parser #($1) })
+ (string.gsub "%[%[([%w_-]+)|(.-)%]%]" subst-links)
+ (string.gsub "%[%[([%w_-]+)%]%]" subst-links))))
+
+(fn post-header [post stream]
+ (stream:write
+ (string.format
+ "<div class=\"entry\">
+<h1 class=\"title\">%s<span class=\"permalink\"><a href=%q>#</a></span></h1>
+<h2 class=\"date\">%s</h2>
+"
+ post.title
+ post.url-path
+ (os.date "%c" post.date)
+ )))
+
+(fn post-body [post site stream excerpt?]
+ (let [path post.full-path]
+ (with-open [f (io.open path :r)]
+ (when f
+ (f:seek :set post.body-offset)
+ (post-header post stream)
+ (if (string.match path "%.textile$")
+ (each [_ el (ipairs (textile.parse f))
+ &until (and excerpt? (> (f:seek :cur 0) 500))]
+ (to-html el stream))
+ (stream:write
+ (translate-markdown site (f:read (if excerpt? 600 "*a")))))
+ (when (and excerpt? (= (f:read 0) ""))
+ (to-html [[:div {:class :continuelink}] [:div [[:a { :href post.url-path}] "... continue reading"]]] stream))))))
+
(fn frontmatter [stream]
(var bail? false)
@@ -119,6 +163,7 @@
(tset :slug slug)
(tset :title title)
(tset :topics topics)
+ (tset :body post-body)
(tset :body-offset (f:seek :cur 0))
(tset :date (and fields.date (parse-timestamp fields.date)))))))))))
@@ -175,8 +220,6 @@
some-posts)
posts)))
-
-
(fn for-pathname [path]
(let [posts (index-posts path)]
{