From f9baec0fd83f293025cfdc26d878425ed90bb8db Mon Sep 17 00:00:00 2001 From: Sorrel <52061229+sorrelbri@users.noreply.github.com> Date: Thu, 9 Nov 2023 13:06:51 -0500 Subject: [PATCH] stub make-index and add support to hx/ resource endpoints --- sorrel.dev.rkt | 26 ++++++++++++++-- source/make-index.txt | 64 ++++++++++++++++++++++++++++++++++++++ source/settled/index.txt | 3 -- source/tagged.txt | 3 -- source/unsettled/index.txt | 3 -- 5 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 source/make-index.txt delete mode 100644 source/settled/index.txt delete mode 100644 source/tagged.txt delete mode 100644 source/unsettled/index.txt diff --git a/sorrel.dev.rkt b/sorrel.dev.rkt index 397e3cb..10e1e6b 100644 --- a/sorrel.dev.rkt +++ b/sorrel.dev.rkt @@ -1,6 +1,8 @@ #lang racket (require racket/date + (only-in racket/list first second third rest) + (only-in racket/string string-prefix? string-replace) net/url xml web-server/web-server @@ -11,7 +13,8 @@ (prefix-in filter: web-server/dispatchers/dispatch-filter) (prefix-in sequencer: web-server/dispatchers/dispatch-sequencer) web-server/dispatchers/filesystem-map - web-server/http) + web-server/http + csv-reading) (define-namespace-anchor anc) (define ns (namespace-anchor->namespace anc)) @@ -70,6 +73,23 @@ (string->bytes/utf-8 (xexpr->string (read (open-input-file file))))) +(define (build-index res) + ;; get all the entries in res table + ;; build index + (let* ([table (csv->list (open-input-file + (path-add-extension + (build-path "data" res) + #".csv")))] + [make-index + (read (open-input-file "data/make-index.txt"))] + [index ((eval make-index ns) res (rest table))]) + (lambda (req) + (if (equal? res "tagged") + '() + (html-response + (string->bytes/utf-8 + (xexpr->string index))))))) + (define (404-hx request) (html-response (xexpr-file->xml "source/not-found.txt"))) @@ -77,9 +97,9 @@ (define-values (httpx-app reverse-httpx-uri) (dispatch-rules [("hx" "home") (respond-resource-with-processor "root" xexpr-file->xml)] - [("hx" "settled") (respond-resource-with-processor "settled" xexpr-file->xml)] + [("hx" "settled") (build-index "settled")] [("hx" "settled" (integer-arg)) (respond-resource-with-processor "settled" xexpr-file->xml)] - [("hx" "unsettled") (respond-resource-with-processor "unsettled" xexpr-file->xml)] + [("hx" "unsettled") (build-index "unsettled")] [("hx" "unsettled" (integer-arg)) (respond-resource-with-processor "unsettled" xexpr-file->xml)] [("hx" "tagged") (respond-resource-with-processor "tagged" xexpr-file->xml)] [("hx" "tagged" (string-arg)) (respond-resource-with-processor "tagged" xexpr-file->xml)] diff --git a/source/make-index.txt b/source/make-index.txt new file mode 100644 index 0000000..950083d --- /dev/null +++ b/source/make-index.txt @@ -0,0 +1,64 @@ +(lambda (res posts) + (let ([article + `(article + ,(cond + [(equal? res "settled") + '(hgroup (h1 "settled* thoughts") + (p (em "*-ish, something like a portfolio of projects")))] + [(equal? res "unsettled") + '(hgroup (h1 "unsettled thoughts") + (p (em "just doing some thinking aloud")))] + ;; if res is tagged, build index of tags + [(equal? res "tagged") + '(hgroup (h1 "all the tags") + (p (em "you looking for a flavor of something?")))] + [(string-prefix? res "tagged/") + (let ([tag (string-replace res "tagged/" "#")]) + `(hgroup (h1 ,(~a "stuff what's tagged like \"~a\"" tag)) + (p (em (~a "everything (or maybe just some things) i've ever said about \"~a\"" tag)))))] + [else + '(hgroup (h1 "i'm lost") + (p (em "you weren't meant to be here")))]))]) + (define (settled-post post) + `((div ((class "post-preview")) + (h4 ,(second post)) + (p ,(third post)) + (a ((href ,(~a "/settled/" (first post)))) + ,(~a "go! to " (second post) " page"))))) + (define (unsettled-post post) + `((div ((class "post-preview")) + (h4 ,(second post)) + (p ,(third post)) + (a ((href ,(~a "/unsettled/" (first post)))) + ,(~a "go! to " (second post) " page"))))) + (define (tag-post post) + `((div ((class "post-preview")) + (h4 ,(second post)) + (p ,(third post)) + (a ((href ,(~a "/tagged/" (first post)))) + ,(~a "go! to " (second post) " page"))))) + (define (tagged-post post) + `((div ((class "post-preview")) + (h4 ,(second post)) + (p ,(third post)) + ;; tagged posts will insert href instead of id + (a ((href ,first post)) + ,(~a "go! to " (second post) " page"))))) + + + (if + (< 0 (length posts)) + (for-each + (lambda (post) + (set! article + (append article + (cond + [(equal? res "settled") (settled-post post)] + [(equal? res "unsettled") (unsettled-post post)] + [(equal? res "tagged") (tag-post post)] + [(string-prefix? res "tagged/") (tagged-post post)])))) + posts) + (set! article + (append article + `((p "there's nothing here yet"))))) + article)) diff --git a/source/settled/index.txt b/source/settled/index.txt deleted file mode 100644 index 36750a1..0000000 --- a/source/settled/index.txt +++ /dev/null @@ -1,3 +0,0 @@ -(article - (p - "stubbed tagged page")) \ No newline at end of file diff --git a/source/tagged.txt b/source/tagged.txt deleted file mode 100644 index 36750a1..0000000 --- a/source/tagged.txt +++ /dev/null @@ -1,3 +0,0 @@ -(article - (p - "stubbed tagged page")) \ No newline at end of file diff --git a/source/unsettled/index.txt b/source/unsettled/index.txt deleted file mode 100644 index 36750a1..0000000 --- a/source/unsettled/index.txt +++ /dev/null @@ -1,3 +0,0 @@ -(article - (p - "stubbed tagged page")) \ No newline at end of file