changeset 437:62274d805a6e

bake: Tweaks to the `sitemap` processor. Add tests. * Now using a `PageIterator` to get the expected behaviour from a given page source, such as skipping taxonomy pages and such. * Fix formatting of priority. * Use the bake time for `lastmod` attribute.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 27 Jun 2015 21:50:36 -0700
parents 2aa879d63133
children cff70eeb1cc1
files piecrust/processing/sitemap.py tests/procs/test_sitemap.yaml
diffstat 2 files changed, 49 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/processing/sitemap.py	Sat Jun 27 21:48:26 2015 -0700
+++ b/piecrust/processing/sitemap.py	Sat Jun 27 21:50:36 2015 -0700
@@ -1,6 +1,7 @@
 import time
 import logging
 import yaml
+from piecrust.data.iterators import PageIterator
 from piecrust.processing.base import SimpleFileProcessor
 from piecrust.routing import create_route_metadata
 
@@ -10,8 +11,7 @@
 
 SITEMAP_HEADER = \
 """<?xml version="1.0" encoding="utf-8"?>
-<urlset
-  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 """
 SITEMAP_FOOTER = "</urlset>\n"
 
@@ -19,7 +19,7 @@
 SITEURL_LOC =        "    <loc>%s</loc>\n"
 SITEURL_LASTMOD =    "    <lastmod>%s</lastmod>\n"
 SITEURL_CHANGEFREQ = "    <changefreq>%s</changefreq>\n"
-SITEURL_PRIORITY =   "    <priority>%f</priority>\n"
+SITEURL_PRIORITY =   "    <priority>%0.1f</priority>\n"
 SITEURL_FOOTER =     "  </url>\n"
 
 
@@ -59,6 +59,7 @@
         if not source_names:
             return
 
+        cur_time = strftime_iso8601(time.time())
         for name in source_names:
             logger.debug("Generating automatic sitemap entries for '%s'." %
                          name)
@@ -66,15 +67,12 @@
             if source is None:
                 raise Exception("No such source: %s" % name)
 
-            for page in source.getPages():
-                route_metadata = create_route_metadata(page)
-                route = self.app.getRoute(source.name, route_metadata)
-                uri = route.getUri(route_metadata)
+            it = PageIterator(source)
+            for page in it:
+                uri = page['url']
+                sm_cfg = page.get('sitemap')
 
-                t = page.datetime.timestamp()
-                sm_cfg = page.config.get('sitemap')
-
-                args = {'url': uri, 'lastmod': strftime_iso8601(t)}
+                args = {'url': uri, 'lastmod': cur_time}
                 if sm_cfg:
                     args.update(sm_cfg)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/procs/test_sitemap.yaml	Sat Jun 27 21:50:36 2015 -0700
@@ -0,0 +1,40 @@
+---
+in:
+    assets/sitemap.sitemap: |
+        autogen: [pages, theme_pages]
+    pages/foo.md: This is a foo
+outfiles:
+    sitemap.xml: |
+        <?xml version="1.0" encoding="utf-8"?>
+        <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+          <url>
+            <loc>/foo.html</loc>
+            <lastmod>%test_time_iso8601%</lastmod>
+          </url>
+          <url>
+            <loc>/</loc>
+            <lastmod>%test_time_iso8601%</lastmod>
+          </url>
+        </urlset>
+---
+in:
+    assets/sitemap.sitemap: |
+        autogen: [pages]
+    pages/foo.md: |
+        ---
+        sitemap:
+            changefreq: monthly
+            priority: 0.8
+        ---
+        This is a foo
+outfiles:
+    sitemap.xml: |
+        <?xml version="1.0" encoding="utf-8"?>
+        <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+          <url>
+            <loc>/foo.html</loc>
+            <lastmod>%test_time_iso8601%</lastmod>
+            <changefreq>monthly</changefreq>
+            <priority>0.8</priority>
+          </url>
+        </urlset>