From 145127b4301b6dfe66ac278f93e3267f341e79b6 Mon Sep 17 00:00:00 2001 From: sorrel Date: Thu, 18 Jan 2024 12:38:14 -0500 Subject: [PATCH] refactor publish and modify scripts to share utils --- .dev-log | 6 ++++++ modify.rkt | 11 +++------- publish.rkt | 39 +++++++++++++++++++---------------- utils.rkt | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 26 deletions(-) create mode 100644 utils.rkt diff --git a/.dev-log b/.dev-log index e37f544..348bd47 100644 --- a/.dev-log +++ b/.dev-log @@ -1,3 +1,9 @@ +2024/01/17 +- add html cache directories to build script +TODO: + - putting content in atom will require changing csv parsing; + that's for v2 + 2024/01/15 - cache static logic - START HERE: diff --git a/modify.rkt b/modify.rkt index d80a655..ac1824b 100644 --- a/modify.rkt +++ b/modify.rkt @@ -8,7 +8,7 @@ (only-in racket/function thunk identity) (only-in csv-reading csv->list) (only-in xml read-xml) - (only-in "./tool/utils.rkt" list->csv resource-link atom-table-entry add-atom-entry + (only-in "./utils.rkt" list->csv resource-link atom-table-entry add-atom-entry make-feed archive-file)) (define-namespace-anchor anc) @@ -327,7 +327,7 @@ [desc (third res-row)] [publish-time (hash-ref accumulator 'publish-time)] [new-table (add-atom-entry old-table - (atom-table-entry l-head res-link desc publish-time update-time))]) + (atom-table-entry l-head res-link desc publish-time #:update-time update-time))]) (hash-remove (hash-remove (hash-update @@ -394,9 +394,4 @@ (displayln "modify was successful"))))) -;(run modify) - -(run modify #("-i" "in-progress/now.txt" - "-l" "now" - "-r" "root" - "-x")) \ No newline at end of file +(run modify) diff --git a/publish.rkt b/publish.rkt index 68e6dc0..df4fe0d 100644 --- a/publish.rkt +++ b/publish.rkt @@ -5,7 +5,7 @@ (only-in racket/format ~a) (only-in racket/list append* first second third fourth fifth rest flatten add-between take) (only-in xml xexpr->string) - (only-in "./tool/utils.rkt" homepage list->csv add-atom-entry archive-file) + (only-in "./utils.rkt" homepage list->csv add-atom-entry atom-table-entry make-feed archive-file) csv-reading) (define-namespace-anchor anc) @@ -254,6 +254,7 @@ ;; update tag table + (displayln "updating tag table") (define tag-table (csv->list (open-input-file (if x "publish-test/data/tagged.csv" "data/tagged.csv")))) (define new-tag-table (write-new-tag-table tag-table t r res-id)) @@ -263,11 +264,11 @@ (if x (write-new-tagged/index new-tag-table #:test #t) (write-new-tagged/index new-tag-table))) - ; (displayln new-tag-table) + (write-csv-to-file (list->csv new-tag-table) (if x "publish-test/data/tagged.csv" "data/tagged.csv")) - + (displayln "updating tagged/'tag") ;; update tagged/'tag for each tag (for-each (lambda (tt-row) @@ -282,7 +283,7 @@ '()))) new-tag-table) - + (displayln "writing resource to file") ;; write to file (if (port-try-file-lock? out 'exclusive) (begin @@ -292,9 +293,13 @@ (error "couldn't obtain file lock on ~a" out)) ;; update 'resource/index - (write-new-resource/index r new-res-table #:test (if x #t #f)) - + (if (equal? r "root") + (displayln "skipping resource/index") + (begin + (displayln "updating resource/index") + (write-new-resource/index r new-res-table #:test (if x #t #f)))) + (displayln "updating feed table") ;; update feed table (define atom-table (csv->list (open-input-file @@ -304,20 +309,14 @@ (build-path "data" "atom")) #".csv")))) (define new-atom-table - (add-atom-entry atom-table (list l (~a res-link) d publish-time))) + (add-atom-entry atom-table (atom-table-entry l (~a res-link) d publish-time))) (write-csv-to-file (list->csv new-atom-table) (if x "publish-test/data/atom.csv" "data/atom.csv")) ;; update feed.atom ;; for now, this does not include the html for the post, only a link - (define feed ((eval (read (open-input-file (if x - "publish-test/data/make-atom.txt" - "data/make-atom.txt"))) ns) - "https://oxaliq.net/feed.atom" - publish-time - homepage - (rest new-atom-table))) + (displayln "updating feed") (define feed-out (open-output-file (if x "publish-test/source/feed.atom" "source/feed.atom") @@ -325,10 +324,14 @@ #:exists (if x 'replace 'error))) (if (port-try-file-lock? feed-out 'exclusive) (begin - (display - (string-append "" - (xexpr->string feed)) - feed-out) + (display + (make-feed ns (open-input-file + (if x + "publish-test/data/make-atom.txt" + "data/make-atom.txt")) + new-atom-table + publish-time) + feed-out) (port-file-unlock feed-out) (close-output-port feed-out)) (error "couldn't obtain file lock on ~a" feed-out)) diff --git a/utils.rkt b/utils.rkt new file mode 100644 index 0000000..b05b3b4 --- /dev/null +++ b/utils.rkt @@ -0,0 +1,59 @@ +#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) + (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")))) + + +;; 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 "" + (xexpr->string ((eval (read make-atom-input-port) ns) + "https://oxaliq.net/feed.atom" + publish-time + hp + (rest atom-table))))) +;; ---------------