~aasg/haunted-blog

ref: eaecc6f5047916811c55a564be2bf3f78a6aab1e haunted-blog/haunt.scm -rw-r--r-- 10.1 KiB
eaecc6f5 — Aluísio Augusto Silva Gonçalves css: Apply special navbar style 1 year, 4 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
; vim: set fdm=marker fmr=;+,;- :

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

(use-modules (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 regex)
             (ice-9 textual-ports)
             (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 (output-directory)
  (or (getenv "HAUNT_DESTDIR") "site"))

;----------------
;+ 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 icon-name uri)
  `(a (@ (rel "me")
         (href ,uri))
      ,(embedded-icon icon-name)))

(define (hcard name url)
  (cond
      ((and name url)
       `(a (@ (class "p-author h-card")
              (href ,url))
           ,name))
      (name
        `(span (@ (class "p-author h-card")) ,name))
      (else #f)))

;-------
;+ Theme

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

(define (aasg-layout site title body)
  `((doctype "html")
    (head
      (meta (@ (charset "utf-8")))
      (meta (@ (name "viewport")
               (content "width=device-width, initial-scale=1")))
      (title ,(string-append title " — " (site-title site)))
      (base (@ (href ".")))
      (link (@ (rel "stylesheet")
               (href "css/a11y.css")))
      (link (@ (rel "stylesheet")
               (href "css/classless.css")))
      (link (@ (rel "stylesheet")
               (href "css/site.css"))))
    (body
      (header
        (nav (@ (class "profile-links"))
             (ul
               (li ,(link-as (embedded-icon 'signature) "index.html"))
               (li ,(profile-link 'email "mailto:aluisio@aasg.name"))
               (li ,(profile-link 'matrix "https://matrix.to/#/@aasg:aasg.name"))
               (li ,(profile-link 'github "https://github.com/AluisioASG"))
               (li ,(profile-link 'gitlab "https://gitlab.com/AluisioASG"))
               (li ,(profile-link 'linkedin "https://www.linkedin.com/in/aasg/")))))
      ,body
      (footer (p
                (a (@ (rel "license")
                      (href "https://creativecommons.org/licenses/by-sa/4.0/")
                      (title "Creative Commons Attribution-ShareAlike 4.0 International")
                      (aria-label "Creative Commons Attribution-ShareAlike 4.0 International"))
                   "\U01F16D\U01F16F\U01F10E")
                " Aluísio Augusto Silva Gonçalves. "
                ,(link-as `("Built with, powered by, licensed under, "
                            (i "et cetera, et cetera, et cetera")
                            ".")
                          "about-site.html"))))))

(define (author-hcard post)
  (let* ((author-name (post-ref post 'author))
         (author-url (post-ref post 'author-profile)))
    (hcard author-name author-url)))

(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 (aasg-post-template post)
  `(article (@ (class "h-entry"))
            (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)))
            (h1 (@ (class "p-name")) ,(post-ref post 'title))
            (div (@ (class "e-content")) ,(post-sxml post))
            (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)))))

(define (aasg-page-template post)
  `(article (@ (class "h-entry"))
            (h1 (@ (class "p-name")) ,(post-ref post 'title))
            (div (@ (class "e-content")) ,(post-sxml post))))

(define (aasg-collection-template site title posts prefix)
  (define (post-uri post)
    (string-append (or prefix "") (site-post-slug site post) ".html"))
  (define %preamble
    (post-sxml (read-post extended-markdown-reader "posts/_index.md")))
  `(,%preamble
     (h1 ,title)
     ,(map (lambda (post)
             `(article
                (h2 ,(link-as (post-ref post 'title) (post-uri post)))
                (time ,(date->string (post-date post) "~1"))
                ,(first-paragraph post)
                ,(link-as "continue…" (post-uri post))))
           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 (transform-markdown-tree sxml)
  (define (parse-heading-attributes . sxml)
    (define (extract-attributes str)
      (let ((match (string-match "[[:space:]]*\\{([^\\{\\}]*)\\}$" str)))
        (if match
            (values
              (match:prefix match)
              (string-split (match:substring match 1) char-whitespace?))
            (values str '()))))
    (define (append-attribute str attrs)
      (let ((id (string-match "#([^[:space:]]+)" str)))
        (append attrs (cond (id (list 'id (match:substring id 1)))
                            (else '())))))
    (define (deserialize-attribute-string str)
      (let-values (((before attrs) (extract-attributes str)))
        (values before (fold append-attribute '() attrs))))
    (match sxml
           ((tag middle ... tail)
            (let*-values (((before attrs) (deserialize-attribute-string tail))
                          ((wrapped-attrs) (if (null? attrs) '() (list '@ attrs))))
              `(,tag ,wrapped-attrs ,@middle ,before)))
           (_ sxml)))
  (define (remove-single-paragraphs-from-lists . sxml)
    (match sxml
           (`(li (p ,text ...))
            `(li ,text))
           (`(li (p ,text ...) (ul ,sublist))
            `(li ,text (ul ,sublist)))
           (_ sxml)))
  (pre-post-order sxml `((h1 . ,parse-heading-attributes)
                         (h2 . ,parse-heading-attributes)
                         (h3 . ,parse-heading-attributes)
                         (h4 . ,parse-heading-attributes)
                         (h5 . ,parse-heading-attributes)
                         (h6 . ,parse-heading-attributes)
                         (li . ,remove-single-paragraphs-from-lists)
                        (*default* . ,(lambda (. sxml) sxml))
                        (*text* . ,(lambda (_ text) text)))))

(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) (transform-markdown-tree orig-tree)))
        (values metadata new-tree)))))

(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"))
      #:file-filter (lambda (filename) (not (aasg-file-fragment? filename)))
      #:readers (list extended-markdown-reader)
      #:builders (list (blog #:theme aasg-post-theme)
                       (uncollected-pages "pages" aasg-page-theme)
                       (atom-feed)
                       (static-directory "static/css" "css")
                       (static-directory "static/fonts" "fonts"))
      #:build-directory (output-directory)
      #:make-slug aasg-post-slug)