From ef378b9b56786120165c2b499494dbc5c3e1e3e6 Mon Sep 17 00:00:00 2001 From: Brit Butler Date: Wed, 27 Aug 2014 14:08:48 -0400 Subject: [PATCH] Finish the routing/slugs overhaul. --- src/content.lisp | 7 ++++++- src/documents.lisp | 23 ++++++++++++----------- src/indexes.lisp | 13 +++++++------ themes/atom.tmpl | 2 +- themes/hyde/index.tmpl | 10 +++++----- themes/hyde/post.tmpl | 6 +++--- themes/readable/index.tmpl | 6 +++--- themes/readable/post.tmpl | 6 +++--- themes/rss.tmpl | 4 ++-- 9 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/content.lisp b/src/content.lisp index 2c24f42..7b40945 100644 --- a/src/content.lisp +++ b/src/content.lisp @@ -4,7 +4,12 @@ (defclass tag () ((name :initarg :name :reader tag-name) - (slug :initarg :slug :reader tag-slug))) + (slug :initarg :slug :reader tag-slug) + (url :initarg :url))) + +(defmethod initialize-instance :after ((tag tag) &key) + (with-slots (url slug) tag + (setf url (compute-url nil slug 'tag-index)))) (defun make-tag (str) "Takes a string and returns a TAG instance with a name and slug." diff --git a/src/documents.lisp b/src/documents.lisp index bd7516e..0d62f48 100644 --- a/src/documents.lisp +++ b/src/documents.lisp @@ -28,9 +28,15 @@ (defgeneric page-url (document) (:documentation "The relative URL to the DOCUMENT.")) -(defun compute-url (document unique-id) - "Compute the relative URL for a DOCUMENT based on its UNIQUE-ID." - (let* ((class-name (class-name (class-of document))) +(defgeneric render (document &key &allow-other-keys) + (:documentation "Render the given DOCUMENT to HTML.")) + +;; Helper Functions + +(defun compute-url (document unique-id &optional class) + "Compute the relative URL for a DOCUMENT based on its UNIQUE-ID. If CLASS +is provided, it overrides the route used." + (let* ((class-name (or class (class-name (class-of document)))) (route (get-route class-name))) (unless route (error "No routing method found for: ~A" class-name)) @@ -38,10 +44,9 @@ (type (or (pathname-type result) (page-ext *config*)))) (make-pathname :type type :defaults result)))) -(defgeneric render (document &key &allow-other-keys) - (:documentation "Render the given DOCUMENT to HTML.")) - -;; Helper Functions +(defun get-route (doc-type) + "Return the route format string for DOC-TYPE." + (second (assoc (make-keyword doc-type) (routing *config*)))) (defun add-document (document) "Add DOCUMENT to the in-memory database. Error if a matching entry is present." @@ -63,10 +68,6 @@ use it as the template passing any RENDER-ARGS." (url (namestring (page-url document)))) (write-file (rel-path (staging-dir *config*) url) html))) -(defun get-route (doc-type) - "Return the route format string for DOC-TYPE." - (second (assoc (make-keyword doc-type) (routing *config*)))) - (defun find-all (doc-type) "Return a list of all instances of a given DOC-TYPE." (loop for val being the hash-values in *site* diff --git a/src/indexes.lisp b/src/indexes.lisp index 463b02e..40b295c 100644 --- a/src/indexes.lisp +++ b/src/indexes.lisp @@ -7,6 +7,7 @@ (defclass index () ((url :initarg :url :reader page-url) + (name :initarg :name :reader index-name) (title :initarg :title :reader title-of) (content :initarg :content :reader index-content))) @@ -33,7 +34,7 @@ (defun index-by-tag (tag content) "Return an index of all CONTENT matching the given TAG." - (make-instance 'tag-index :slug (tag-slug tag) + (make-instance 'tag-index :slug (tag-slug tag) :name (tag-name tag) :content (remove-if-not (lambda (x) (tag-p tag x)) content) :title (format nil "Content tagged ~a" (tag-name tag)))) @@ -52,7 +53,7 @@ (defun index-by-month (month content) "Return an index of all CONTENT matching the given MONTH." - (make-instance 'month-index :slug month + (make-instance 'month-index :slug month :name month :content (remove-if-not (lambda (x) (month-p month x)) content) :title (format nil "Content from ~a" month))) @@ -72,15 +73,15 @@ (defun index-by-n (i content) "Return the index for the Ith page of CONTENT in reverse chronological order." (let ((content (subseq content (* 10 i)))) - (make-instance 'numeric-index :slug (1+ i) + (make-instance 'numeric-index :slug (1+ i) :name (1+ i) :content (take-up-to 10 content) :title "Recent Content"))) (defmethod publish ((doc-type (eql (find-class 'numeric-index)))) - (let ((indexes (sort (find-all 'numeric-index) #'< :key #'index-slug))) + (let ((indexes (sort (find-all 'numeric-index) #'< :key #'index-name))) (dolist (index indexes) - (let ((prev (1- (index-slug index))) - (next (1+ (index-slug index)))) + (let ((prev (1- (index-name index))) + (next (1+ (index-name index)))) (write-document index nil :prev (when (plusp prev) prev) :next (when (<= next (length indexes)) next)))))) diff --git a/themes/atom.tmpl b/themes/atom.tmpl index 6061503..52e4ee4 100644 --- a/themes/atom.tmpl +++ b/themes/atom.tmpl @@ -14,7 +14,7 @@ {foreach $post in $content.content} - + {$post.title} {$post.date} {$post.date} diff --git a/themes/hyde/index.tmpl b/themes/hyde/index.tmpl index 954eeb8..326e517 100644 --- a/themes/hyde/index.tmpl +++ b/themes/hyde/index.tmpl @@ -4,20 +4,20 @@

{$index.title}

{foreach $obj in $index.content} {/foreach}
- {if $prev} Previous {/if} - {if $next} Next {/if} + {if $prev} Previous {/if} + {if $next} Next {/if}
{if $tags}

This blog covers {foreach $tag in $tags} - {$tag.name}{nil} + {$tag.name}{nil} {if not isLast($tag)},{sp}{/if} {/foreach}

@@ -26,7 +26,7 @@

View content from {foreach $month in $months} - {$month}{nil} + {$month.name}{nil} {if not isLast($month)},{sp}{/if} {/foreach}

diff --git a/themes/hyde/post.tmpl b/themes/hyde/post.tmpl index d66e8e9..f7b81bc 100644 --- a/themes/hyde/post.tmpl +++ b/themes/hyde/post.tmpl @@ -6,7 +6,7 @@
{\n} {if $post.tags} Tagged as {foreach $tag in $post.tags} - {$tag.name}{nil} + {$tag.name}{nil} {if not isLast($tag)},{sp}{/if} {/foreach} {/if} @@ -21,7 +21,7 @@ {$post.text |noAutoescape}
{\n}
{\n} - {if $prev} Previous
{/if}{\n} - {if $next} Next
{/if}{\n} + {if $prev} Previous
{/if}{\n} + {if $next} Next
{/if}{\n}
{\n} {/template} diff --git a/themes/readable/index.tmpl b/themes/readable/index.tmpl index 5cf5514..1061f93 100644 --- a/themes/readable/index.tmpl +++ b/themes/readable/index.tmpl @@ -4,7 +4,7 @@

{$index.title}

{foreach $obj in $index.content}
-

{$obj.title}

+

{$obj.title}

posted on {$obj.date}

{$obj.text |noAutoescape}
@@ -13,7 +13,7 @@

This blog covers {foreach $tag in $tags} - {$tag.name}{nil} + {$tag.name}{nil} {if not isLast($tag)},{sp}{/if} {/foreach}

@@ -23,7 +23,7 @@

View content from {foreach $month in $months} - {$month}{nil} + {$month.name}{nil} {if not isLast($month)},{sp}{/if} {/foreach}

diff --git a/themes/readable/post.tmpl b/themes/readable/post.tmpl index 76bb24a..051750b 100644 --- a/themes/readable/post.tmpl +++ b/themes/readable/post.tmpl @@ -6,7 +6,7 @@

{if $post.tags} Tagged as {foreach $tag in $post.tags} - {$tag.name}{nil} + {$tag.name}{nil} {if not isLast($tag)},{sp}{/if} {/foreach} {/if} @@ -20,8 +20,8 @@ {$post.text |noAutoescape}

{\n} {/template} diff --git a/themes/rss.tmpl b/themes/rss.tmpl index bcd4460..7a4ecb5 100644 --- a/themes/rss.tmpl +++ b/themes/rss.tmpl @@ -13,10 +13,10 @@ {foreach $post in $content.content} {$post.title} - {$config.domain}/{$post.path} + {$config.domain}/{$post.url} {$post.date} {$config.author} - {$config.domain}/{$post.path} + {$config.domain}/{$post.url} {foreach $tag in $post.tags} {/foreach}