miniblog

Miniblog: A command-line static blog system in Common Lisp
Log | Files | Refs | README | LICENSE

commit 6e7ab0dfdbe4f2de63925b6a3e27e73f29484fe6
parent f378df5dd6db182615a4adc4e8bc251db98763e3
Author: Decay <decay@todayiwilllaunchmyinfantsonintoorbit.com>
Date:   Mon, 12 Oct 2020 21:58:16 -0700

Getting rid of the spurious make-*-format functions

Also introducing a wrapper function to augment the DB data
transformations. Currently it just creates duplicate date fields.

Diffstat:
Msrc/content.lisp | 15++++++++-------
Msrc/data.lisp | 43+++++++++++++++++++++++++++----------------
Msrc/format.lisp | 44++++++++++++++++++++++----------------------
Msrc/miniblog.lisp | 2+-
Msrc/packages.lisp | 5+++--
Mtst/format.lisp | 14+++++---------
6 files changed, 66 insertions(+), 57 deletions(-)

diff --git a/src/content.lisp b/src/content.lisp @@ -34,9 +34,9 @@ :month month :pages pages :archive-date-list archive-date-list - :content-formatter (miniblog.format:make-content-formatter) - :short-date-formatter (miniblog.format:make-short-date-formatter (or tz *default-timezone*)) - :long-date-formatter (miniblog.format:make-long-date-formatter (or tz *default-timezone*))))))) + :content-formatter #'miniblog.format:markdown + :short-date-formatter (lambda (datetime) (miniblog.format:short-date-format datetime (or tz *default-timezone*))) + :long-date-formatter (lambda (datetime) (miniblog.format:long-date-format datetime (or tz *default-timezone*)))))))) (defun make-page-generator (&key title root-uri header links stylesheet) (lambda (entry path pages &key archive-date-list tz enable-rss) @@ -54,8 +54,9 @@ :post entry :path path :archive-date-list archive-date-list - :content-formatter (miniblog.format:make-content-formatter) - :long-date-formatter (miniblog.format:make-long-date-formatter (or tz *default-timezone*))))))) + :content-formatter #'miniblog.format:markdown + :short-date-formatter (lambda (datetime) (miniblog.format:short-date-format datetime (or tz *default-timezone*))) + :long-date-formatter (lambda (datetime) (miniblog.format:long-date-format datetime (or tz *default-timezone*)))))))) (defun strip-html-tags (content) (format nil "~{~A~}" @@ -84,9 +85,9 @@ :category category :posts entries :build-date (now) - :content-formatter (miniblog.format:make-content-formatter) + :content-formatter #'miniblog.format:markdown :content-stripper #'strip-html-tags - :rfc-822-date-formatter (miniblog.format:make-rfc-822-date-formatter (or tz *default-timezone*)))))) + :rfc-822-date-formatter (lambda (datetime) (miniblog.format:rfc-822-format datetime (or tz *default-timezone*))))))) (lambda (entries &key tz) (declare (ignore entries tz))))) diff --git a/src/data.lisp b/src/data.lisp @@ -1,29 +1,40 @@ (in-package :miniblog.data) +(defun format-dates (content) + (let ((created-at (getf content :created-at)) + (last-updated-at (getf content :last-updated-at))) + (setf (getf content :created-at-short) created-at) + (setf (getf content :created-at-long) created-at) + (setf (getf content :last-updated-at-short) last-updated-at) + (setf (getf content :last-updated-at-long) last-updated-at) + content)) + (defgeneric xform (entry) (:documentation "Transform an entry of some type into an idiomatic p-list")) (defmethod xform ((entry blog-entries)) "Transform a blog-entries object into an idiomatic property list" - (list :id (mito.dao.mixin:object-id entry) - :created-at (mito.dao.mixin:object-created-at entry) - :last-updated-at (mito.dao.mixin:object-updated-at entry) - :title (entry-title entry) - :content (entry-content entry) - :created-by (entry-username entry) - :last-updated-by (entry-last-updated-by entry))) + (format-dates + (list :id (mito.dao.mixin:object-id entry) + :created-at (mito.dao.mixin:object-created-at entry) + :last-updated-at (mito.dao.mixin:object-updated-at entry) + :title (entry-title entry) + :content (entry-content entry) + :created-by (entry-username entry) + :last-updated-by (entry-last-updated-by entry)))) (defmethod xform ((entry blog-pages)) "Transform a blog-pages object into an idiomatic p-list" - (list :id (mito.dao.mixin:object-id entry) - :created-at (mito.dao.mixin:object-created-at entry) - :last-updated-at (mito.dao.mixin:object-updated-at entry) - :name (page-name entry) - :parent (page-parent entry) - :title (page-title entry) - :content (page-content entry) - :created-by (page-username entry) - :last-updated-by (page-last-updated-by entry))) + (format-dates + (list :id (mito.dao.mixin:object-id entry) + :created-at (mito.dao.mixin:object-created-at entry) + :last-updated-at (mito.dao.mixin:object-updated-at entry) + :name (page-name entry) + :parent (page-parent entry) + :title (page-title entry) + :content (page-content entry) + :created-by (page-username entry) + :last-updated-by (page-last-updated-by entry)))) (defun add-entry (title content &key (username "nobody")) (miniblog.db:add-entry title content :username username :transform #'xform)) diff --git a/src/format.lisp b/src/format.lisp @@ -10,28 +10,28 @@ "Long format datetime, like: Monday, February 3rd 2020 at 2:46 PM PST") -(defun make-content-formatter () - "Return the default content formatter (parses Markdown)" - (lambda (content) - (nth 1 (multiple-value-list (markdown content :stream nil))))) +(defun markdown (content) + "Translate CONTENT with CL-MARKDOWN" + (nth 1 (multiple-value-list (cl-markdown:markdown content :stream nil)))) + +(defun rfc-822-format (datetime tz) + "RFC 822/1123 date formatter for RSS items" + (format-timestring nil datetime + :format +rfc-1123-format+ + :timezone tz)) -(defun make-rfc-822-date-formatter (&optional (timezone *default-timezone*)) - "Return an RFC 822/1123-style date formatter for RSS items" - (lambda (datetime) - (format-timestring nil datetime - :format +rfc-1123-format+ - :timezone timezone))) +(defun short-date-format (datetime tz) + "Short-datetime formatter (see +short-date-format+)" + (format-timestring nil datetime + :format +short-date-format+ + :timezone tz)) -(defun make-short-date-formatter (&optional (timezone *default-timezone*)) - "Return the default short-date formatter (see +short-date-format+)" - (lambda (datetime) - (format-timestring nil datetime - :format +short-date-format+ - :timezone timezone))) +(defun long-date-format (datetime tz) + "Long-datetime formatter (see +long-date-format+)" + (format-timestring nil datetime + :format +long-date-format+ + :timezone tz)) -(defun make-long-date-formatter (&optional (timezone *default-timezone*)) - "Return the default long-datetime formatter (see +long-date-format+)" - (lambda (datetime) - (format-timestring nil datetime - :format +long-date-format+ - :timezone timezone))) +(defun make-content-formatter () + "Return the default content formatter (parses Markdown)" + #'markdown) diff --git a/src/miniblog.lisp b/src/miniblog.lisp @@ -279,7 +279,7 @@ (format t "Invalid page name ~a~%" uri))) (defun date-format (datetime) - (funcall (miniblog.format:make-long-date-formatter *blog-timezone*) datetime)) + (miniblog.format:long-date-format datetime *blog-timezone*)) (defgeneric get-entry (entry-type id) (:documentation "Get an entry of type ENTRY-TYPE with id ID")) diff --git a/src/packages.lisp b/src/packages.lisp @@ -1,8 +1,9 @@ (in-package :cl-user) (defpackage :miniblog.format - (:use :cl :cl-markdown :local-time) - (:export #:make-content-formatter + (:use :cl :local-time) + (:export #:markdown #:rfc-822-format #:short-date-format #:long-date-format + #:make-content-formatter #:make-rfc-822-date-formatter #:make-short-date-formatter #:make-long-date-formatter)) diff --git a/tst/format.lisp b/tst/format.lisp @@ -4,18 +4,14 @@ (test test-date-formatters "Validate that date formatters return expected results" - (let ((ts (encode-timestamp 0 0 0 12 20 4 2020 :timezone +utc-zone+)) - (rfc-822 (miniblog.format:make-rfc-822-date-formatter +utc-zone+)) - (short-date (miniblog.format:make-short-date-formatter +utc-zone+)) - (long-date (miniblog.format:make-long-date-formatter +utc-zone+))) - (is (string= "Mon, 20 Apr 2020 12:00:00 +0000" (funcall rfc-822 ts))) - (is (string= "2020-04-20" (funcall short-date ts))) - (is (string= "Monday, April 20th 2020 at 12:00 pm UTC" (funcall long-date ts))))) + (let ((ts (encode-timestamp 0 0 0 12 20 4 2020 :timezone +utc-zone+))) + (is (string= "Mon, 20 Apr 2020 12:00:00 +0000" (miniblog.format:rfc-822-format ts +utc-zone+))) + (is (string= "2020-04-20" (miniblog.format:short-date-format ts +utc-zone+))) + (is (string= "Monday, April 20th 2020 at 12:00 pm UTC" (miniblog.format:long-date-format ts +utc-zone+))))) (test test-markdown-formatter "Simple validation that the Markdown formatter works" - (let* ((formatter (miniblog.format:make-content-formatter)) - (html (funcall formatter "# foo")) + (let* ((html (miniblog.format:markdown "# foo")) (html-frag (parse-html5-fragment html)) (maybe-h1 (node-first-child html-frag)) (maybe-text (node-first-child maybe-h1)))