oxaliq.net/utils.rkt
sorrel 7bc26d0864
All checks were successful
ci/woodpecker/push/build-and-deploy Pipeline was successful
fix bugs in modify and publish
file-writing bugs caused problems on file-read depending on wether print or display
was used. publish was not overwriting existing feed.
2024-01-18 17:32:03 -05:00

59 lines
2.2 KiB
Racket

#lang racket
(provide (all-defined-out))
(require (only-in racket/list flatten add-between)
(only-in xml xexpr->string))
(define homepage "https://oxaliq.net")
;; takes a parsed table as a list of lists and formats for writing as a .csv file
(define (list->csv l)
(foldl (lambda (i res)
(string-append res i))
""
(flatten
(add-between (map (lambda (row)
(add-between row ","))
l)
"\n"))))
;; construct resource-link for use in atom feed
(define (resource-link resource-type resource-id #:homepage (hp homepage))
(if (equal? resource-type "root")
(build-path hp (~a resource-id))
(build-path hp resource-type (~a resource-id))))
(define (archive-file input-file test #:modify-mode (exists-ok #f))
(if test
(copy-file input-file (string-replace input-file "in-progress" "publish-test/archive") #t)
(rename-file-or-directory input-file (string-replace input-file "in-progress" "archive") exists-ok)))
;; atom-table utils
;; ----------------
;; atom-table-entry constructs new row to pass to add-atom-entry
(define (atom-table-entry headline resource-link desc publish-time #:update-time (update-time ""))
(list headline (~a resource-link) desc publish-time update-time))
;; add-atom-entry takes old atom table and new row, constructing new
;; atom table with only first 21 rows (or all rows)
(define (add-atom-entry atom-table new-row)
;; insert new-row after header
(let ([out-length (min (+ 1 (length atom-table)) 21)]
[header (first atom-table)]
[old-content (rest atom-table)])
(take (append (list header new-row) old-content)
out-length)))
;; ----------------
;; atom-feed utils
;; ---------------
(define (make-feed ns make-atom-input-port atom-table publish-time #:homepage (hp homepage))
(string-append "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
(xexpr->string ((eval (read make-atom-input-port) ns)
"https://oxaliq.net/feed.atom"
publish-time
hp
(rest atom-table)))))
;; ---------------