implement tag pages
[~bandali/bndl.org] / haunt.scm
index 30293b1..03af1aa 100644 (file)
--- a/haunt.scm
+++ b/haunt.scm
@@ -16,7 +16,7 @@
 (define my-domain "bandali.eu.org")
 (define my-url
   (string-append (symbol->string my-scheme) "://" my-domain))
-
+(define my-tag-prefix "tags")
 (define my-date-format "~B ~e, ~Y")
 
 (define (stylesheet name)
                (aa "license.html" "/license.html"))
           " for license conditions.  Please copy and share."))))))
 
+(define* (tag-uri prefix tag #:optional (ext ".html"))
+  "Return a URI relative to the site's root for a page listing entries
+in PREFIX that are tagged with TAG."
+  (string-append "/" my-tag-prefix "/" tag ext))
+
+(define* (tag-pages #:key
+                    (theme bandali-theme)
+                    (prefix "")
+                    (filter posts/reverse-chronological))
+  "Return a builder procedure that renders a list page for every tag
+used in a post.  All arguments are optional:
+
+PREFIX: The directory in which to write the posts
+FILTER: The procedure called to manipulate the posts list before rendering"
+  (lambda (site posts)
+    (define (tag-list tag posts all-posts)
+      (define (render-list title posts prefix)
+        (let ((body ((theme-collection-template theme)
+                     site title posts prefix all-posts tag)))
+          ((theme-layout theme) site title body)))
+      (make-page (tag-uri my-tag-prefix tag)
+                 (render-list (string-append "Notes tagged ‘" tag "’")
+                              (filter posts)
+                              prefix)
+                 sxml->html))
+    (let ((tag-groups (posts/group-by-tag posts)))
+      (map (match-lambda
+             ((tag . tagged-posts) (tag-list tag tagged-posts posts)))
+           tag-groups))))
+
+(define (tag-links posts)
+  "Generate an alphabetically sorted list of links to tagged posts.
+The link text consists of the tag name and the number of tagged posts
+in parentheses."
+  `(ul (@ (class "tag-list"))
+       ,(map (match-lambda
+               ((tag . posts)
+                `(li
+                  ,(aa (string-append tag
+                                      " ("
+                                      (number->string (length posts))
+                                      ")")
+                       (tag-uri my-tag-prefix tag)))))
+             ;; sort by tag
+             (sort (posts/group-by-tag posts)
+                   (lambda (a b) (string<? (car a) (car b)))))))
 
 (register-metadata-parser! 'updated string->date*)
 
+(define (intersperse lst delim)
+  "Return the elements of LST delimited by DELIM, such that the
+resulting list is of an odd length and every second element is DELIM."
+  (if (<= (length lst) 1)
+      lst
+      (cons* (car lst)
+             delim
+             (intersperse (cdr lst) delim))))
+
 (define (my-post-template post)
   `((header
      (h1 ,(post-ref post 'title))
           `(p (@ (class "updated"))
               "Updated "
               ,(date->string (post-ref post 'updated)
-                             my-date-format)) ""))
+                             my-date-format)) '())
+     ,(if (post-ref post 'tags)
+          `(p (@ (class "tags"))
+              "Tagged "
+              ,@(intersperse
+                 (map (lambda (tag)
+                        (aa tag (tag-uri my-tag-prefix tag)))
+                      (post-ref post 'tags))
+                 ", "))
+          '()))
     ,(post-sxml post)
     (p (@ (class "muted inbox"))
        "Have a question or comment?  Start a discussion in my "
                   "https://man.sr.ht/lists.sr.ht/etiquette.md") "]")
        ".")))
 
-(define (my-collection-template site title posts prefix)
+(define* (my-collection-template site title posts prefix
+                                 #:optional all-posts tag)
   (define (post-uri post)
     (string-append (or prefix "") "/"
                    (site-post-slug site post) ".html"))
 
-  `((h2 ,title)
+  `((h2 ,title
+        ,(if tag
+             (aa `(img (@ (class "feed-icon")
+                          (src "/feed.svg")
+                          (alt "subscribe to atom feed")))
+                 (tag-uri my-tag-prefix tag ".xml"))
+             '()))
     (table
      (@ (class "post-list"))
      (tbody
                  (td (@ (style "font-size: 0.875em;"))
                      ,(date->string (post-date post)
                                     my-date-format))))
-             posts)))))
+             posts)))
+    (h2 "Tags")
+    ,(tag-links (or all-posts posts))
+    ,(if tag
+         '(a (@ (href "/notes.html"))
+             "← all posts")
+         '())))
 
 (define bandali-theme
   (theme #:name "bandali"
          "images"))
     (h2 (@ (id "notes")) "Notes")
     (p "Here are notes about a variety of topics and issues I care "
-       "about.  They’re also available via " ,(aa "Atom" "feed.atom")
-       " and " ,(aa "RSS" "feed.rss") " feeds.")
+       "about.  They’re also available via " ,(aa "Atom" "notes.atom")
+       " and " ,(aa "RSS" "notes.rss") " feeds.")
     (table
      (@ (class "post-list"))
      (tbody
                              #:collections
                              `(("Notes" "notes.html"
                                 ,posts/reverse-chronological)))
+                       (tag-pages)
                        index-page
                        (atom-feed
-                        #:file-name "feed.atom")
+                        #:file-name "notes.atom")
                        (atom-feeds-by-tag
-                        #:prefix "tags")
+                        #:prefix my-tag-prefix)
                        (rss-feed
-                        #:file-name "feed.rss")
+                        #:file-name "notes.rss")
                        contact-page
                        cv-page
                        license-page