Cleanups to RENDER-BY-FOO functions based on @bigthingist's codereview.

This commit is contained in:
Brit Butler 2012-08-27 15:02:10 -04:00
parent 8f4f2b0af9
commit 7d9f26a15c

View file

@ -10,14 +10,14 @@
(reduce (lambda (x y) (union x y :test #'string=)) (reduce (lambda (x y) (union x y :test #'string=))
(mapcar #'post-tags (hash-table-values *posts*)))) (mapcar #'post-tags (hash-table-values *posts*))))
(defun taglinks (&optional tags) (defun taglinks (&optional (tags (all-tags)))
"Generate links to all the tag indices or those in TAGS." "Generate links to all the tag indices or those in TAGS."
(loop for tag in (or tags (sort (all-tags) #'string<)) (loop for tag in (sort tags #'string<)
collect (list :url (format nil "tag/~a.html" tag) :name tag))) collect (list :url (format nil "tag/~a.html" tag) :name tag)))
(defun monthlinks () (defun monthlinks (&optional (months (all-months)))
"Generate links to all the month indices." "Generate links to all the month indices."
(loop for month in (sort (all-months) #'string<) (loop for month in (sort months #'string<)
collect (list :url (format nil "date/~a.html" month) :name month))) collect (list :url (format nil "date/~a.html" month) :name month)))
(defun get-month (timestamp) (defun get-month (timestamp)
@ -49,42 +49,40 @@
:prev (and prev (format nil "~d.html" prev)) :prev (and prev (format nil "~d.html" prev))
:next (and next (format nil "~d.html" next))))))) :next (and next (format nil "~d.html" next)))))))
(defun render-by-20 () (defun render-by-n (&optional n)
"Render the indices to view posts in reverse chronological order by 20." "Render the indices to view posts in reverse chronological order by N."
(flet ((by-20 (posts start) (flet ((by-n (posts start)
(let ((index (* 20 (1- start)))) (let ((index (* n (1- start))))
(subseq posts index (min (length posts) (+ index 20)))))) (subseq posts index (min (length posts) (+ index n))))))
(let ((posts (by-date (hash-table-values *posts*)))) (let ((posts (by-date (hash-table-values *posts*))))
(loop for i = 1 then (1+ i) (loop for i = 1 then (1+ i)
do (write-index (by-20 posts i) (format nil "~d.html" i) "Recent Posts" do (write-index (by-n posts i) (format nil "~d.html" i) "Recent Posts"
:prev (and (plusp (1- i)) (1- i)) :prev (and (plusp (1- i)) (1- i))
:next (and (< (* i 20) (length posts)) (1+ i)) :next (and (< (* i n) (length posts)) (1+ i))
:relative nil) :relative nil)
until (> (* i 20) (length posts))) until (> (* i n) (length posts)))
(update-symlink "index.html" "1.html")))) (update-symlink "index.html" "1.html"))))
(defun render-by-tag () (defun render-by-tag (tags)
"Render the indices to view posts by tag." "Render the indices to view posts by tag for each tag in TAGS."
(loop for tag in (all-tags) (dolist (tag tags)
do (flet ((match-tag (post) (let ((posts (remove-if-not (lambda (post) (member tag (post-tags post)) :test #'string=)
(member tag (post-tags post) :test #'string=))) (hash-table-values *posts*))))
(let ((posts (remove-if-not #'match-tag (hash-table-values *posts*)))) (write-index (by-date posts)
(write-index (by-date posts) (format nil "tag/~a.html" tag)
(format nil "tag/~a.html" tag) (format nil "Posts tagged ~a" tag)))))
(format nil "Posts tagged ~a" tag))))))
(defun render-by-month () (defun render-by-month (months)
"Render the indices to view posts by month." "Render the indices to view posts by month for each month in MONTHS."
(loop for month in (all-months) (dolist (month months)
do (flet ((match-month (post) (let ((posts (remove-if-not (lambda (post) (search month (post-date post)))
(search month (post-date post)))) (hash-table-values *posts*))))
(let ((posts (remove-if-not #'match-month (hash-table-values *posts*)))) (write-index (by-date posts)
(write-index (by-date posts) (format nil "date/~a.html" month)
(format nil "date/~a.html" month) (format nil "Posts from ~a" month)))))
(format nil "Posts from ~a" month))))))
(defun render-indices () (defun render-indices ()
"Render the indices to view posts in groups of 20, by month, and by tag." "Render the indices to view posts in groups of size N, by month, and by tag."
(render-by-20) (render-by-n 10)
(render-by-tag) (render-by-tag (all-tags))
(render-by-month)) (render-by-month (all-months)))