Skip to content

Commit

Permalink
feat: generate formatted dates (finally).
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Adam Simpson committed Aug 21, 2020
1 parent 0e97df1 commit 7afbb74
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 41 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,6 +1,7 @@
language: lisp
sudo: required

dist: focal
os:
- linux
- osx
Expand Down
2 changes: 1 addition & 1 deletion cycle.asd
Expand Up @@ -4,7 +4,7 @@
:description "A opinionated static site builder."
:author "Adam Simpson <adam@adamsimpson.net>"
:license "GNU GPLv3"
:version "0.2.6"
:version "0.2.7"
:serial t
:depends-on (
:local-time
Expand Down
55 changes: 15 additions & 40 deletions cycle.lisp
Expand Up @@ -40,7 +40,16 @@
"Create list that contains the data from each JSON file combined with the body of every post."
(let* ((json (uiop:read-file-string (car (cdr post))))
(list-json (json:decode-json-from-string json)))
`(,@list-json (:content . ,(post-for-slug (cdr (assoc :slug list-json)))))))
`(,@list-json
(:published_pretty . ,(pretty-date (cdr (assoc :published list-json))))
(:modified_pretty . ,(pretty-date (cdr (assoc :modified list-json))))
(:content . ,(post-for-slug (cdr (assoc :slug list-json)))))))

(defun pretty-date (date)
"Returns a pretty date given a date from a JSON file."
(local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:LONG-MONTH " " :DAY ", " :YEAR)))

(defun full-path-as-string (dir)
(namestring (truename dir)))
Expand All @@ -60,12 +69,6 @@
(ensure-directories-exist (concat (full-path-as-string "site/") folder)))
folder)))

(defun parse-date (date)
(let ((month (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:month)))
(year (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:year)))
(day (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:day))))
(concat year "-" month "-" day)))

(defun write-file (contents file)
"Write CONTENTS to FILE."
(with-open-file (stream file
Expand All @@ -92,8 +95,8 @@
(setf post `((:content . ,(cdr (assoc :content pair)))
(:pub_date . ,(cdr (assoc :published pair)))
(:mod_date . ,(cdr (assoc :modified pair)))
(:modifiedDate . ,(parse-date (cdr (assoc :modified pair))))
(:formattedDate . ,(parse-date (cdr (assoc :published pair))))
(:modifiedDate . ,(pretty-date (cdr (assoc :modified pair))))
(:formattedDate . ,(pretty-date (cdr (assoc :published pair))))
(:link . ,(cdr (assoc :link pair)))
(:description . ,(cdr (assoc :excerpt pair)))
(:slug . ,(concat "/writing/"
Expand Down Expand Up @@ -215,37 +218,9 @@
(date-as-rfc-822 (local-time:format-timestring nil (local-time:now))))

(defun date-as-rfc-822 (date)
(let ((year (local-time:format-timestring nil (local-time:parse-timestring date) :format '(:year)))
(month (local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:short-month)))
(day (local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:short-weekday)))
(date (return-leading-zero-as-string
(read-from-string
(local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:day)))))
(hour (return-leading-zero-as-string
(read-from-string
(local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:hour)))))
(minute (return-leading-zero-as-string
(read-from-string
(local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:min)))))
(seconds (return-leading-zero-as-string
(read-from-string
(local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:sec)))))
(tz (local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:gmt-offset-hhmm))))
(concat day ", " date " " month " " year " " hour ":" minute ":" seconds " " tz)))
(local-time:format-timestring nil
(local-time:parse-timestring date)
:format '(:short-weekday ", " :day " " :short-month " " :year " " :hour ":" :min ":" (:sec 2) " " :gmt-offset-hhmm)))

(defun format-data-for-rss(post)
(let ((slug (cdr (assoc :slug post))))
Expand Down

0 comments on commit 7afbb74

Please sign in to comment.