Parcourir la Source

Merge pull request #33 from mrordinaire/sitemap

Added sitemap generation.
Brit Butler il y a 12 ans
Parent
commit
e6c6bcbe26
6 fichiers modifiés avec 68 ajouts et 16 suppressions
  1. 6 0
      docs/plugin-use.md
  2. 29 0
      plugins/sitemap.lisp
  3. 10 6
      src/coleslaw.lisp
  4. 8 8
      src/indices.lisp
  5. 2 2
      src/posts.lisp
  6. 13 0
      themes/sitemap.tmpl

+ 6 - 0
docs/plugin-use.md

@@ -55,3 +55,9 @@
55 55
 **Description**: Import blog posts from Wordpress using their export tool. Blog entries will be read from the XML and converted into .post files. Afterwards the XML file will be deleted to prevent reimporting. Optionally an ```:output``` argument may be supplied to the plugin. If provided, it should be a directory in which to store the .post files. Otherwise, the value of ```:repo``` in your .coleslawrc will be used.
56 56
 
57 57
 **Example**: ```(import :filepath "/home/redline/redlinernotes-export.timestamp.xml" :output "/home/redlinernotes/blog/")```
58
+
59
+## Sitemap generator
60
+
61
+**Description**: this plugin generates a sitemap.xml under the page root, which is useful if you want google to crawl your site.
62
+
63
+**Example**: ```(sitemap)```

+ 29 - 0
plugins/sitemap.lisp

@@ -0,0 +1,29 @@
1
+(defpackage :coleslaw-sitemap 
2
+  (:use :cl)
3
+  (:import-from :coleslaw
4
+                #:*config*
5
+                #:deploy
6
+                #:domain
7
+                #:find-all
8
+                #:page-url
9
+                #:rel-path
10
+                #:staging-dir
11
+                #:theme-fn
12
+                #:write-page)
13
+  (:export #:enable))
14
+
15
+(in-package :coleslaw-sitemap) 
16
+
17
+(defmethod deploy :before (staging)
18
+  "Render sitemap.xml under document root"
19
+  (let* ((urls (append '("" "sitemap.xml") ; empty string is for root url
20
+                       (mapcar #'page-url (find-all 'coleslaw:post)))))
21
+    (write-page (rel-path (staging-dir *config*) "sitemap.xml")
22
+                (funcall (theme-fn :sitemap "feeds")
23
+                         (list :domain (domain *config*)
24
+                               :urls urls
25
+                               :pubdate (local-time:format-rfc3339-timestring
26
+                                          nil
27
+                                          (local-time:now))))))) 
28
+
29
+(defun enable ())

+ 10 - 6
src/coleslaw.lisp

@@ -12,14 +12,18 @@
12 12
       (with-output-to-string (str)
13 13
         (3bmd:parse-string-and-print-to-stream text str)))))
14 14
 
15
-(defgeneric page-path (object)
16
-  (:documentation "The path to store OBJECT at once rendered."))
15
+(defgeneric page-url (object)
16
+  (:documentation "The url to the object, without the domain"))
17 17
 
18
-(defmethod page-path :around ((object t))
18
+(defmethod page-url :around ((object t))
19 19
   (let ((result (call-next-method)))
20
-    (if (pathname-type result)
21
-        result
22
-        (make-pathname :type "html" :defaults result))))
20
+    (namestring (if (pathname-type result)
21
+                  result
22
+                  (make-pathname :type "html" :defaults result)))))
23
+
24
+(defun page-path (object)
25
+  "The path to store OBJECT at once rendered."
26
+  (rel-path (staging-dir *config*) (page-url object)))
23 27
 
24 28
 (defun render-page (content &optional theme-fn &rest render-args)
25 29
   "Render the given CONTENT to disk using THEME-FN if supplied.

+ 8 - 8
src/indices.lisp

@@ -17,14 +17,14 @@
17 17
 (defclass date-index (index) ())
18 18
 (defclass int-index (index) ())
19 19
 
20
-(defmethod page-path ((object index))
21
-  (rel-path (staging-dir *config*) (index-id object)))
22
-(defmethod page-path ((object tag-index))
23
-  (rel-path (staging-dir *config*) "tag/~a" (index-id object)))
24
-(defmethod page-path ((object date-index))
25
-  (rel-path (staging-dir *config*) "date/~a" (index-id object)))
26
-(defmethod page-path ((object int-index))
27
-  (rel-path (staging-dir *config*) "~d" (index-id object)))
20
+(defmethod page-url ((object index))
21
+  (index-id object))
22
+(defmethod page-url ((object tag-index))
23
+  (format nil "tag/~a" (index-id object)))
24
+(defmethod page-url ((object date-index))
25
+  (format nil "date/~a" (index-id object)))
26
+(defmethod page-url ((object int-index))
27
+  (format nil "~d" (index-id object)))
28 28
 
29 29
 (defun all-months ()
30 30
   "Retrieve a list of all months with published content."

+ 2 - 2
src/posts.lisp

@@ -10,8 +10,8 @@
10 10
                                   :prev prev
11 11
                                   :next next)))
12 12
 
13
-(defmethod page-path ((object post))
14
-  (rel-path (staging-dir *config*) "posts/~a" (content-slug object)))
13
+(defmethod page-url ((object post))
14
+  (format nil "posts/~a" (content-slug object)))
15 15
 
16 16
 (defmethod initialize-instance :after ((object post) &key)
17 17
   (with-accessors ((title post-title)

+ 13 - 0
themes/sitemap.tmpl

@@ -0,0 +1,13 @@
1
+{namespace coleslaw.theme.feeds}
2
+
3
+{template sitemap}
4
+<?xml version="1.0"?>{\n}
5
+<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>
6
+    {foreach $url in $urls}
7
+    <url>
8
+        <loc>{$domain}/{$url}</loc>
9
+        <lastmod>{$pubdate}</lastmod>
10
+    </url>
11
+    {/foreach}
12
+</urlset>
13
+{/template}