(in-package :coleslaw) (defmethod make-index (id posts) (make-instance 'index :id id :posts posts)) (defmethod find-index (id) (gethash id (gethash :indices *storage*))) (defun (setf find-index) (new-val id) (setf (gethash id (gethash :indices *storage*)) new-val) new-val) (defmethod add-to-index (id (post post)) (let ((index (find-index id))) (if index (push post (index-posts index)) (setf index (make-index id (list post)))) (setf (find-index id) index))) (defmethod remove-from-index (id (post post)) (let ((index (find-index id))) (setf (index-posts index) (remove post (index-posts index))) (if (index-posts index) (setf (find-index id) index) (remhash id (gethash :indices *storage*))))) (defun monthlinks () (loop for month in (gethash :months-list *storage*) collecting (list :url (index-url (concatenate 'string "date/" month) 1) :name month))) (defun taglinks () (loop for tag in (gethash :tags-list *storage*) collecting (list :url (index-url (concatenate 'string "tag/" tag) 1) :name tag))) (defun index-title (id) (let ((split-id (split-sequence:split-sequence #\/ id))) (cond ((string= "date" (first split-id)) (format nil "Posts from ~A" (second split-id))) ((string= "tag" (first split-id)) (format nil "Posts tagged ~A" (second split-id))) (t (format nil "Recent Posts"))))) (defmethod render-index (id page) (let* ((index-posts (index-posts (find-index id))) (start (* 10 (1- page))) (end (if (> (length index-posts) (+ start 9)) (+ start 9) (length index-posts))) (posts (subseq index-posts start end)) (content (funcall (find-symbol "INDEX" (theme-package)) (list :taglinks (taglinks) :monthlinks (monthlinks) :title (index-title id) :posts (loop for post in posts collect (list :url (post-url (post-id post)) :title (post-title post) :date (pretty-date (post-date post)) :contents (post-content post))) :prev (when (> page 1) (index-url id (1- page))) :next (when (< (* 10 page) (length index-posts)) (index-url id (1+ page))))))) content)) (defun index-file (id page) (if (string= "recent" id) (concatenate 'string (write-to-string page) ".html") (concatenate 'string id "/" (write-to-string page) ".html"))) (defmethod index-url (id page) (concatenate 'string *site-root* "/" (index-file id page)))