Cleanups to feeds.

This commit is contained in:
Brit Butler 2013-04-21 14:11:24 -04:00
parent 96be3254ab
commit 0e6edb7211
3 changed files with 33 additions and 21 deletions

View file

@ -11,6 +11,10 @@
"Takes a string and returns a TAG instance with a name and slug."
(make-instance 'tag :name (string-trim " " str) :slug (slugify str)))
(defun tag-slug= (a b)
"Test if the slugs for tag A and B are equal."
(string= (tag-slug a) (tag-slug b)))
(defclass content ()
((tags :initform nil :initarg :tags :accessor content-tags)
(slug :initform nil :initarg :slug :accessor content-slug)

View file

@ -4,20 +4,31 @@
"Make a RFC1123 pubdate representing the current time."
(local-time:format-rfc1123-timestring nil (local-time:now)))
(defun render-feeds (feeds)
"Render and write the given FEEDS for the site."
(flet ((first-10 (list)
(subseq list 0 (min (length list) 10))))
(let* ((by-date (by-date (find-all 'post)))
(posts (first-10 by-date))
(rss (make-instance 'index :id "rss.xml" :posts posts))
(atom (make-instance 'index :id "feed.atom" :posts posts))
(rss-template (theme-fn :rss-feed "feeds"))
(atom-template (theme-fn :atom-feed "feeds")))
(write-page (page-path rss) (render-page rss rss-template))
(write-page (page-path atom) (render-page atom atom-template))
(dolist (feed feeds)
(let ((index (index-by-tag (make-tag feed) by-date)))
(setf (index-id index) (format nil "~a-rss.xml" feed)
(index-posts index) (first-10 (index-posts index)))
(write-page (page-path index) (render-page index rss-template)))))))
(defun first-10 (list)
"Get up to the first 10 items in LIST."
(subseq list 0 (min (length list) 10)))
(defun make-tag-feed (tag posts)
"Make an RSS feed for the given TAG and POSTS."
(flet ((valid-p (obj) (member tag (content-tags obj) :test #'tag-slug=)))
(make-instance 'tag-index :id (format nil "~A-rss.xml" (tag-slug tag))
:posts (first-10 (remove-if-not #'valid-p posts)))))
(defun render-feed (posts &key path template tag)
"Given a PATH, TEMPLATE, and possibly a TAG, render the appropriate feed."
(let ((template (theme-fn template "feeds"))
(index (if tag
(make-tag-feed tag posts)
(make-instance 'index :id path
:posts (first-10 posts)))))
(write-page (page-path index) (render-page index template))))
(defun render-feeds (tag-feeds)
"Render the default RSS and ATOM feeds along with any TAG-FEEDS."
(let ((posts (by-date (find-all 'post))))
(dolist (feed '((:path "rss.xml" :template :rss-feed)
(:path "feed.atom" :template :atom-feed)))
(apply #'render-feed posts feed))
(dolist (feed tag-feeds)
(apply #'render-feed posts (list :tag (make-tag feed)
:template :rss-feed)))))

View file

@ -44,10 +44,7 @@
(defun index-by-tag (tag content)
"Return an index of all CONTENT matching the given TAG."
(labels ((tag-slug= (a b)
(string= (tag-slug a) (tag-slug b)))
(valid-p (obj)
(member tag (content-tags obj) :test #'tag-slug=)))
(flet ((valid-p (obj) (member tag (content-tags obj) :test #'tag-slug=)))
(make-instance 'tag-index :id (tag-slug tag)
:posts (remove-if-not #'valid-p content)
:title (format nil "Posts tagged ~a" (tag-name tag)))))