Refactor INDEX rendering...again.

This commit is contained in:
Brit Butler 2012-09-12 13:37:55 -04:00
parent e57f350cb4
commit c439f1a448
3 changed files with 31 additions and 33 deletions

View file

@ -57,6 +57,7 @@ Additional args to render CONTENT can be passed via RENDER-ARGS."
(defun main () (defun main ()
"Load the user's config, then compile and deploy the blog." "Load the user's config, then compile and deploy the blog."
(load-config) (load-config)
(load-posts)
(compile-theme (theme *config*)) (compile-theme (theme *config*))
(compile-blog (staging *config*)) (compile-blog (staging *config*))
(deploy (staging *config*))) (deploy (staging *config*)))

View file

@ -35,41 +35,39 @@
"Sort POSTS in reverse chronological order." "Sort POSTS in reverse chronological order."
(sort posts #'string> :key #'post-date)) (sort posts #'string> :key #'post-date))
(defun render-by-n (posts &optional n) (defun index-by-tag (tag posts)
"Render the indices to view POSTS in reverse chronological order by N." "Return an index of all POSTS matching the given TAG."
(flet ((by-n (posts start) (let ((content (remove-if-not (lambda (post) (member tag (post-tags post)
(let ((index (* n (1- start))))
(subseq posts index (min (length posts) (+ index n))))))
(loop for i = 1 then (1+ i)
do (render-page (make-instance 'index :path (format nil "~d.html" i)
:posts (by-n posts i)
:title "Recent Posts")
nil
:prev (and (plusp (1- i)) (1- i))
:next (and (< (* i n) (length posts)) (1+ i)))
until (> (* i n) (length posts))))
(update-symlink "index.html" "1.html"))
(defun render-by-tag (posts 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=)) posts))) :test #'string=)) posts)))
(render-page (make-instance 'index :path (format nil "tag/~a.html" tag) (make-instance 'index :path (format nil "tag/~a.html" tag)
:posts (by-date posts) :posts (by-date content)
:title (format nil "Posts tagged ~a" tag)))))) :title "Posts tagged ~a" tag)))
(defun render-by-month (posts months) (defun index-by-month (month posts)
"Render the indices to view POSTS by month for each month in MONTHS." "Return an index of all POSTS matching the given MONTH."
(dolist (month months) (let ((content (remove-if-not (lambda (post) (search month (post-date post)))
(let ((posts (remove-if-not (lambda (post) (search month (post-date post))) posts))) posts)))
(render-page (make-instance 'index :path (format nil "date/~a.html" month) (make-instance 'index :path (format nil "date/~a.html" month)
:posts (by-date posts) :posts (by-date content)
:title (format nil "Posts from ~a" month)))))) :title (format nil "Posts from ~a" month))))
(defun index-by-n (i posts &optional (step 10))
"Return the index for the Ith page of POSTS in reverse chronological order."
(make-instance 'index :path (format nil "~d.html" i)
:posts (let ((index (* step (1- i))))
(subseq posts index (min (length posts)
(+ index step))))
:title "Recent Posts"))
(defun render-indices () (defun render-indices ()
"Render the indices to view posts in groups of size N, by month, and by tag." "Render the indices to view posts in groups of size N, by month, and by tag."
(let ((posts (hash-table-values *posts*))) (let ((posts (hash-table-values *posts*)))
(render-by-n (by-date posts) 10) (dolist (tag (all-tags))
(render-by-tag posts (all-tags)) (render-page (index-by-tag tag posts)))
(render-by-month posts (all-months)))) (dolist (month (all-months))
(render-page (index-by-month month posts)))
(dolist (i (ceiling (length posts) 10))
(render-page (index-by-n i (by-date posts)) nil
:prev (and (plusp (1- i)) (1- i))
:next (and (< (* i 10) (length posts)) (1+ i)))))
(update-symlink "index.html" "1.html"))

View file

@ -31,7 +31,6 @@
(defun render-posts () (defun render-posts ()
"Iterate through the files in the repo to render+write the posts out to disk." "Iterate through the files in the repo to render+write the posts out to disk."
(load-posts)
(loop with posts = (sort (hash-table-values *posts*) #'string< :key #'post-date) (loop with posts = (sort (hash-table-values *posts*) #'string< :key #'post-date)
for i from 1 upto (length posts) for i from 1 upto (length posts)
for prev = nil then post for prev = nil then post