blog/haunt/jakob/builder/outbox.scm -rw-r--r-- 4.6 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.org>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (jakob builder outbox)
  #:use-module (haunt html)
  #:use-module (haunt page)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (jakob theme)
  #:use-module (jakob utils pagination)
  #:use-module (jakob utils sxml)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-19)
  #:export (outbox))

;;; Commentary:
;;;
;;; [TODO]
;;;
;;; Code:

;; Prefix for all pages representing Webmention interactions.
(define %outbox-prefix "/outbox")


;;;
;;; Profile.
;;;

(define %h-card
  `(div (@ (class "u-author h-card"))
        (img (@ (class "u-photo")
                (src "/static/image/profile-picture.jpg")
                (width "40")))
        (a (@ (class "u-url p-name")
              (href "http://jakob.space"))
           "Jakob L. Kreuze")))


;;;
;;; Common rendering code.
;;;

(define (datetime uri date)
  `(p (a (@ (class "u-url") (href uri))
         (time (@ (class "dt-published")
                  (datetime ,(date->string date "~4")))
               ,(date->string date "~B ~e, ~Y")))))


;;;
;;; Record type for replies -- by far, my most frequently-used type of
;;; Webmention response.
;;;

(define-record-type <reply>
  (make-reply content date target-uri target-handle)
  reply?
  (content reply-content)
  (date reply-date)
  (target-uri reply-target-uri)
  (target-handle reply-target-handle))

(define (reply-uri reply)
  (let* ((date (date->string (reply-date reply) "~Y-~m-~d-~H:~M:~S"))
         (target (reply-target-handle reply))
         (slug (format #f "reply-~a-~a" target date)))
    (string-append %outbox-prefix "/" slug ".html")))

(define reply
  (match-lambda
    ((target-uri target-handle date-string content)
     (let ((date (string->date date-string "~Y-~m-~dT~H:~M:~S~z")))
       (make-reply content date target-uri target-handle)))))


;;;
;;; Reply rendering.
;;;

(define (render-reply reply)
  (let ((content (cons* (car (reply-content reply))
                        `(@ (class "e-content"))
                        (cdr (reply-content reply)))))
    `(div (@ (class "h-entry"))
          ,%h-card
          (p "In reply to: "
             (a (@ (class "u-in-reply-to")
                   (href ,(reply-target-uri reply)))
                ,(reply-target-handle reply)))
          ,content
          ,(datetime (reply-uri reply) (reply-date reply)))))

(define (render-preview reply)
  (let* ((simple-text? (eqv? 'p (car (reply-content reply))))
         (truncated? (and simple-text?
                          (> (length (cdr (reply-content reply))) 80)))
         (preview (if simple-text?
                      (if truncated?
                          (format #f "~a..."
                                  (substring (cdr (reply-content reply))
                                             0 80))
                          (cdr (reply-content reply)))
                      "[No preview available...]")))
    `(section
      (h2 ,(hyperlink
            (reply-uri reply)
            (format #f "Reply directed towards ~a on ~a"
                    (reply-target-handle reply)
                    (date->string (reply-date reply) "~B ~e, ~Y"))))
      (p ,preview))))

(define (reply->page reply)
  (let ((title (format #f "Reply to ~a" (reply-target-handle reply))))
    (make-page (reply-uri reply)
               (theme #:title title
                      ;; #:description (first-paragraph post)
                      ;; #:keywords (post-ref post 'tags)
                      #:content (render-reply reply))
               sxml->html)))


;;;
;;; Builder.
;;;

(define (outbox)
  (let ((replies (map reply (primitive-load "data/replies.scm"))))
    (lambda (site posts)
      (append
       ;; Permalinks.
       (map reply->page replies)

       ;; Outbox listing.
       (items->pages render-preview
                     (reverse replies)
                     "Webmentions"
                     (string-append %outbox-prefix "/" "index")
                     #:items-per-page 50)))))