coleslaw/src/coleslaw.lisp

97 lines
3.6 KiB
Common Lisp
Raw Normal View History

2011-04-16 15:45:37 -04:00
(in-package :coleslaw)
(defgeneric render (object &key &allow-other-keys)
(:documentation "Render the given OBJECT to HTML."))
(defgeneric render-content (text format)
(:documentation "Compile TEXT from the given FORMAT to HTML for display.")
(:method (text (format (eql :html)))
text)
2012-12-11 12:38:54 +02:00
(:method (text (format (eql :md)))
(let ((3bmd-code-blocks:*code-blocks* t))
(with-output-to-string (str)
(3bmd:parse-string-and-print-to-stream text str)))))
2013-04-26 10:03:04 +08:00
(defgeneric page-url (object)
2013-04-28 10:11:38 -04:00
(:documentation "The url to the object, without the domain."))
2012-09-12 10:38:42 -04:00
2013-04-26 10:03:04 +08:00
(defmethod page-url :around ((object t))
2012-12-14 15:04:21 -05:00
(let ((result (call-next-method)))
2013-04-28 10:11:38 -04:00
(if (pathname-type result)
result
(make-pathname :type "html" :defaults result))))
2013-04-26 10:03:04 +08:00
(defun page-path (object)
"The path to store OBJECT at once rendered."
2013-04-28 10:11:38 -04:00
(rel-path (staging-dir *config*) (namestring (page-url object))))
2012-09-12 10:38:42 -04:00
(defun render-page (content &optional theme-fn &rest render-args)
"Render the given CONTENT to disk using THEME-FN if supplied.
Additional args to render CONTENT can be passed via RENDER-ARGS."
(funcall (or theme-fn (theme-fn 'base))
(list :config *config*
:content content
:raw (apply 'render content render-args)
:pubdate (make-pubdate)
:injections (find-injections content))))
(defun write-page (filepath page)
"Write the given PAGE to FILEPATH."
(ensure-directories-exist filepath)
(with-open-file (out filepath
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-line page out)))
(defun compile-blog (staging)
"Compile the blog to a STAGING directory as specified in .coleslawrc."
(when (probe-file staging)
(run-program "rm -R ~a" staging))
(ensure-directories-exist staging)
(with-current-directory staging
(dolist (dir (list (app-path "themes/~a/css" (theme *config*))
(app-path "themes/~a/img" (theme *config*))
(app-path "themes/~a/js" (theme *config*))
(merge-pathnames "static" (repo *config*))))
(when (probe-file dir)
(run-program "cp -R ~a ." dir)))
(do-ctypes (publish (make-keyword ctype)))
(render-indices)
(update-symlink "index.html" "1.html")
(render-feeds (feeds *config*))))
(defgeneric deploy (staging)
(:documentation "Deploy the STAGING dir, updating the .prev and .curr symlinks.")
(:method (staging)
(let* ((dest (deploy-dir *config*))
2013-04-18 15:53:42 -04:00
(new-build (rel-path dest "generated/~a" (get-universal-time)))
(prev (rel-path dest ".prev"))
(curr (rel-path dest ".curr")))
(ensure-directories-exist new-build)
(run-program "mv ~a ~a" staging new-build)
2013-05-11 21:44:42 -04:00
(when (and (probe-file prev) (truename prev))
(run-program "rm -r ~a" (truename prev)))
2013-04-18 15:53:42 -04:00
(when (probe-file curr)
(update-symlink prev (truename curr)))
(update-symlink curr new-build))))
(defun main (&optional config-key)
"Load the user's config file, then compile and deploy the blog."
(load-config config-key)
(load-content)
(compile-theme (theme *config*))
(let ((blog (staging-dir *config*)))
(compile-blog blog)
(deploy blog)))
2013-04-09 16:23:10 -04:00
(defun preview (path &optional (content-type 'post))
2013-04-09 16:39:39 -04:00
"Render the content at PATH under user's configured repo and save it to
~/tmp.html. Load the user's config and theme if necessary."
2013-04-09 16:23:10 -04:00
(unless *config*
(load-config nil)
(compile-theme (theme *config*)))
2013-04-09 16:39:39 -04:00
(let* ((file (rel-path (repo *config*) path))
(content (construct content-type (read-content file))))
(write-page "~/tmp.html" (render-page content))))