Browse Source

Add back prev/next link feat.

main
fshee 2 weeks ago
parent
commit
54d5f73493
  1. 2
      .gitignore
  2. 48
      routes.rkt
  3. 2
      server.rkt
  4. 46
      wp.rkt

2
.gitignore

@ -1,2 +1,4 @@
dist
*.tgz
compiled
*.rkt~

48
routes.rkt

@ -85,7 +85,7 @@
#:overflow-y hidden
#:-webkit-overflow-scrolling touch))))))
(define (tmpl c)
(define (tmpl post-cat c)
;; here be dragons (string->xexpr ...)
`(html
(head
@ -132,7 +132,23 @@
(br)
(br)))
(wp:content-posts c)))
`(span)))
`(span))
,(if (wp:content-is c post-cat)
`(div
,(if (wp:content-next c)
`(blockquote (p (span "Next")
(br)
(a ((href ,(string-append "/" (wp:content-slug (wp:content-next c)))))
,(wp:content-title (wp:content-next c)))))
`(span))
,(if (wp:content-prev c)
`(blockquote (p (span "Prev")
(br)
(a ((href ,(string-append "/" (wp:content-slug (wp:content-prev c)))))
,(wp:content-title (wp:content-prev c)))))
`(span)))
`(span)))
(footer
(pre
(span "PUB " ,(wp:content-pub-fmt c))
@ -157,11 +173,12 @@
(define (route-root cache wp-url home-content-slug post-cat page-cat req)
(with-handlers ((exn:fail? (lambda (exn)
(displayln exn)
(route-content (wp:dummy-err)))))
(route-content post-cat (wp:dummy-err)))))
(let* ((uri (request->string-path req))
(uri-parts (string-split uri "/")))
(cond ((empty? uri-parts)
(route-content-list
post-cat
(lambda ()
(wp:fetch-post cache wp-url (list page-cat) home-content-slug))
(lambda ()
@ -176,26 +193,28 @@
(wp:dummy-search-found
term
(wp:fetch-post-search cache wp-url (list post-cat page-cat) term))))))
(route-content (list (func)))))
(route-content post-cat (list (func)))))
((equal? (first uri-parts) "subject")
(let ((tag (wp:fetch-tag cache wp-url (string-trim uri "subject/" #:left? #t))))
(route-content-list
post-cat
(lambda () (map wp:tag->content tag))
(lambda () (wp:fetch-post-list cache wp-url (list post-cat) (map wp:tag-id tag))))))
(else (route-content (wp:fetch-post cache wp-url (list page-cat post-cat) uri)))))))
(else (route-content post-cat (wp:fetch-post cache wp-url (list page-cat post-cat) uri)))))))
;; helpers
(define (route-content c)
(define (route-content post-cat c)
(if (empty? c)
(response/xexpr (tmpl (wp:dummy-not-found)) #:code 404)
(response/xexpr (tmpl (car c)))))
(response/xexpr (tmpl post-cat (wp:dummy-not-found)) #:code 404)
(response/xexpr (tmpl post-cat (car c)))))
(define (route-content-list make-content make-content-list)
(define (route-content-list post-cat make-content make-content-list)
(let ((content-list (make-content-list)))
(if (empty? content-list)
(route-content content-list)
(route-content (map (lambda (c)
(route-content post-cat content-list)
(route-content post-cat
(map (lambda (c)
(wp:content (wp:content-title c)
(wp:content-desc c)
(wp:content-slug c)
@ -203,5 +222,8 @@
(wp:content-pub c)
(wp:content-mod c)
(wp:content-tags c)
content-list))
(make-content))))))
content-list
(wp:content-next c)
(wp:content-prev c)
(wp:content-cats c)))
(make-content))))))

2
server.rkt

@ -19,7 +19,7 @@
(define (cache-attempt h key)
(if (hash-has-key? h key)
(hash-ref h key)
'()))
'())) ;; All downstream APIs return list and empty lists are valid returns.
(define (forever f)
(f)

46
wp.rkt

@ -23,8 +23,12 @@
content-mod
content-tags
content-posts
content-prev
content-next
content-cats
content-pub-fmt
content-mod-fmt
content-is
tag->content
tag
tag-id
@ -40,7 +44,10 @@
(dummy-date)
(dummy-date)
'()
posts))
posts
#f
#f
'()))
(define (dummy-search-init)
(content
@ -51,6 +58,9 @@
(dummy-date)
(dummy-date)
'()
'()
#f
#f
'()))
(define (dummy-not-found)
@ -62,6 +72,9 @@
(dummy-date)
(dummy-date)
'()
'()
#f
#f
'()))
(define (dummy-err)
@ -73,6 +86,9 @@
(dummy-date)
(dummy-date)
'()
'()
#f
#f
'()))
(define date-compiled
@ -131,7 +147,7 @@
q))
(struct content
(title desc slug excerpt pub mod tags posts))
(title desc slug excerpt pub mod tags posts prev next cats))
;; methods
(define (content-pub-fmt c)
(content-format-date (content-pub c)))
@ -147,6 +163,14 @@
(string-pad-left 2 "0" (number->string (date-hour d)))
(string-pad-left 2 "0" (number->string (date-minute d)))
"Z")))
(define (content-is c cat)
(foldl (lambda (val ret)
(if (equal? val cat)
#t
ret))
#f
(content-cats c)))
(struct tag
(id title desc slug))
@ -160,6 +184,9 @@
(dummy-date)
(dummy-date)
'()
'()
#f
#f
'()))
(define (make-post hash)
@ -171,11 +198,16 @@
(hash-ref* hash 'date_gmt)
(hash-ref* hash 'modified_gmt)
(if (hash-has-key? hash '_embedded)
(map
make-tag
(second (hash-ref* hash '_embedded 'wp:term)))
(map make-tag (second (hash-ref* hash '_embedded 'wp:term)))
'())
'()))
'()
(if (hash-has-key? hash 'prev)
(make-post (hash-ref* hash 'prev))
#f)
(if (hash-has-key? hash 'next)
(make-post (hash-ref* hash 'next))
#f)
(hash-ref* hash 'categories)))
(define (make-tag hash)
(tag
@ -261,4 +293,4 @@
(fetch-post cache wp (list cat) (content-slug content))))
(define (prefetch-tag cache wp tag)
(fetch-tag cache wp (tag-slug tag)))
(fetch-tag cache wp (tag-slug tag)))
Loading…
Cancel
Save