commit 9a729139b93c5b103251138fbd92a6642a39599a
parent 6df87b8ac8abff5e4ffbdc4acfcb45f5490225cb
Author: Daniel Barlow <dan@telent.net>
Date: Sun, 27 Jul 2025 22:44:50 +0100
extract to-html into its own module
Diffstat:
4 files changed, 38 insertions(+), 24 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,7 +2,7 @@ FENNEL?=fennel
PREFIX?=/usr/local
NAME?=cripslock
-MODULES=main.fnl textile.fnl site.fnl
+MODULES=main.fnl textile.fnl site.fnl html.fnl
%.lua : %.fnl
$(FENNEL) --compile $< > $@
diff --git a/html.fnl b/html.fnl
@@ -0,0 +1,29 @@
+(fn string-stream []
+ (let [strings []]
+ {
+ :write #(table.insert strings $2)
+ :to-string (table.concat strings)
+ }))
+
+(fn to-html [el out-stream]
+ (if (not out-stream)
+ (let [s (to-html el (string-stream))] (s:to-string))
+ (if (= (type el) :string)
+ (out-stream:write el)
+ (= (type el) :number)
+ (out-stream:write (tostring el))
+ (let [[tag & content] el
+ (name attrstring)
+ (case tag
+ [name attrs]
+ (values name (accumulate [s ""
+ name value (pairs attrs)]
+ (.. s " " (string.format "%s=%q" name value))))
+ name
+ (values name ""))]
+ (out-stream:write (string.format "<%s%s>" name attrstring))
+ (each [_ child (ipairs content)]
+ (to-html child out-stream))
+ (out-stream:write (string.format "</%s>" name))))))
+
+{ : to-html }
diff --git a/main.fnl b/main.fnl
@@ -5,6 +5,7 @@
(local server (require :server))
(local textile (require :textile))
(local site (require :site))
+(local { : to-html } (require :html))
(local { : view &as fennel } (require :fennel))
@@ -161,7 +162,7 @@ in the Fediverse at <a href=\"https://axillae.telent.net/\">@dan@axillae.telent.
</div>
<h2>The Archive</h2>
")
- (textile.to-html s (doto ladder (table.insert 1 :ul)))
+ (to-html (doto ladder (table.insert 1 :ul)) s)
(stream:write_chunk "</ul></aside>")))
(define-tests :sidebar
@@ -206,12 +207,12 @@ in the Fediverse at <a href=\"https://axillae.telent.net/\">@dan@axillae.telent.
(if (string.match path "%.textile$")
(each [_ el (ipairs (textile.parse f))
&until (and excerpt? (> (f:seek :cur 0) 500))]
- (textile.to-html s el))
+ (to-html el s))
(stream:write_chunk
(translate-markdown (f:read (if excerpt? 600 "*a")))
false))
(when (and excerpt? (= (f:read 0) ""))
- (textile.to-html s [[:div {:class :continuelink}] [:div [[:a { :href post.url-path}] "... continue reading"]]]))))))
+ (to-html [[:div {:class :continuelink}] [:div [[:a { :href post.url-path}] "... continue reading"]]] s))))))
(fn search-posts [route stream]
diff --git a/textile.fnl b/textile.fnl
@@ -1,5 +1,7 @@
(import-macros { : define-tests : expect : expect= } :assert)
+(local { : to-html } (require :html))
+
(fn list-append [a1 a2]
(icollect [_ v (ipairs a2) &into a1] v))
@@ -306,24 +308,7 @@ filesystem. "])
(let [(tag body) (tag-and-body p)]
(cons-tag tag (extract-lists body)))))
-(fn to-html [out-stream el]
- (if (= (type el) :string)
- (out-stream:write el)
- (= (type el) :number)
- (out-stream:write (tostring el))
- (let [[tag & content] el
- (name attrstring)
- (case tag
- [name attrs]
- (values name (accumulate [s ""
- name value (pairs attrs)]
- (.. s " " (string.format "%s=%q" name value))))
- name
- (values name ""))]
- (out-stream:write (string.format "<%s%s>" name attrstring))
- (each [_ child (ipairs content)]
- (to-html out-stream child))
- (out-stream:write (string.format "</%s>" name)))))
+
(define-tests :parse-stream
@@ -331,7 +316,7 @@ filesystem. "])
(with-open [f (io.open name :r)]
(with-open [out (assert (io.tmpfile))]
(each [_ el (ipairs (parse-stream f))]
- (to-html out el))
+ (to-html el out))
(out:seek :set 0)
(out:read "*a"))))
@@ -355,5 +340,4 @@ hello
{
:parse parse-stream
- : to-html
}