From 25891fb7ca8e7b2c4da975a592cb3e0f209bfac7 Mon Sep 17 00:00:00 2001 From: tycho garen Date: Sun, 2 Mar 2014 11:54:39 -0500 Subject: [PATCH 1/2] adding additional data to the environment, making more build factors configurable --- src/coleslaw.lisp | 27 ++++++++++++++++----------- src/config.lisp | 37 ++++++++++++++++++++++++------------- src/content.lisp | 4 ++-- src/packages.lisp | 1 + src/posts.lisp | 23 ++++++++++++++--------- 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/src/coleslaw.lisp b/src/coleslaw.lisp index 9306ed3..433e2e8 100644 --- a/src/coleslaw.lisp +++ b/src/coleslaw.lisp @@ -16,10 +16,16 @@ (:documentation "The url to the object, without the domain.")) (defmethod page-url :around ((object t)) - (let ((result (call-next-method))) + (let ((result (call-next-method)) + (extension (if (string-equal (pageext *config*) "/") + "html" + (pageext *config*)))) + (when (string= (char extension 0) ".") + (setf extension (string-trim "." extension))) (if (pathname-type result) result - (make-pathname :type "html" :defaults result)))) + (make-pathname :type extension :defaults result) + ))) (defun page-path (object) "The path to store OBJECT at once rendered." @@ -46,8 +52,6 @@ Additional args to render CONTENT can be passed via RENDER-ARGS." (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*)) @@ -55,7 +59,7 @@ Additional args to render CONTENT can be passed via RENDER-ARGS." (app-path "themes/~a/js" (theme *config*)) (merge-pathnames "static" (repo *config*)))) (when (probe-file dir) - (run-program "cp -R ~a ." dir))) + (run-program "rsync --delete -raz ~a ." dir))) (do-ctypes (publish (make-keyword ctype))) (render-indices) (update-symlink "index.html" "1.html") @@ -88,9 +92,10 @@ Additional args to render CONTENT can be passed via RENDER-ARGS." (defun preview (path &optional (content-type 'post)) "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." - (unless *config* - (load-config nil) - (compile-theme (theme *config*))) - (let* ((file (rel-path (repo *config*) path)) - (content (construct content-type (read-content file)))) - (write-page "~/tmp.html" (render-page content)))) + (let ((current-working-directory (cl-fad:pathname-directory-pathname path))) + (unless *config* + (load-config (namestring current-working-directory)) + (compile-theme (theme *config*))) + (let* ((file (rel-path (repo *config*) path)) + (content (construct content-type (read-content file)))) + (write-page "tmp.html" (render-page content))))) diff --git a/src/config.lisp b/src/config.lisp index fc35d34..3a92ef7 100644 --- a/src/config.lisp +++ b/src/config.lisp @@ -1,17 +1,20 @@ (in-package :coleslaw) (defclass blog () - ((author :initarg :author :accessor author) - (deploy-dir :initarg :deploy-dir :accessor deploy-dir) - (domain :initarg :domain :accessor domain) - (feeds :initarg :feeds :accessor feeds) - (license :initarg :license :accessor license) - (plugins :initarg :plugins :accessor plugins) - (repo :initarg :repo :accessor repo) - (sitenav :initarg :sitenav :accessor sitenav) - (staging-dir :initarg :staging-dir :accessor staging-dir) - (title :initarg :title :accessor title) - (theme :initarg :theme :accessor theme))) + ((author :initarg :author :accessor author) + (deploy-dir :initarg :deploy-dir :accessor deploy-dir) + (domain :initarg :domain :accessor domain) + (feeds :initarg :feeds :accessor feeds) + (license :initarg :license :accessor license) + (plugins :initarg :plugins :accessor plugins) + (repo :initarg :repo :accessor repo) + (sitenav :initarg :sitenav :accessor sitenav) + (staging-dir :initarg :staging-dir :accessor staging-dir) + (postsdir :initarg :postsdir :accessor postsdir :initform "posts") + (separator :initarg :separator :accessor separator :initform ";;;;;") + (pageext :initarg :pageext :accessor pageext :initform ".html") + (title :initarg :title :accessor title) + (theme :initarg :theme :accessor theme))) (define-condition unknown-config-section-error (error) ((text :initarg :text :reader text))) @@ -37,10 +40,18 @@ are in the plugins folder in coleslaw's source directory." (destructuring-bind (name &rest args) plugin (apply 'enable-plugin (plugin-path name) args))))) -(defun load-config (&optional config-key (dir (user-homedir-pathname))) +(defun discover-config-path (&optional (path "")) + (let ((default-path (make-pathname :directory (namestring (user-homedir-pathname)) :name ".coleslawrc")) + (custom-path (make-pathname :directory path :name ".coleslawrc"))) + (cond + ((file-exists-p custom-path) custom-path) + ((file-exists-p default-path) default-path)))) + +(defun load-config (config-key) "Load the coleslaw configuration from DIR/.coleslawrc, using CONFIG-KEY if necessary. DIR is ~ by default." - (with-open-file (in (merge-pathnames ".coleslawrc" dir)) + + (with-open-file (in (discover-config-path config-key)) (let ((config-form (read in))) (if (symbolp (car config-form)) ;; Single site config: ignore CONFIG-KEY. diff --git a/src/content.lisp b/src/content.lisp index 1d5ff32..4d99760 100644 --- a/src/content.lisp +++ b/src/content.lisp @@ -51,10 +51,10 @@ (read-tags (str) (mapcar #'make-tag (cl-ppcre:split "," str)))) (with-open-file (in file) - (unless (string= (read-line in) ";;;;;") + (unless (string= (read-line in) (separator *config*)) (error "The provided file lacks the expected header.")) (let ((meta (loop for line = (read-line in nil) - until (string= line ";;;;;") + until (string= line (separator *config*)) appending (list (field-name line) (aref (parse-field line) 0)))) (content (slurp-remainder in))) diff --git a/src/packages.lisp b/src/packages.lisp index e95e3a2..f946309 100644 --- a/src/packages.lisp +++ b/src/packages.lisp @@ -5,6 +5,7 @@ #:make-keyword #:mappend #:compose) + (:import-from :cl-fad #:file-exists-p) (:import-from :closure-template #:compile-template) (:export #:main #:preview diff --git a/src/posts.lisp b/src/posts.lisp index 898c81e..f4f1529 100644 --- a/src/posts.lisp +++ b/src/posts.lisp @@ -2,8 +2,21 @@ (defclass post (content) ((title :initform nil :initarg :title :accessor post-title) + (author :initform nil :initarg :author :accessor post-author) (format :initform nil :initarg :format :accessor post-format))) +(defmethod initialize-instance :after ((object post) &key) + (with-accessors ((title post-title) + (author post-author) + (format post-format) + (text content-text)) object + (setf (content-slug object) (slugify title) + format (make-keyword (string-upcase format)) + text (render-content text format) + author (if author + author + (author *config*))))) + (defmethod render ((object post) &key prev next) (funcall (theme-fn 'post) (list :config *config* :post object @@ -11,15 +24,7 @@ :next next))) (defmethod page-url ((object post)) - (format nil "posts/~a" (content-slug object))) - -(defmethod initialize-instance :after ((object post) &key) - (with-accessors ((title post-title) - (format post-format) - (text content-text)) object - (setf (content-slug object) (slugify title) - format (make-keyword (string-upcase format)) - text (render-content text format)))) + (format nil "~a/~a" (postsdir *config*) (content-slug object))) (defmethod publish ((content-type (eql :post))) (loop for (next post prev) on (append '(nil) (by-date (find-all 'post))) From 795ddf5f1beccb08ec014c658cfe8b9733b6dd6f Mon Sep 17 00:00:00 2001 From: tycho garen Date: Sat, 22 Mar 2014 12:53:49 -0400 Subject: [PATCH 2/2] revisions to configuration option patch in response to code review --- src/coleslaw.lisp | 6 ++---- src/config.lisp | 17 +++++++++-------- src/posts.lisp | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/coleslaw.lisp b/src/coleslaw.lisp index 433e2e8..48f6fb8 100644 --- a/src/coleslaw.lisp +++ b/src/coleslaw.lisp @@ -17,11 +17,9 @@ (defmethod page-url :around ((object t)) (let ((result (call-next-method)) - (extension (if (string-equal (pageext *config*) "/") + (extension (if (string= (page-ext *config*) "/") "html" - (pageext *config*)))) - (when (string= (char extension 0) ".") - (setf extension (string-trim "." extension))) + (page-ext *config*)))) (if (pathname-type result) result (make-pathname :type extension :defaults result) diff --git a/src/config.lisp b/src/config.lisp index 3a92ef7..074daef 100644 --- a/src/config.lisp +++ b/src/config.lisp @@ -10,9 +10,9 @@ (repo :initarg :repo :accessor repo) (sitenav :initarg :sitenav :accessor sitenav) (staging-dir :initarg :staging-dir :accessor staging-dir) - (postsdir :initarg :postsdir :accessor postsdir :initform "posts") - (separator :initarg :separator :accessor separator :initform ";;;;;") - (pageext :initarg :pageext :accessor pageext :initform ".html") + (posts-dir :initarg :posts-dir :accessor posts-dir :initform "posts") + (separator :initarg :separator :accessor separator :initform ";;;;;") + (page-ext :initarg :page-ext :accessor page-ext :initform "html") (title :initarg :title :accessor title) (theme :initarg :theme :accessor theme))) @@ -41,11 +41,12 @@ are in the plugins folder in coleslaw's source directory." (apply 'enable-plugin (plugin-path name) args))))) (defun discover-config-path (&optional (path "")) - (let ((default-path (make-pathname :directory (namestring (user-homedir-pathname)) :name ".coleslawrc")) - (custom-path (make-pathname :directory path :name ".coleslawrc"))) - (cond - ((file-exists-p custom-path) custom-path) - ((file-exists-p default-path) default-path)))) + "Checks the project directory for a coleslawrc and if one +doesn't exist, uses the coleslawrc in the home directory." + (let ((rel-path (make-pathname :directory path :name ".coleslawrc"))) + (if (file-exists-p rel-path) + rel-path + (make-pathname :directory (namestring (user-homedir-pathname)) :name ".coleslawrc")))) (defun load-config (config-key) "Load the coleslaw configuration from DIR/.coleslawrc, using CONFIG-KEY diff --git a/src/posts.lisp b/src/posts.lisp index f4f1529..baacf15 100644 --- a/src/posts.lisp +++ b/src/posts.lisp @@ -24,7 +24,7 @@ :next next))) (defmethod page-url ((object post)) - (format nil "~a/~a" (postsdir *config*) (content-slug object))) + (format nil "~a/~a" (posts-dir *config*) (content-slug object))) (defmethod publish ((content-type (eql :post))) (loop for (next post prev) on (append '(nil) (by-date (find-all 'post)))