|
7 | 7 | (defvar css nil |
8 | 8 | "CSS for the site.") |
9 | 9 |
|
| 10 | +(opts:define-opts |
| 11 | + (:name :help |
| 12 | + :description "`generate` is the only available command at the moment." |
| 13 | + :short #\h |
| 14 | + :long "help")) |
| 15 | + |
10 | 16 | (defun concat (&rest strings) |
11 | 17 | "Wrapper around the more cumbersome concatenate form." |
12 | 18 | (let (result) |
13 | 19 | (dolist (x strings) |
14 | 20 | (setf result (concatenate 'string result x))) |
15 | 21 | result)) |
16 | 22 |
|
| 23 | +(defun shell (cmd) |
| 24 | + "Return the output of CMD as a string." |
| 25 | + (multiple-value-bind (result) |
| 26 | + (uiop:run-program cmd :output '(:string :stripped t)) |
| 27 | + result)) |
| 28 | + |
17 | 29 | (defun gen-data () |
18 | 30 | "Read markdown posts from 'posts' dir and retrieve data from each matching json file." |
19 | 31 | (let* ((posts (uiop:directory-files "posts/" "*.md")) |
|
272 | 284 | "site/sitemap.xml")) |
273 | 285 | (print "No sitemap.mustache template found. Please create one in templates/."))) |
274 | 286 |
|
| 287 | +(defun get-id() |
| 288 | + "Get all JSON files representing all posts and return the next ID to use." |
| 289 | + (let ((files (uiop:directory-files "posts/" "*.json"))) |
| 290 | + (if files |
| 291 | + (+ 1 |
| 292 | + (car (sort |
| 293 | + (mapcar |
| 294 | + (lambda (file) |
| 295 | + (cdr (assoc :id (json:decode-json-from-string |
| 296 | + (uiop:read-file-string file))))) |
| 297 | + files) |
| 298 | + #'>))) |
| 299 | + 1))) |
| 300 | + |
| 301 | +(defun generate-post (title) |
| 302 | + "Take TITLE and create the necessary JSON and MD files for it." |
| 303 | + (let ((date (shell "date +%Y-%m-%dT%R:%S%:z")) |
| 304 | + (id (get-id)) |
| 305 | + (json-file (concat "./posts/" title ".json")) |
| 306 | + (md-file (concat "./posts/" title ".md"))) |
| 307 | + (write-file (json:encode-json-to-string `(("id" . ,id) |
| 308 | + ("published" . ,date) |
| 309 | + ("title" . ,title) |
| 310 | + ("slug" . ,title) |
| 311 | + ("modified" . ,date) |
| 312 | + ("excerpt" . ""))) |
| 313 | + json-file) |
| 314 | + (write-file title md-file))) |
| 315 | + |
275 | 316 | (defun main () |
276 | 317 | "The pipeline to build the site." |
277 | | - (ensure-directories-exist "site/writing/") |
278 | | - (when (uiop:subdirectories "./templates") |
279 | | - (setf mustache:*load-path* `(,(namestring (car (uiop:subdirectories "./templates")))))) |
280 | | - (when (uiop:file-exists-p "site.css") |
281 | | - (setf css (uiop:read-file-string "site.css"))) |
282 | | - (setf mustache:*default-pathname-type* "mustache") |
283 | | - (setf 3bmd-code-blocks:*code-blocks* t) |
284 | | - (setf posts (reverse (sort (gen-data) |
285 | | - 'sort-by-ids |
286 | | - :key 'car))) |
287 | | - (if (and css posts) |
288 | | - (progn |
289 | | - (copy-public) |
290 | | - (gen-archive) |
291 | | - (gen-index) |
292 | | - (gen-pages) |
293 | | - (gen-posts) |
294 | | - (gen-rss) |
295 | | - (gen-sitemap)) |
296 | | - (print "No posts found. Create a md file in posts/. Also create a site.css file in the root."))) |
| 318 | + |
| 319 | + (if (equal (car (cdr (opts:argv))) "generate") |
| 320 | + (generate-post (car (last (opts:argv)))) |
| 321 | + (progn |
| 322 | + (ensure-directories-exist "site/writing/") |
| 323 | + (when (uiop:subdirectories "./templates") |
| 324 | + (setf mustache:*load-path* `(,(namestring (car (uiop:subdirectories "./templates")))))) |
| 325 | + (when (uiop:file-exists-p "site.css") |
| 326 | + (setf css (uiop:read-file-string "site.css"))) |
| 327 | + (setf mustache:*default-pathname-type* "mustache") |
| 328 | + (setf 3bmd-code-blocks:*code-blocks* t) |
| 329 | + (setf posts (reverse (sort (gen-data) |
| 330 | + 'sort-by-ids |
| 331 | + :key 'car))) |
| 332 | + |
| 333 | + (multiple-value-bind (options free-args) |
| 334 | + (opts:get-opts) |
| 335 | + (when options |
| 336 | + (opts:describe))) |
| 337 | + |
| 338 | + (if (and css posts) |
| 339 | + (progn |
| 340 | + (copy-public) |
| 341 | + (gen-archive) |
| 342 | + (gen-index) |
| 343 | + (gen-pages) |
| 344 | + (gen-posts) |
| 345 | + (gen-rss) |
| 346 | + (gen-sitemap)) |
| 347 | + (print "No posts found. Create a md file in posts/. Also create a site.css file in the root."))))) |
0 commit comments