Sketch out posts, DO-FILES improvements.

This commit is contained in:
Brit Butler 2012-08-20 10:44:46 -04:00
parent 4b264e2587
commit 3736c0862d
3 changed files with 53 additions and 38 deletions

View file

@ -9,11 +9,17 @@ If ARGS is provided, use (apply 'format nil PATH ARGS) as the value of PATH."
"Convert an iolib file-path back to a pathname." "Convert an iolib file-path back to a pathname."
(merge-pathnames (file-path-namestring file) parent)) (merge-pathnames (file-path-namestring file) parent))
(defmacro do-files ((var path) &body body) (defmacro do-files ((var path &optional extension) &body body)
"For each file under PATH, run BODY." "For each file under PATH, run BODY. If EXTENSION is provided, only run BODY
on files that match the given extension."
(alexandria:with-gensyms (ext)
`(iolib.os:mapdir (lambda (x) `(iolib.os:mapdir (lambda (x)
(let ((,var (to-pathname x ,path))) (let* ((,var (to-pathname x ,path))
,@body)) ,path)) ,@(when extension `((,ext (pathname-type ,var)))))
,@(if extension
`((when (and ,ext (string= ,ext ,extension))
,@body))
`,body))) ,path)))
(defun compile-blog () (defun compile-blog ()
(let ((staging #p"/tmp/coleslaw/")) (let ((staging #p"/tmp/coleslaw/"))

View file

@ -1,34 +1,38 @@
(in-package :coleslaw) (in-package :coleslaw)
(defparameter *metadata* (make-hash-table :test #'equal))
(defclass post () (defclass post ()
((id :initform nil :initarg :id ((slug :initform nil :initarg :slug :accessor post-slug)
:accessor post-id) (title :initform nil :initarg :title :accessor post-title)
(title :initform nil :initarg :title (tags :initform nil :initarg :tags :accessor post-tags)
:accessor post-title) (date :initform nil :initarg :date :accessor post-date)
(tags :initform nil :initarg :tags (format :initform nil :initarg :format :accessor post-format)
:accessor post-tags) (content :initform nil :initarg :content :accessor post-content)
(date :initform nil :initarg :date (aliases :initform nil :initarg :aliases :accessor post-aliases)))
:accessor post-date)
(content :initform nil :initarg :content
:accessor post-content)
(aliases :initform nil :initarg :aliases
:accessor post-aliases)))
(defgeneric make-post (title tags date content (defun render-posts ()
&key id aliases &allow-other-keys) (do-files (file (repo *config*) "post")
(:documentation "Create a POST with the given data.")) (with-open-file (in file)
(let ((post (read-post in)))
(setf (gethash (post-slug post) *metadata*) post)))
(maphash #'write-post *metadata*)))
(defgeneric add-post (post id) (defun read-post (stream)
(:documentation "Insert a post into *storage* with the given ID.")) "Make a POST instance based on the data from STREAM."
(defgeneric remove-post (id) )
(:documentation "Remove a post from *storage* matching ID."))
(defgeneric render-post (id) (defun write-post (slug post)
(:documentation "Generate the final HTML for the post with given ID.")) "Write out the HTML for POST in SLUG.html."
(with-open-file (out (format nil "~a.html" slug)
(defgeneric find-post (id) :direction :output
(:documentation "Retrieve a post from *storage* matching ID.")) :if-does-not-exist :create)
(let ((content (funcall (theme-fn "POST")
(defgeneric post-url (id) (list :title (post-title post)
(:documentation "Return the URL for the post with the given ID.")) :tags (post-tags post)
:date (post-date post)
:content (post-content post)
:prev nil
:next nil))))
(write content out))))

View file

@ -1,19 +1,24 @@
(in-package :coleslaw) (in-package :coleslaw)
(defparameter *injections* (make-hash-table :test #'equal))
(defgeneric add-injection (str location) (defgeneric add-injection (str location)
(:documentation "Add STR to the list of elements injected in LOCATION.")) (:documentation "Add STR to the list of elements injected in LOCATION.")
(:method ((str string) location)
(pushnew str (gethash location *injections*) :test #'string=)))
(defgeneric remove-injection (str location) (defgeneric remove-injection (str location)
(:documentation "Remove STR from the list of elements injected in LOCATION.")) (:documentation "Remove STR from the list of elements injected in LOCATION.")
(:method ((str string) location)
(setf (gethash location *injections*)
(remove str (gethash location *injections*) :test #'string=))))
(defun theme-package (&key (name (theme *config*))) (defun theme-package (&key (name (theme *config*)))
(find-package (string-upcase (concatenate 'string "coleslaw.theme." name)))) (find-package (string-upcase (concatenate 'string "coleslaw.theme." name))))
(defun compile-theme (&key (theme-dir (app-path "themes/~a/" (theme *config*)))) (defun compile-theme (&key (theme-dir (app-path "themes/~a/" (theme *config*))))
(do-files (file theme-dir) (do-files (file theme-dir "tmpl")
(let ((extension (pathname-type file))) (compile-template :common-lisp-backend file)))
(when (and extension (string= extension "tmpl"))
(compile-template :common-lisp-backend file)))))
;; DOCUMENTATION ;; DOCUMENTATION
;; A theme directory should be named after the theme and contain *.tmpl files ;; A theme directory should be named after the theme and contain *.tmpl files