From 7d9f26a15c055f9d56204b7ee459e62725bc2476 Mon Sep 17 00:00:00 2001 From: Brit Butler Date: Mon, 27 Aug 2012 15:02:10 -0400 Subject: [PATCH] Cleanups to RENDER-BY-FOO functions based on @bigthingist's codereview. --- src/indices.lisp | 66 +++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/src/indices.lisp b/src/indices.lisp index 3a325eb..eb2954a 100644 --- a/src/indices.lisp +++ b/src/indices.lisp @@ -10,14 +10,14 @@ (reduce (lambda (x y) (union x y :test #'string=)) (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." - (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))) -(defun monthlinks () +(defun monthlinks (&optional (months (all-months))) "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))) (defun get-month (timestamp) @@ -49,42 +49,40 @@ :prev (and prev (format nil "~d.html" prev)) :next (and next (format nil "~d.html" next))))))) -(defun render-by-20 () - "Render the indices to view posts in reverse chronological order by 20." - (flet ((by-20 (posts start) - (let ((index (* 20 (1- start)))) - (subseq posts index (min (length posts) (+ index 20)))))) +(defun render-by-n (&optional n) + "Render the indices to view posts in reverse chronological order by N." + (flet ((by-n (posts start) + (let ((index (* n (1- start)))) + (subseq posts index (min (length posts) (+ index n)))))) (let ((posts (by-date (hash-table-values *posts*)))) (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)) - :next (and (< (* i 20) (length posts)) (1+ i)) + :next (and (< (* i n) (length posts)) (1+ i)) :relative nil) - until (> (* i 20) (length posts))) + until (> (* i n) (length posts))) (update-symlink "index.html" "1.html")))) -(defun render-by-tag () - "Render the indices to view posts by tag." - (loop for tag in (all-tags) - do (flet ((match-tag (post) - (member tag (post-tags post) :test #'string=))) - (let ((posts (remove-if-not #'match-tag (hash-table-values *posts*)))) - (write-index (by-date posts) - (format nil "tag/~a.html" tag) - (format nil "Posts tagged ~a" tag)))))) +(defun render-by-tag (tags) + "Render the indices to view posts by tag for each tag in TAGS." + (dolist (tag tags) + (let ((posts (remove-if-not (lambda (post) (member tag (post-tags post)) :test #'string=) + (hash-table-values *posts*)))) + (write-index (by-date posts) + (format nil "tag/~a.html" tag) + (format nil "Posts tagged ~a" tag))))) -(defun render-by-month () - "Render the indices to view posts by month." - (loop for month in (all-months) - do (flet ((match-month (post) - (search month (post-date post)))) - (let ((posts (remove-if-not #'match-month (hash-table-values *posts*)))) - (write-index (by-date posts) - (format nil "date/~a.html" month) - (format nil "Posts from ~a" month)))))) +(defun render-by-month (months) + "Render the indices to view posts by month for each month in MONTHS." + (dolist (month months) + (let ((posts (remove-if-not (lambda (post) (search month (post-date post))) + (hash-table-values *posts*)))) + (write-index (by-date posts) + (format nil "date/~a.html" month) + (format nil "Posts from ~a" month))))) (defun render-indices () - "Render the indices to view posts in groups of 20, by month, and by tag." - (render-by-20) - (render-by-tag) - (render-by-month)) + "Render the indices to view posts in groups of size N, by month, and by tag." + (render-by-n 10) + (render-by-tag (all-tags)) + (render-by-month (all-months)))