Skip to content

Commit c92d6bf

Browse files
author
Adam Simpson
committed
feat: add generate command which generates an empty post and date.
cycle generate TITLE will generate two files: - posts/TITLE.json - posts/TITLE.md The json will be filled with the current date and the TITLE will become the slug as well.
1 parent 09d20a5 commit c92d6bf

File tree

2 files changed

+73
-21
lines changed

2 files changed

+73
-21
lines changed

cycle.asd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
:cl-json
1212
:cl-mustache
1313
:3bmd
14-
:3bmd-ext-code-blocks)
14+
:3bmd-ext-code-blocks
15+
:unix-opts)
1516
:components ((:file "package")
1617
(:file "cycle"))
1718
:build-pathname "cycle"

cycle.lisp

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@
77
(defvar css nil
88
"CSS for the site.")
99

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+
1016
(defun concat (&rest strings)
1117
"Wrapper around the more cumbersome concatenate form."
1218
(let (result)
1319
(dolist (x strings)
1420
(setf result (concatenate 'string result x)))
1521
result))
1622

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+
1729
(defun gen-data ()
1830
"Read markdown posts from 'posts' dir and retrieve data from each matching json file."
1931
(let* ((posts (uiop:directory-files "posts/" "*.md"))
@@ -272,25 +284,64 @@
272284
"site/sitemap.xml"))
273285
(print "No sitemap.mustache template found. Please create one in templates/.")))
274286

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+
275316
(defun main ()
276317
"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

Comments
 (0)