commit 2070db3bb4df28253637ee4fcdaac91c0be4c5f2
parent ba95673e7a8ed15fb7802fc66905db1820f8c528
Author: Decay <decay@todayiwilllaunchmyinfantsonintoorbit.com>
Date: Wed, 15 Jul 2020 23:13:26 -0700
Converting the unstructured post list into a property list
This makes the data passed to the CL-EMB templates more idiomatic
for what CL-EMB template style likes. I haven't really cleaned the
templates up yet (because there's other reasons we use a bunch of
directly-embedded lisp) but it's a start.
Diffstat:
5 files changed, 57 insertions(+), 53 deletions(-)
diff --git a/src/content.lisp b/src/content.lisp
@@ -65,7 +65,7 @@
(if (not entry)
(error "Entry cannot be nil")
(let* ((real-tz (or tz *default-timezone*))
- (entry-created-at (nth 1 entry))
+ (entry-created-at (getf entry :created-at))
(entry-year (timestamp-year entry-created-at :timezone real-tz))
(entry-month (timestamp-month entry-created-at :timezone real-tz)))
(cons entry-year entry-month))))
@@ -103,7 +103,7 @@
(labels
((get-year-month-pair-for-entry (entry)
(let* ((real-tz (or tz *default-timezone*))
- (created-at (nth 1 entry))
+ (created-at (getf entry :created-at))
(year (timestamp-year created-at :timezone real-tz))
(month (timestamp-month created-at :timezone real-tz)))
(cons year month)))
diff --git a/src/db.lisp b/src/db.lisp
@@ -18,14 +18,14 @@
(ensure-table-exists 'blog-entries))
(defun xform (entry)
- "Transform a blog-entries object into a simple field list"
- (list (mito.dao.mixin:object-id entry)
- (mito.dao.mixin:object-created-at entry)
- (mito.dao.mixin:object-updated-at entry)
- (entry-title entry)
- (entry-content entry)
- (entry-username entry)
- (entry-last-updated-by entry)))
+ "Transform a blog-entries object into an idiomatic property list"
+ (list :id (mito.dao.mixin:object-id entry)
+ :created-at (mito.dao.mixin:object-created-at entry)
+ :last-updated-at (mito.dao.mixin:object-updated-at entry)
+ :title (entry-title entry)
+ :content (entry-content entry)
+ :created-by (entry-username entry)
+ :last-updated-by (entry-last-updated-by entry)))
(defun add-entry (title content &key (username "nobody"))
"Add a new blog entry to the database"
diff --git a/src/miniblog.lisp b/src/miniblog.lisp
@@ -172,13 +172,13 @@
(defun get-post (id)
(miniblog.db:with-entry-id entry id
- (format t "ID: ~d~%" (nth 0 entry))
+ (format t "ID: ~d~%" (getf entry :id))
(format t "Created: ~A by ~A~%"
- (date-format (nth 1 entry)) (nth 5 entry))
+ (date-format (getf entry :created-at)) (getf entry :created-by))
(format t "Last updated: ~A by ~A~%"
- (date-format (nth 2 entry)) (nth 6 entry))
- (format t "Title: ~A~%" (nth 3 entry))
- (format t "Content:~%~A~%" (nth 4 entry))))
+ (date-format (getf entry :last-updated-at)) (getf entry :last-updated-by))
+ (format t "Title: ~A~%" (getf entry :title))
+ (format t "Content:~%~A~%" (getf entry :content))))
(defun make-template (title content)
(with-output-to-string (out)
@@ -190,7 +190,7 @@
(defun edit-post (id regen)
(miniblog.db:with-entry-id entry id
(let ((text (miniblog.edit:edit-text
- :template (make-template (nth 3 entry) (nth 4 entry)))))
+ :template (make-template (getf entry :title) (getf entry :content)))))
(if text
(let* ((post (miniblog.edit:get-title-and-content text))
(title (nth 0 post))
@@ -198,7 +198,7 @@
(miniblog.db:update-entry
id title content
:username (get-username))
- (let* ((created-at (nth 1 entry))
+ (let* ((created-at (getf entry :created-at))
(year (timestamp-year created-at
:timezone *blog-timezone*))
(month (timestamp-month created-at
@@ -217,7 +217,7 @@
(miniblog.db:with-entry-id entry id
(format t "Deleting post ID ~d...~%" id)
(miniblog.db:delete-entry id)
- (let* ((created-at (nth 1 entry))
+ (let* ((created-at (getf entry :created-at))
(year (timestamp-year created-at
:timezone *blog-timezone*))
(month (timestamp-month created-at
@@ -234,7 +234,7 @@
(+ first n)
(length entries))))
(dolist (entry (subseq entries first last))
- (format t "~d \"~A\" ~A~%" (first entry) (nth 3 entry) (nth 5 entry)))))
+ (format t "~d \"~A\" ~A~%" (getf entry :id) (getf entry :title) (getf entry :created-by)))))
(defun init-tz ()
(reread-timezone-repository))
diff --git a/src/rss.lxml b/src/rss.lxml
@@ -31,15 +31,17 @@
<category><% @var category %></category>
<% @endif %>
<% @if posts %>
- <% (loop for (id created-at updated-at title content username last-updated-by) in (getf env :posts) do %>
- <item>
- <% (let* ((short-date (funcall (getf env :rfc-822-date-formatter) updated-at)) (formatted-content (funcall (getf env :content-formatter) content)) (stripped-content (funcall (getf env :content-stripper) formatted-content)) (truncated-content (str:substring 0 200 stripped-content))) %>
- <title><%= title %></title>
- <link><% @var link %>#<%= id %></link>
- <description><%= (if (equal stripped-content truncated-content) stripped-content (concatenate 'string truncated-content "...")) %></description>
- <pubDate><%= short-date %></pubDate>
- <% ) %>
- </item>
+ <% (loop for post in (getf env :posts) do %>
+ <% (destructuring-bind (&key id title content last-updated-at &allow-other-keys) post %>
+ <item>
+ <% (let* ((short-date (funcall (getf env :rfc-822-date-formatter) last-updated-at)) (formatted-content (funcall (getf env :content-formatter) content)) (stripped-content (funcall (getf env :content-stripper) formatted-content)) (truncated-content (str:substring 0 200 stripped-content))) %>
+ <title><%= title %></title>
+ <link><% @var link %>#<%= id %></link>
+ <description><%= (if (equal stripped-content truncated-content) stripped-content (concatenate 'string truncated-content "...")) %></description>
+ <pubDate><%= short-date %></pubDate>
+ <% ) %>
+ </item>
+ <% ) %>
<% ) %>
<% @endif %>
</channel>
diff --git a/src/template.lhtml b/src/template.lhtml
@@ -46,31 +46,33 @@
<section id="miniblog-main">
<% @if posts %>
<% (let ((short-date) (render-hr nil)) %>
- <% (loop for (id created-at updated-at title content username last-updated-by) in (getf env :posts) do %>
- <% (let ((curr-short-date (funcall (getf env :short-date-formatter) created-at))) %>
- <% (if render-hr %>
- <hr>
- <% ) %>
- <% (setf render-hr t) %>
- <% (if (string/= short-date curr-short-date)
- (progn
- (setf short-date curr-short-date)
- (format t "<h1>~A</h1>~%" short-date)))) %>
- <a name="<%= id %>"></a>
- <h2><%= title %></h2>
- <article>
- <%= (funcall (getf env :content-formatter) content) %>
- </article>
- <p>
- <small>
- Posted by <%= username %> on
- <%= (funcall (getf env :long-date-formatter) created-at) %>
- <% (if (local-time:timestamp/= created-at updated-at)
- (format t "<br>~%Last updated by ~A on ~A~%"
- last-updated-by
- (funcall (getf env :long-date-formatter) updated-at))) %>
- </small>
- </p>
+ <% (loop for post in (getf env :posts) do %>
+ <% (destructuring-bind (&key id created-at last-updated-at title content created-by last-updated-by) post %>
+ <% (let ((curr-short-date (funcall (getf env :short-date-formatter) created-at))) %>
+ <% (if render-hr %>
+ <hr>
+ <% ) %>
+ <% (setf render-hr t) %>
+ <% (if (string/= short-date curr-short-date)
+ (progn
+ (setf short-date curr-short-date)
+ (format t "<h1>~A</h1>~%" short-date)))) %>
+ <a name="<%= id %>"></a>
+ <h2><%= title %></h2>
+ <article>
+ <%= (funcall (getf env :content-formatter) content) %>
+ </article>
+ <p>
+ <small>
+ Posted by <%= created-by %> on
+ <%= (funcall (getf env :long-date-formatter) created-at) %>
+ <% (if (local-time:timestamp/= created-at last-updated-at)
+ (format t "<br>~%Last updated by ~A on ~A~%"
+ last-updated-by
+ (funcall (getf env :long-date-formatter) last-updated-at))) %>
+ </small>
+ </p>
+ <% ) %>
<% ) %>
<% ) %>
<% @else %>