~aasg/haunted-blog

ref: e20dc282fae04bd2f99bd8001f203a7c5b498dce haunted-blog/haunt.scm -rw-r--r-- 13.3 KiB
e20dc282 — Aluísio Augusto Silva Gonçalves posts/lri-log-2021w21: New post 2 months ago
                                                                                
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
; vim: set fdm=marker fmr=;+,;- :
; SPDX-FileCopyrightText: 2018 David Thompson
; SPDX-FileCopyrightText: 2020 Aluísio Augusto Silva Gonçalves <https://aasg.name>
; SPDX-License-Identifier: GPL-3.0-or-later

;---------
;+ Imports

(add-to-load-path (string-append (dirname (current-filename)) "/scm"))
(use-modules (aasg front-matter json)
             (aasg sxml from-pandoc)
             (aasg sxml reparse-extended-markdown)
             (haunt asset)
             (haunt builder blog)
             (haunt builder atom)
             (haunt builder assets)
             (haunt html)
             (haunt reader)
             (haunt reader commonmark)
             (haunt page)
             (haunt post)
             (haunt site)
             (ice-9 match)
             (ice-9 popen)
             (ice-9 regex)
             (ice-9 rdelim)
             (ice-9 textual-ports)
             (ice-9 vlist)
             (srfi srfi-1)
             (srfi srfi-11)
             (srfi srfi-19)
             (srfi srfi-26)
             (sxml match)
             (sxml simple)
             (sxml transform)
             (web uri))

;----------------
;+ I/O procedures

(define (read-asset path)
  (call-with-input-file path get-string-all))

