From 0e6edb72112d2d425f6b48f20ab564cdcd0c98a4 Mon Sep 17 00:00:00 2001 From: Brit Butler Date: Sun, 21 Apr 2013 14:11:24 -0400 Subject: [PATCH] Cleanups to feeds. --- src/content.lisp | 4 ++++ src/feeds.lisp | 45 ++++++++++++++++++++++++++++----------------- src/indices.lisp | 5 +---- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/content.lisp b/src/content.lisp index d5ce393..4b9a1bb 100644 --- a/src/content.lisp +++ b/src/content.lisp @@ -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) diff --git a/src/feeds.lisp b/src/feeds.lisp index b9ac1e8..3d289dc 100644 --- a/src/feeds.lisp +++ b/src/feeds.lisp @@ -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))))) diff --git a/src/indices.lisp b/src/indices.lisp index fd16223..82224e9 100644 --- a/src/indices.lisp +++ b/src/indices.lisp @@ -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)))))