Skip to content

Commit

Permalink
Introduce custom bindings form for posts.
Browse files Browse the repository at this point in the history
Use `bindings: name1 value1 ...` to bind special variables while
compiling a post.  Only a single bindings directive is supported in the
post metadata.
  • Loading branch information
Ferada committed Nov 16, 2015
1 parent 9651201 commit 5b20c4d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
31 changes: 23 additions & 8 deletions src/content.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,32 @@
;; Content Types

(defclass content ()
((url :initarg :url :reader page-url)
(date :initarg :date :reader content-date)
(file :initarg :file :reader content-file)
(tags :initarg :tags :reader content-tags)
(text :initarg :text :reader content-text))
(:default-initargs :tags nil :date nil))
((url :initarg :url :reader page-url)
(date :initarg :date :reader content-date)
(file :initarg :file :reader content-file)
(tags :initarg :tags :reader content-tags)
(text :initarg :text :reader content-text)
(bindings :initarg :bindings :reader content-bindings))
(:default-initargs :tags nil :date nil :bindings nil :code nil))

(defun read-bindings-from-string (string)
"Read multiple pairs of values from STRING and return them as two LISTs
of variables and values ready to be passed into PROGV."
(with-input-from-string (stream string)
(loop
for variable = (read stream nil)
for value = (read stream nil)
while variable
collect variable into variables
collect value into values
finally (return (cons variables values)))))

(defmethod initialize-instance :after ((object content) &key)
(with-slots (tags) object
(with-slots (tags bindings) object
(when (stringp tags)
(setf tags (mapcar #'make-tag (cl-ppcre:split "," tags))))))
(setf tags (mapcar #'make-tag (cl-ppcre:split "," tags))))
(when bindings
(setf bindings (read-bindings-from-string bindings)))))

(defun parse-initarg (line)
"Given a metadata header, LINE, parse an initarg name/value pair from it."
Expand Down
11 changes: 6 additions & 5 deletions src/posts.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
(:default-initargs :author nil))

(defmethod initialize-instance :after ((object post) &key)
(with-slots (url title author format text) object
(setf url (compute-url object (slugify title))
format (make-keyword (string-upcase format))
text (render-text text format)
author (or author (author *config*)))))
(with-slots (url title author format text bindings) object
(progv (car bindings) (cdr bindings)
(setf url (compute-url object (slugify title))
format (make-keyword (string-upcase format))
text (render-text text format)
author (or author (author *config*))))))

(defmethod render ((object post) &key prev next)
(funcall (theme-fn 'post) (list :config *config*
Expand Down

0 comments on commit 5b20c4d

Please sign in to comment.