(define (get-non-empty-envvar name)
  (let ((value (getenv name)))
    (if (or (nil? value) (string-null? value)) #f value)))

(define (output-directory)
  (or (get-non-empty-envvar "HAUNT_DESTDIR") "site"))

(define (site-rev)
  (get-non-empty-envvar "GIT_COMMIT"))

;----------------
;+ Markup helpers

(define (prefix-sxml prefix tree)
  (if tree (list prefix tree) `()))

(define (humanize-url url)
  ; TODO: look at sindresorhus' humanize-url and normalize-url
  ; if we ever actually use this
  url)

(define (link-as name uri)
  `(a (@ (href ,uri)) ,name))

(define (embedded-icon name)
  (let ((icon-path (string-append "assets/icons/" (symbol->string name) ".svg")))
    `(raw ,(read-asset icon-path))))

(define (profile-link name uri)
  `(a (@ (rel "me")
         (href ,uri)
         (aria-label ,name))
      ,(embedded-icon (string->symbol (string-downcase name)))
      (span ,name)))

(define* (hcard name url #:key (extra-classes '()))
  (let ((classes (string-join (append extra-classes '("h-card")) " ")))
    (cond
      ((and name url)
       `(a (@ (class ,classes)
              (href ,url))
           ,name))
      (name
        `(span (@ (class ,classes)) ,name))
      (else #f))))

;-------
;+ Theme

(define (first-paragraph post)
  (let loop ((sxml (post-sxml post))
             (result '()))
    (match sxml
           (() (reverse result))
           ((('details _ ...) paragraph ...)
            (loop paragraph result))
           ((or (('p ...) _ ...) (paragraph _ ...))
            (reverse (cons paragraph result)))
           ((head . tail)
            (loop tail (cons head result))))))

(define (aasg-layout site title body)
  `((doctype "html")
    (html (@ (lang "en"))
          (head
            (meta (@ (charset "utf-8")))
            (meta (@ (name "viewport")
                     (content "width=device-width, initial-scale=1")))
            (title ,(string-append title " — " (site-title site)))
            (link (@ (rel "alternate")
                     (title "Posts")
                     (type "application/atom+xml")
                     (href "feed.xml")))
            (link (@ (rel "stylesheet")
                     (href "css/a11y.css")))
            (link (@ (rel "stylesheet")
                     (href "css/classless.css")))
            (link (@ (rel "stylesheet")
                     (href "css/site.css"))))
          (body
            (p (@ (class "visually-hidden"))
               (a (@ (href "#content")) "Skip to content"))
            (header
              (nav (@ (class "profile-links"))
                   (ul
                     (li (a (@ (href "index.html")
                               (aria-label "Home"))
                            ,(embedded-icon 'signature)
                            (span "~aasg")))
                     (li ,(profile-link "GitHub" "https://github.com/AluisioASG"))
                     (li ,(profile-link "GitLab" "https://gitlab.com/AluisioASG"))
                     (li ,(profile-link "sourcehut" "https://sr.ht/~aasg"))
                     (li ,(profile-link "LinkedIn" "https://www.linkedin.com/in/aasg/")))))
            ,body
            (footer
              (p (@ (xmlns:cc "http://creativecommons.org/ns#") (xmlns:dct "http://purl.org/dc/terms/"))
                 (a (@ (href "https://aasg.name")
                       (rel "cc:attributionURL")
                       (property "dct:title")) "aasg.name")
                 ,(let ((commit (site-rev)))
                    (if (nil? commit) '()
                        `(a (@ (rel "noopener noreferrer")
                               (href ,(string-append "https://git.sr.ht/~aasg/haunted-blog/tree/" commit)))
                            "#" ,commit)))
                 " by "
                 (span (@ (rel "cc:attributionName")) "Aluísio Augusto Silva Gonçalves")
                 " is licensed under a "
                 (a (@ (href "https://creativecommons.org/licenses/by-sa/4.0/")
                       (rel "license noopener noreferrer"))
                    (span (@ (aria-hidden "true")) "\U01F16D\U01F16F\U01F10E ")
                    "Creative Commons Attribution-ShareAlike 4.0 International")
                 " license. "
                 ,(link-as "Additional licensing options and details about the site's tech stack"
                           "about-site.html")
                 " are also available."))))))

(define (author-hcard post)
  (let* ((author-name (post-ref post 'author))
         (author-url (post-ref post 'author-profile)))
    (hcard author-name author-url #:extra-classes '("p-author"))))

(define (cite-if-reply post)
  (let* ((irt-url (post-ref post 'in-reply-to))
         (irt-desc (post-ref post 'irt-description))
         (irt-title (post-ref post 'irt-title))
         (irt-author-name (post-ref post 'irt-author))
         (irt-author-url (post-ref post 'irt-author-profile)))
    (if (not irt-url) #f
        `(span (@ (class "u-in-reply-to h-cite"))
               (cite
                 (a (@ (class ,(if irt-title "u-url p-name" "u-url"))
                       (href ,irt-url))
                    ,(or irt-title irt-desc (humanize-url irt-url)))
                 ,(prefix-sxml " by " (hcard irt-author-name irt-author-url)))))))

(define (post-comments-url post)
  (string-append "https://lists.sr.ht/~aasg/public-inbox?search="
                 (uri-encode (post-ref post 'title))))

(define (post-comments-email post)
  (string-append "mailto:aasg/public-inbox@lists.sr.ht?subject="
                 (uri-encode (string-append "Re: " (post-ref post 'title)))))

(define (filed-under post)
  (define (tag-link tag)
    `(span (@ (class "p-category")) ,tag))
  (let ((tags (post-ref post 'tags)))
    (if (or (not tags) (null? tags)) #f
        (cdr (reverse (fold (lambda (tag sxml)
                              (cons* (tag-link tag) ", " sxml)) '() tags))))))

(define (content-block post)
  (let* ((lang (post-ref post 'lang))
         (header-attrs (if (not lang)
                           `(@ (class "p-name"))
                           `(@ (class "p-name") (lang ,lang))))
         (body-attrs (if (not lang)
                         `(@ (id "content") (class "e-content"))
                         `(@ (id "content") (class "e-content") (lang ,lang)))))
    `((h1 ,header-attrs ,(post-ref post 'title))
      (div ,body-attrs ,(post-sxml post)))))

(define (aasg-post-template post)
  (define %post-metadata
    `(small (@ (class "post-info"))
            "Published on "
            (time (@ (class "dt-published")) ,(date->string (post-date post) "~1"))
            " by "
            ,(author-hcard post)
            ,(prefix-sxml " in reply to " (cite-if-reply post))
            ,(prefix-sxml ". Filed under " (filed-under post))
            "."))
  (define %comments-link
    `(p (@ (class "post-comments"))
        ,(link-as "View comments to this post" (post-comments-url post))
        " or "
        ,(link-as "send your own" (post-comments-email post))))
  `(article (@ (class "h-entry"))
            ,%post-metadata
            ,@(content-block post)
            ,%comments-link))

(define (aasg-page-template post)
  `(article (@ (class "h-entry"))
            ,@(content-block post)))

(define (taglist post)
  (define (tag-link tag)
    `(span (@ (class "p-category")
              (aria-label ,(string-append "tagged: " tag))) ,tag))
  (let ((tags (post-ref post 'tags)))
    (if (or (not tags) (null? tags)) '()
        (cdr (reverse (fold (lambda (tag sxml)
                              (cons* (tag-link tag) " " sxml)) '() tags))))))

(define (aasg-collection-template site title posts prefix)
  (define (post-uri post)
    (string-append (or prefix "") (site-post-slug site post) ".html"))
  (define (post-entry post)
    `(article (@ (class "h-entry"))
              (div (@ (class "post-info-card"))
                   (h3 (@ (class "p-name"))
                       (a (@ (class "u-url")
                             (href ,(post-uri post)))
                          ,(post-ref post 'title)))
                   (time (@ (class "dt-published"))
                         ,(date->string (post-date post) "~1"))
                   ,(taglist post))
              (div (@ (class "p-summary"))
                   ,(first-paragraph post))
              ,(link-as "continue…" (post-uri post))))
  (define %preamble
    (post-sxml (read-post html-reader "pages/_index.html")))
  `(,%preamble
     (h2 "Recent posts")
     ,(map post-entry posts)))

(define aasg-post-theme
  (theme #:name "aasg-posts"
         #:layout aasg-layout
         #:post-template aasg-post-template
         #:collection-template aasg-collection-template))

(define aasg-page-theme
  (theme #:name "aasg-pages"
         #:layout aasg-layout
         #:post-template aasg-page-template))

;---------------------
;+ Readers and writers

(define extended-markdown-reader
  (make-reader
    (make-file-extension-matcher "md")
    (lambda (filename)
      (let*-values (((read-commonmark) (reader-proc commonmark-reader))
                    ((metadata orig-tree) (read-commonmark filename))
                    ((new-tree) (reparse-markdown-sxml orig-tree)))
        (values metadata new-tree)))))

(define (make-pandoc-reader)
  (define (parse-date datestring)
    (let loop ((formats '("~Y-~m-~dT~H:~M:~S~z"
                          "~Y-~m-~d ~H:~M:~S"
                          "~Y-~m-~d")))
      (when (null? formats)
        (error "couldn't parse date string: " datestring))
      (catch 'misc-error
             (lambda () (string->date datestring (car formats)))
             (lambda (key caller msg args rest) (loop (cdr formats))))))
  (define (read-front-matter filename)
    (map (lambda (kv)
           (let ((key (string->symbol (car kv)))
                 (value (cdr kv)))
             (cond
               ((eq? 'date key)
                (cons key (parse-date value)))
               ((vector? value)
                (cons key (vector->list value)))
               (else
                 (cons key value)))))
         (call-with-input-file filename read-json-front-matter)))
  (make-reader
    (make-file-extension-matcher "md")
    (lambda (filename)
      (values (read-front-matter filename) (pandoc-md->sxml filename)))))
(define pandoc-reader (make-pandoc-reader))

; Wrap a Haunt reader so that its output includes a table of contents.
(define (with-toc reader)
  (define (add-toc sxml) (cons (sxml-toc sxml) sxml))
  (define (read-and-add-toc reader filename)
    (let-values (((metadata sxml) (reader filename)))
      (if (assq-ref metadata 'toc)
          (values metadata (add-toc sxml))
          (values metadata sxml))))
  (let ((matcher (reader-matcher reader))
        (reader (reader-proc reader)))
    (make-reader matcher (cute read-and-add-toc reader <>))))

(define (uncollected-pages directory theme)
  (lambda (site posts)
    (let ((post->page (lambda (post)
                        (let ((output-name (string-append (site-post-slug site post) ".html"))
                              (rendered-post (render-post theme site post)))
                          (make-page output-name rendered-post sxml->html))))
          (posts (read-posts directory
                             (site-file-filter site)
                             (site-readers site)
                             (site-default-metadata site))))
      (map post->page posts))))

;-----------------
;+ Site definition

(define (aasg-post-slug post)
  (or
    (post-ref post 'slug)
    (basename (post-file-name post) ".md")))

(define (aasg-file-fragment? filename)
  (string-prefix? "_" (basename filename)))

(site #:title "aasg's most experimental weblog"
      #:domain "aasg.name"
      #:default-metadata
      '((author . "Aluísio Augusto Silva Gonçalves")
        (author-profile . "https://aasg.name"))
      #:file-filter (lambda (filename) (not (aasg-file-fragment? filename)))
      #:readers (list pandoc-reader)
      #:builders (list (blog #:theme aasg-post-theme
                             #:collections `(("Home" "index.html" ,posts/reverse-chronological)))
                       (uncollected-pages "pages" aasg-page-theme)
                       (atom-feed)
                       (static-directory "static" ""))
      #:build-directory (output-directory)
      #:make-slug aasg-post-slug)