2011-04-16 15:45:37 -04:00
|
|
|
(in-package :coleslaw)
|
|
|
|
|
2014-05-16 11:07:36 -04:00
|
|
|
(defvar *all-months* nil
|
|
|
|
"The list of months in which content was authored.")
|
|
|
|
(defvar *all-tags* nil
|
|
|
|
"The list of tags which content has been tagged with.")
|
|
|
|
|
2012-09-12 11:00:21 -04:00
|
|
|
(defclass index ()
|
2014-08-27 11:51:12 -04:00
|
|
|
((url :initarg :url :reader page-url)
|
2014-08-27 14:08:48 -04:00
|
|
|
(name :initarg :name :reader index-name)
|
2014-08-27 11:51:12 -04:00
|
|
|
(title :initarg :title :reader title-of)
|
2014-05-07 22:31:06 -04:00
|
|
|
(content :initarg :content :reader index-content)))
|
2012-09-12 11:00:21 -04:00
|
|
|
|
2014-08-27 11:51:12 -04:00
|
|
|
(defmethod initialize-instance :after ((object index) &key slug)
|
|
|
|
(with-slots (url) object
|
|
|
|
(setf url (compute-url object slug))))
|
2014-08-22 17:29:47 -04:00
|
|
|
|
2013-04-29 10:17:19 -04:00
|
|
|
(defmethod render ((object index) &key prev next)
|
2014-08-22 17:29:47 -04:00
|
|
|
(funcall (theme-fn 'index) (list :tags (find-all 'tag-index)
|
|
|
|
:months (find-all 'month-index)
|
2013-04-29 10:17:19 -04:00
|
|
|
:config *config*
|
|
|
|
:index object
|
|
|
|
:prev prev
|
|
|
|
:next next)))
|
|
|
|
|
2014-04-15 15:27:46 -04:00
|
|
|
;;; Index by Tag
|
2012-08-20 17:26:12 -04:00
|
|
|
|
2014-04-15 15:27:46 -04:00
|
|
|
(defclass tag-index (index) ())
|
|
|
|
|
|
|
|
(defmethod discover ((doc-type (eql (find-class 'tag-index))))
|
|
|
|
(let ((content (by-date (find-all 'post))))
|
2014-08-22 17:29:47 -04:00
|
|
|
(dolist (tag *all-tags*)
|
2014-04-15 15:27:46 -04:00
|
|
|
(add-document (index-by-tag tag content)))))
|
2011-04-22 17:16:42 -04:00
|
|
|
|
2013-01-02 14:23:56 -05:00
|
|
|
(defun index-by-tag (tag content)
|
|
|
|
"Return an index of all CONTENT matching the given TAG."
|
2014-08-27 14:08:48 -04:00
|
|
|
(make-instance 'tag-index :slug (tag-slug tag) :name (tag-name tag)
|
2014-04-15 20:30:47 -04:00
|
|
|
:content (remove-if-not (lambda (x) (tag-p tag x)) content)
|
2014-04-15 20:39:13 -04:00
|
|
|
:title (format nil "Content tagged ~a" (tag-name tag))))
|
2011-04-16 15:45:37 -04:00
|
|
|
|
2014-04-15 15:27:46 -04:00
|
|
|
(defmethod publish ((doc-type (eql (find-class 'tag-index))))
|
|
|
|
(dolist (index (find-all 'tag-index))
|
2014-04-28 13:59:50 -04:00
|
|
|
(write-document index)))
|
2014-04-15 15:27:46 -04:00
|
|
|
|
|
|
|
;;; Index by Month
|
|
|
|
|
|
|
|
(defclass month-index (index) ())
|
|
|
|
|
|
|
|
(defmethod discover ((doc-type (eql (find-class 'month-index))))
|
|
|
|
(let ((content (by-date (find-all 'post))))
|
2014-05-16 11:07:36 -04:00
|
|
|
(dolist (month *all-months*)
|
2014-04-15 15:27:46 -04:00
|
|
|
(add-document (index-by-month month content)))))
|
|
|
|
|
2013-01-02 14:23:56 -05:00
|
|
|
(defun index-by-month (month content)
|
|
|
|
"Return an index of all CONTENT matching the given MONTH."
|
2014-08-27 14:08:48 -04:00
|
|
|
(make-instance 'month-index :slug month :name month
|
2014-04-15 20:30:47 -04:00
|
|
|
:content (remove-if-not (lambda (x) (month-p month x)) content)
|
2014-04-15 20:39:13 -04:00
|
|
|
:title (format nil "Content from ~a" month)))
|
2012-09-12 13:37:55 -04:00
|
|
|
|
2014-04-15 15:27:46 -04:00
|
|
|
(defmethod publish ((doc-type (eql (find-class 'month-index))))
|
|
|
|
(dolist (index (find-all 'month-index))
|
2014-04-28 13:59:50 -04:00
|
|
|
(write-document index)))
|
2014-04-15 15:27:46 -04:00
|
|
|
|
|
|
|
;;; Reverse Chronological Index
|
|
|
|
|
|
|
|
(defclass numeric-index (index) ())
|
|
|
|
|
|
|
|
(defmethod discover ((doc-type (eql (find-class 'numeric-index))))
|
|
|
|
(let ((content (by-date (find-all 'post))))
|
|
|
|
(dotimes (i (ceiling (length content) 10))
|
|
|
|
(add-document (index-by-n i content)))))
|
|
|
|
|
2014-04-08 16:51:13 -04:00
|
|
|
(defun index-by-n (i content)
|
2013-01-02 14:23:56 -05:00
|
|
|
"Return the index for the Ith page of CONTENT in reverse chronological order."
|
2014-04-08 16:51:13 -04:00
|
|
|
(let ((content (subseq content (* 10 i))))
|
2014-08-27 14:08:48 -04:00
|
|
|
(make-instance 'numeric-index :slug (1+ i) :name (1+ i)
|
2014-04-15 20:30:47 -04:00
|
|
|
:content (take-up-to 10 content)
|
2014-04-15 20:39:13 -04:00
|
|
|
:title "Recent Content")))
|
2014-04-08 16:51:13 -04:00
|
|
|
|
2014-04-15 15:27:46 -04:00
|
|
|
(defmethod publish ((doc-type (eql (find-class 'numeric-index))))
|
2014-08-27 14:08:48 -04:00
|
|
|
(let ((indexes (sort (find-all 'numeric-index) #'< :key #'index-name)))
|
2014-08-27 15:45:29 -04:00
|
|
|
(loop for (next index prev) on (append '(nil) indexes)
|
|
|
|
while index do (write-document index nil :prev prev :next next))))
|
2014-04-15 15:27:46 -04:00
|
|
|
|
|
|
|
;;; Helper Functions
|
|
|
|
|
2014-05-19 12:01:46 -04:00
|
|
|
(defun update-content-metadata ()
|
|
|
|
"Set *ALL-TAGS* and *ALL-MONTHS* to the union of all tags and months
|
|
|
|
of content loaded in the DB."
|
|
|
|
(setf *all-tags* (all-tags))
|
|
|
|
(setf *all-months* (all-months)))
|
|
|
|
|
2014-04-15 15:27:46 -04:00
|
|
|
(defun all-months ()
|
|
|
|
"Retrieve a list of all months with published content."
|
2016-10-25 17:44:41 -05:00
|
|
|
(let ((months (loop :for post :in (find-all 'post)
|
|
|
|
:for content-date := (content-date post)
|
|
|
|
:when content-date
|
2017-03-24 00:10:33 +01:00
|
|
|
:collect (subseq content-date 0
|
|
|
|
(min 7 (length content-date))))))
|
2014-04-15 15:27:46 -04:00
|
|
|
(sort (remove-duplicates months :test #'string=) #'string>)))
|
|
|
|
|
|
|
|
(defun all-tags ()
|
|
|
|
"Retrieve a list of all tags used in content."
|
2014-04-15 16:46:04 -04:00
|
|
|
(let* ((dupes (mappend #'content-tags (find-all 'post)))
|
2014-05-01 17:28:51 -04:00
|
|
|
(tags (remove-duplicates dupes :test #'tag-slug=)))
|
2014-04-15 15:27:46 -04:00
|
|
|
(sort tags #'string< :key #'tag-name)))
|