commit 2907b037c49b57aca889bce9020f0d3f5950bae0
parent a305d2ebfc7f0a5ec08c4cd9edfc017f03ffe5b6
Author: Daniel Barlow <dan@telent.net>
Date: Wed, 6 Aug 2025 21:04:30 +0100
move timestamp parsing to lib
Diffstat:
| M | lib.fnl | | | 74 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
| M | site.fnl | | | 63 | +-------------------------------------------------------------- |
2 files changed, 72 insertions(+), 65 deletions(-)
diff --git a/lib.fnl b/lib.fnl
@@ -1,3 +1,7 @@
+(import-macros { : define-tests : expect : expect= } :assert)
+(local { : view} (require :fennel))
+
+
(fn prepend [head tbl]
(if tbl
(doto tbl
@@ -11,8 +15,72 @@
:to-string #(table.concat strings)
}))
-(local { : view} (require :fennel))
-
(fn trace [x] (print :trace (view x)) x)
-{ : prepend : string-stream : trace "%%" string.format }
+
+;; 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")
+
+(local months
+ {"jan" 1
+ "feb" 2
+ "mar" 3
+ "apr" 4
+ "may" 5
+ "jun" 6
+ "jul" 7
+ "aug" 8
+ "sep" 9
+ "oct" 10
+ "nov" 11
+ "dec" 12
+ })
+
+(fn parse-rfc2822-timestamp [date-str]
+ (let [(s _ weekday day month year hour min sec offset)
+ (string.find date-str "(%a+), (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) ([+-]%d+)" 1)]
+ (and s
+ (os.time {
+ : year
+ :month (. months (string.lower month))
+ : day
+ : hour
+ : min
+ : sec
+ :isdst (not (= offset "+0000")) ;; massive cheating here
+ }))))
+
+(fn parse-emacs-timestamp [date-str]
+ (let [(s _ weekday month day hour min sec year)
+ (string.find date-str "(%a+) (%a+) +(%d+) (%d+):(%d+):(%d+) (%d+)" 1)]
+ (and s
+ (os.time {
+ : year
+ :month (. months (string.lower month))
+ : day
+ : hour
+ : min
+ : sec
+ :isdst true ; probably?
+ }))))
+
+(fn parse-timestamp [s]
+ (or (parse-rfc2822-timestamp s)
+ (parse-emacs-timestamp s)))
+
+(define-tests :date-parsing
+ (expect= (parse-rfc2822-timestamp "Fri, 18 Oct 2013 10:18:34 +0100")
+ 1382087914)
+ (expect= (parse-rfc2822-timestamp "Tue, 28 Dec 2004 21:41:24 +0000")
+ 1104270084)
+ (expect= (parse-emacs-timestamp "Sat Jul 19 23:53:21 2025") 1752965601)
+ (expect= (parse-rfc2822-timestamp "Sat Jul 19 23:53:21 2025") nil)
+ (expect= (parse-timestamp "Sat Jul 19 23:53:21 2025") 1752965601)
+ (expect= (parse-timestamp "Wed Aug 8 12:51:52 2018") 1533729112)
+ )
+
+{
+ : prepend : string-stream : trace "%%" string.format
+ : parse-timestamp
+ }
diff --git a/site.fnl b/site.fnl
@@ -8,69 +8,8 @@
(local textile (require :textile))
(local lcmark (require :lcmark))
(local { : to-html } (require :html))
-(local { : trace } (require :lib))
+(local { : parse-timestamp : trace } (require :lib))
-;; 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")
-
-(local months
- {"jan" 1
- "feb" 2
- "mar" 3
- "apr" 4
- "may" 5
- "jun" 6
- "jul" 7
- "aug" 8
- "sep" 9
- "oct" 10
- "nov" 11
- "dec" 12
- })
-
-(fn parse-rfc2822-timestamp [date-str]
- (let [(s _ weekday day month year hour min sec offset)
- (string.find date-str "(%a+), (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) ([+-]%d+)" 1)]
- (and s
- (os.time {
- : year
- :month (. months (string.lower month))
- : day
- : hour
- : min
- : sec
- :isdst (not (= offset "+0000")) ;; massive cheating here
- }))))
-
-(fn parse-emacs-timestamp [date-str]
- (let [(s _ weekday month day hour min sec year)
- (string.find date-str "(%a+) (%a+) +(%d+) (%d+):(%d+):(%d+) (%d+)" 1)]
- (and s
- (os.time {
- : year
- :month (. months (string.lower month))
- : day
- : hour
- : min
- : sec
- :isdst true ; probably?
- }))))
-
-(fn parse-timestamp [s]
- (or (parse-rfc2822-timestamp s)
- (parse-emacs-timestamp s)))
-
-(define-tests :date-parsing
- (expect= (parse-rfc2822-timestamp "Fri, 18 Oct 2013 10:18:34 +0100")
- 1382087914)
- (expect= (parse-rfc2822-timestamp "Tue, 28 Dec 2004 21:41:24 +0000")
- 1104270084)
- (expect= (parse-emacs-timestamp "Sat Jul 19 23:53:21 2025") 1752965601)
- (expect= (parse-rfc2822-timestamp "Sat Jul 19 23:53:21 2025") nil)
- (expect= (parse-timestamp "Sat Jul 19 23:53:21 2025") 1752965601)
- (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