Skip to content

Commit 7afbb74

Browse files
author
Adam Simpson
committed
feat: generate formatted dates (finally).
Turns out reading the source for local-time was a very productive exercise I should have done ages ago. I was able to accomplish two things: 1. Properly format the dates beneath titles 2. Remove the ugly rfc-822 date concat and use the proper `local-time:format-string` to handle it. I'll also add this here: the expression `,@` "splices" a list in place as described here[1]. [1]: http://www.lispworks.com/documentation/HyperSpec/Body/02_df.htm
1 parent 0e97df1 commit 7afbb74

File tree

3 files changed

+17
-41
lines changed

3 files changed

+17
-41
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: lisp
22
sudo: required
33

4+
dist: focal
45
os:
56
- linux
67
- osx

cycle.asd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
:description "A opinionated static site builder."
55
:author "Adam Simpson <adam@adamsimpson.net>"
66
:license "GNU GPLv3"
7-
:version "0.2.6"
7+
:version "0.2.7"
88
:serial t
99
:depends-on (
1010
:local-time

cycle.lisp

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@
4040
"Create list that contains the data from each JSON file combined with the body of every post."
4141
(let* ((json (uiop:read-file-string (car (cdr post))))
4242
(list-json (json:decode-json-from-string json)))
43-
`(,@list-json (:content . ,(post-for-slug (cdr (assoc :slug list-json)))))))
43+
`(,@list-json
44+
(:published_pretty . ,(pretty-date (cdr (assoc :published list-json))))
45+
(:modified_pretty . ,(pretty-date (cdr (assoc :modified list-json))))
46+
(:content . ,(post-for-slug (cdr (assoc :slug list-json)))))))
47+
48+
(defun pretty-date (date)
49+
"Returns a pretty date given a date from a JSON file."
50+
(local-time:format-timestring nil
51+
(local-time:parse-timestring date)
52+
:format '(:LONG-MONTH " " :DAY ", " :YEAR)))
4453

4554
(defun full-path-as-string (dir)
4655
(namestring (truename dir)))
@@ -60,12 +69,6 @@
6069
(ensure-directories-exist (concat (full-path-as-string "site/") folder)))
6170
folder)))
6271

63-
(defun parse-date (date)
64-
(let ((month (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:month)))
65-
(year (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:year)))
66-
(day (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:day))))
67-
(concat year "-" month "-" day)))
68-
6972
(defun write-file (contents file)
7073
"Write CONTENTS to FILE."
7174
(with-open-file (stream file
@@ -92,8 +95,8 @@
9295
(setf post `((:content . ,(cdr (assoc :content pair)))
9396
(:pub_date . ,(cdr (assoc :published pair)))
9497
(:mod_date . ,(cdr (assoc :modified pair)))
95-
(:modifiedDate . ,(parse-date (cdr (assoc :modified pair))))
96-
(:formattedDate . ,(parse-date (cdr (assoc :published pair))))
98+
(:modifiedDate . ,(pretty-date (cdr (assoc :modified pair))))
99+
(:formattedDate . ,(pretty-date (cdr (assoc :published pair))))
97100
(:link . ,(cdr (assoc :link pair)))
98101
(:description . ,(cdr (assoc :excerpt pair)))
99102
(:slug . ,(concat "/writing/"
@@ -215,37 +218,9 @@
215218
(date-as-rfc-822 (local-time:format-timestring nil (local-time:now))))
216219

217220
(defun date-as-rfc-822 (date)
218-
(let ((year (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:year)))
219-
(month (local-time:format-timestring nil
220-
(local-time:parse-timestring date)
221-
:format '(:short-month)))
222-
(day (local-time:format-timestring nil
223-
(local-time:parse-timestring date)
224-
:format '(:short-weekday)))
225-
(date (return-leading-zero-as-string
226-
(read-from-string
227-
(local-time:format-timestring nil
228-
(local-time:parse-timestring date)
229-
:format '(:day)))))
230-
(hour (return-leading-zero-as-string
231-
(read-from-string
232-
(local-time:format-timestring nil
233-
(local-time:parse-timestring date)
234-
:format '(:hour)))))
235-
(minute (return-leading-zero-as-string
236-
(read-from-string
237-
(local-time:format-timestring nil
238-
(local-time:parse-timestring date)
239-
:format '(:min)))))
240-
(seconds (return-leading-zero-as-string
241-
(read-from-string
242-
(local-time:format-timestring nil
243-
(local-time:parse-timestring date)
244-
:format '(:sec)))))
245-
(tz (local-time:format-timestring nil
246-
(local-time:parse-timestring date)
247-
:format '(:gmt-offset-hhmm))))
248-
(concat day ", " date " " month " " year " " hour ":" minute ":" seconds " " tz)))
221+
(local-time:format-timestring nil
222+
(local-time:parse-timestring date)
223+
:format '(:short-weekday ", " :day " " :short-month " " :year " " :hour ":" :min ":" (:sec 2) " " :gmt-offset-hhmm)))
249224

250225
(defun format-data-for-rss(post)
251226
(let ((slug (cdr (assoc :slug post))))

0 commit comments

Comments
 (0)