|
8 | 8 | "CSS for the site.") |
9 | 9 |
|
10 | 10 | (opts:define-opts |
11 | | - (:name :help |
12 | | - :description "`generate` is the only available command at the moment." |
13 | | - :short #\h |
14 | | - :long "help")) |
| 11 | + (:name :help |
| 12 | + :description "`generate` is the only available command at the moment." |
| 13 | + :short #\h |
| 14 | + :long "help")) |
15 | 15 |
|
16 | 16 | (defun concat (&rest strings) |
17 | 17 | "Wrapper around the more cumbersome concatenate form." |
|
39 | 39 | (defun parse-post (post) |
40 | 40 | "Create list that contains the data from each JSON file combined with the body of every post." |
41 | 41 | (let* ((json (uiop:read-file-string (car (cdr post)))) |
42 | | - (list-json (json:decode-json-from-string json))) |
| 42 | + (list-json (json:decode-json-from-string json))) |
43 | 43 | `(,@list-json (:content . ,(post-for-slug (cdr (assoc :slug list-json))))))) |
44 | 44 |
|
45 | 45 | (defun full-path-as-string (dir) |
|
69 | 69 | (defun write-file (contents file) |
70 | 70 | "Write CONTENTS to FILE." |
71 | 71 | (with-open-file (stream file |
72 | | - :direction :output |
73 | | - :if-exists :supersede) |
| 72 | + :direction :output |
| 73 | + :if-exists :supersede) |
74 | 74 | (write-sequence contents stream))) |
75 | 75 |
|
76 | 76 | (defun post-for-slug (slug) |
|
85 | 85 | (defun gen-posts () |
86 | 86 | "Generate posts from post data, templates, and css file(s)." |
87 | 87 | (if (uiop:file-exists-p "templates/post.mustache") |
88 | | - (let ((template (uiop:read-file-string "templates/post.mustache")) |
89 | | - post |
90 | | - rendered) |
91 | | - (dolist (pair posts) |
92 | | - (setf post `((:content . ,(cdr (assoc :content pair))) |
93 | | - (:pub_date . ,(cdr (assoc :published pair))) |
94 | | - (:mod_date . ,(cdr (assoc :modified pair))) |
95 | | - (:modifiedDate . ,(parse-date (cdr (assoc :modified pair)))) |
96 | | - (:formattedDate . ,(parse-date (cdr (assoc :published pair)))) |
97 | | - (:link . ,(cdr (assoc :link pair))) |
98 | | - (:description . ,(cdr (assoc :excerpt pair))) |
99 | | - (:slug . ,(concat "/writing/" |
100 | | - (cdr (assoc :slug pair)))) |
101 | | - (:css . ,css) |
102 | | - (:title . ,(cdr (assoc :title pair))))) |
103 | | - |
104 | | - (setf rendered (mustache:render* template post)) |
105 | | - (write-file rendered (concat |
106 | | - "site/writing/" |
107 | | - (cdr (assoc :slug pair)) |
108 | | - ".html")))) |
109 | | - (print "No post.mustache template found. Create it in templates/."))) |
| 88 | + (let ((template (uiop:read-file-string "templates/post.mustache")) |
| 89 | + post |
| 90 | + rendered) |
| 91 | + (dolist (pair posts) |
| 92 | + (setf post `((:content . ,(cdr (assoc :content pair))) |
| 93 | + (:pub_date . ,(cdr (assoc :published pair))) |
| 94 | + (:mod_date . ,(cdr (assoc :modified pair))) |
| 95 | + (:modifiedDate . ,(parse-date (cdr (assoc :modified pair)))) |
| 96 | + (:formattedDate . ,(parse-date (cdr (assoc :published pair)))) |
| 97 | + (:link . ,(cdr (assoc :link pair))) |
| 98 | + (:description . ,(cdr (assoc :excerpt pair))) |
| 99 | + (:slug . ,(concat "/writing/" |
| 100 | + (cdr (assoc :slug pair)))) |
| 101 | + (:css . ,css) |
| 102 | + (:title . ,(cdr (assoc :title pair))))) |
| 103 | + |
| 104 | + (setf rendered (mustache:render* template post)) |
| 105 | + (write-file rendered (concat |
| 106 | + "site/writing/" |
| 107 | + (cdr (assoc :slug pair)) |
| 108 | + ".html")))) |
| 109 | + (print "No post.mustache template found. Create it in templates/."))) |
110 | 110 |
|
111 | 111 | (defun gen-archive () |
112 | 112 | "Create archive type pages." |
113 | 113 | (if (and (uiop:file-exists-p "pages/archive.mustache") |
114 | 114 | (uiop:file-exists-p "pages/archive.json")) |
115 | 115 | (let* ((template (uiop:read-file-string "pages/archive.mustache")) |
116 | | - (data (json:decode-json-from-string (uiop:read-file-string "pages/archive.json"))) |
117 | | - (css `(:css . ,css)) |
118 | | - (limit (cdr (assoc :paginate data))) |
119 | | - (path (concat "site" (cdr (assoc :path data)))) |
120 | | - page |
121 | | - times |
122 | | - pagination) |
| 116 | + (data (json:decode-json-from-string (uiop:read-file-string "pages/archive.json"))) |
| 117 | + (css `(:css . ,css)) |
| 118 | + (limit (cdr (assoc :paginate data))) |
| 119 | + (path (concat "site" (cdr (assoc :path data)))) |
| 120 | + page |
| 121 | + times |
| 122 | + pagination) |
123 | 123 | (ensure-directories-exist path) |
124 | 124 | (if (> limit 0) |
125 | 125 | (progn |
126 | 126 | (setf times (+ (floor (length posts) limit) 1)) |
127 | 127 | (dotimes (i times) |
128 | 128 | (setf page (concat path |
129 | | - (write-to-string (+ 1 i)) |
130 | | - ".html")) |
| 129 | + (write-to-string (+ 1 i)) |
| 130 | + ".html")) |
131 | 131 | (setf pagination (gen-pagination-for-archive (+ i 1) times)) |
132 | 132 | (when (= i (- times 1)) |
133 | 133 | (write-file (mustache:render* template |
|
146 | 146 | (* i limit) |
147 | 147 | (+ (* i limit) limit))))) |
148 | 148 | page)))) |
149 | | - (write-file (mustache:render* template |
150 | | - `(,css |
151 | | - (:posts . ,posts))) |
152 | | - (concat path ".html")))) |
153 | | - (print "The files for generating an archive are missing. Create a archive.mustache file and a archive.json file in pages/."))) |
| 149 | + (write-file (mustache:render* template |
| 150 | + `(,css |
| 151 | + (:posts . ,posts))) |
| 152 | + (concat path ".html")))) |
| 153 | + (print "The files for generating an archive are missing. Create a archive.mustache file and a archive.json file in pages/."))) |
154 | 154 |
|
155 | 155 | (defun gen-pagination-for-archive (index limit) |
156 | 156 | "Given INDEX and LIMIT this will return an alist of values for pagination." |
|
178 | 178 | (defun gen-index() |
179 | 179 | (if (uiop:file-exists-p "templates/index.mustache") |
180 | 180 | (let* ((template (uiop:read-file-string "templates/index.mustache")) |
181 | | - (posts (subseq posts 0 10)) |
182 | | - (rendered (mustache:render* template `((:posts . ,posts) (:css . ,css))))) |
| 181 | + (posts (subseq posts 0 10)) |
| 182 | + (rendered (mustache:render* template `((:posts . ,posts) (:css . ,css))))) |
183 | 183 | (write-file rendered "site/index.html")) |
184 | | - (print "No index.mustache file found. Create a mustache file named index.mustache in templates/."))) |
| 184 | + (print "No index.mustache file found. Create a mustache file named index.mustache in templates/."))) |
185 | 185 |
|
186 | 186 | (defun gen-pages () |
187 | 187 | "Generate any markdown files in the pages/ dir using matching JSON files as context." |
188 | 188 | (if (uiop:file-exists-p "templates/page.mustache") |
189 | | - (let ((pages (uiop:directory-files "pages/" "*.md")) |
190 | | - (css `(:css . ,css)) |
191 | | - (template (uiop:read-file-string "templates/page.mustache")) |
192 | | - data |
193 | | - content) |
194 | | - (dolist (page pages) |
195 | | - (setf data (json:decode-json-from-string (uiop:read-file-string |
196 | | - (concat "pages/" |
197 | | - (file-basename page) |
198 | | - ".json")))) |
199 | | - (setf content (with-output-to-string (p) |
200 | | - (3bmd:parse-string-and-print-to-stream (uiop:read-file-string page) p))) |
201 | | - (ensure-directories-exist (concat "site/" (cdr (assoc :permalink data)))) |
202 | | - (write-file (mustache:render* template `((:slug . ,(cdr (assoc :permalink data))) |
203 | | - ,css |
204 | | - ,@data |
205 | | - (:content . ,content))) |
206 | | - (concat "site/" (cdr (assoc :permalink data)) ".html")))) |
207 | | - (print "No page.mustache file found. Please create one in templates/."))) |
| 189 | + (let ((pages (uiop:directory-files "pages/" "*.md")) |
| 190 | + (css `(:css . ,css)) |
| 191 | + (template (uiop:read-file-string "templates/page.mustache")) |
| 192 | + data |
| 193 | + content) |
| 194 | + (dolist (page pages) |
| 195 | + (setf data (json:decode-json-from-string (uiop:read-file-string |
| 196 | + (concat "pages/" |
| 197 | + (file-basename page) |
| 198 | + ".json")))) |
| 199 | + (setf content (with-output-to-string (p) |
| 200 | + (3bmd:parse-string-and-print-to-stream (uiop:read-file-string page) p))) |
| 201 | + (ensure-directories-exist (concat "site/" (cdr (assoc :permalink data)))) |
| 202 | + (write-file (mustache:render* template `((:slug . ,(cdr (assoc :permalink data))) |
| 203 | + ,css |
| 204 | + ,@data |
| 205 | + (:content . ,content))) |
| 206 | + (concat "site/" (cdr (assoc :permalink data)) ".html")))) |
| 207 | + (print "No page.mustache file found. Please create one in templates/."))) |
208 | 208 |
|
209 | 209 | (defun return-leading-zero-as-string (number) |
210 | 210 | (if (< number 10) |
211 | 211 | (concat "0" (write-to-string number)) |
212 | | - (write-to-string number))) |
| 212 | + (write-to-string number))) |
213 | 213 |
|
214 | 214 | (defun now-as-rfc-822 () |
215 | 215 | (date-as-rfc-822 (local-time:format-timestring nil (local-time:now)))) |
|
257 | 257 |
|
258 | 258 | (defun gen-rss () |
259 | 259 | (if (uiop:file-exists-p "templates/rss.mustache") |
260 | | - (let* ((posts (subseq posts 0 20)) |
261 | | - (now (now-as-rfc-822)) |
262 | | - (template (uiop:read-file-string "templates/rss.mustache")) |
263 | | - (proper-posts (mapcar 'format-data-for-rss posts))) |
264 | | - (write-file (mustache:render* template `((:now . ,now) (:posts . ,proper-posts))) "site/rss.xml")) |
265 | | - (print "No rss template found. Please create one in templates/."))) |
| 260 | + (let* ((posts (subseq posts 0 20)) |
| 261 | + (now (now-as-rfc-822)) |
| 262 | + (template (uiop:read-file-string "templates/rss.mustache")) |
| 263 | + (proper-posts (mapcar 'format-data-for-rss posts))) |
| 264 | + (write-file (mustache:render* template `((:now . ,now) (:posts . ,proper-posts))) "site/rss.xml")) |
| 265 | + (print "No rss template found. Please create one in templates/."))) |
266 | 266 |
|
267 | 267 | (defun format-data-for-sitemap (post) |
268 | 268 | `((:slug . ,(cdr (assoc :slug post))) (:date . ,(cdr (assoc :published post))))) |
|
277 | 277 |
|
278 | 278 | (defun gen-sitemap () |
279 | 279 | (if (uiop:file-exists-p "templates/sitemap.mustache") |
280 | | - (let ((proper-posts (mapcar 'format-data-for-sitemap posts)) |
281 | | - (pages (get-page-slugs)) |
282 | | - (template (uiop:read-file-string "templates/sitemap.mustache"))) |
283 | | - (write-file (mustache:render* |
284 | | - template |
285 | | - `((:posts . ,proper-posts) (:pages . ,pages))) |
286 | | - "site/sitemap.xml")) |
287 | | - (print "No sitemap.mustache template found. Please create one in templates/."))) |
| 280 | + (let ((proper-posts (mapcar 'format-data-for-sitemap posts)) |
| 281 | + (pages (get-page-slugs)) |
| 282 | + (template (uiop:read-file-string "templates/sitemap.mustache"))) |
| 283 | + (write-file (mustache:render* |
| 284 | + template |
| 285 | + `((:posts . ,proper-posts) (:pages . ,pages))) |
| 286 | + "site/sitemap.xml")) |
| 287 | + (print "No sitemap.mustache template found. Please create one in templates/."))) |
288 | 288 |
|
289 | 289 | (defun get-id() |
290 | 290 | "Get all JSON files representing all posts and return the next ID to use." |
|
319 | 319 | "The pipeline to build the site." |
320 | 320 |
|
321 | 321 | (if (equal (car (cdr (opts:argv))) "generate") |
322 | | - (generate-post (car (last (opts:argv)))) |
323 | | - (progn |
324 | | - (ensure-directories-exist "site/writing/") |
325 | | - (when (uiop:subdirectories "./templates") |
326 | | - (setf mustache:*load-path* `(,(namestring (car (uiop:subdirectories "./templates")))))) |
327 | | - (when (uiop:file-exists-p "site.css") |
328 | | - (setf css (uiop:read-file-string "site.css"))) |
329 | | - (setf mustache:*default-pathname-type* "mustache") |
330 | | - (setf 3bmd-code-blocks:*code-blocks* t) |
331 | | - (setf posts (reverse (sort (gen-data) |
332 | | - 'sort-by-ids |
333 | | - :key 'car))) |
334 | | - |
335 | | - (multiple-value-bind (options free-args) |
336 | | - (opts:get-opts) |
337 | | - (when options |
338 | | - (opts:describe))) |
339 | | - |
340 | | - (if (and css posts) |
| 322 | + (generate-post (car (last (opts:argv)))) |
341 | 323 | (progn |
342 | | - (copy-public) |
343 | | - (gen-archive) |
344 | | - (gen-index) |
345 | | - (gen-pages) |
346 | | - (gen-posts) |
347 | | - (gen-rss) |
348 | | - (gen-sitemap)) |
349 | | - (print "No posts found. Create a md file in posts/. Also create a site.css file in the root."))))) |
| 324 | + (ensure-directories-exist "site/writing/") |
| 325 | + (when (uiop:subdirectories "./templates") |
| 326 | + (setf mustache:*load-path* `(,(namestring (car (uiop:subdirectories "./templates")))))) |
| 327 | + (when (uiop:file-exists-p "site.css") |
| 328 | + (setf css (uiop:read-file-string "site.css"))) |
| 329 | + (setf mustache:*default-pathname-type* "mustache") |
| 330 | + (setf 3bmd-code-blocks:*code-blocks* t) |
| 331 | + (setf posts (reverse (sort (gen-data) |
| 332 | + 'sort-by-ids |
| 333 | + :key 'car))) |
| 334 | + |
| 335 | + (multiple-value-bind (options free-args) |
| 336 | + (opts:get-opts) |
| 337 | + (when options |
| 338 | + (opts:describe))) |
| 339 | + |
| 340 | + (if (and css posts) |
| 341 | + (progn |
| 342 | + (copy-public) |
| 343 | + (gen-archive) |
| 344 | + (gen-index) |
| 345 | + (gen-pages) |
| 346 | + (gen-posts) |
| 347 | + (gen-rss) |
| 348 | + (gen-sitemap)) |
| 349 | + (print "No posts found. Create a md file in posts/. Also create a site.css file in the root."))))) |
0 commit comments