# HG changeset patch # User Ludovic Chabant # Date 1408571723 25200 # Node ID bdb103c571680cad0bbb71384fb2f5c0fa775842 # Parent 62c7a97c83403c98a6b2059650d8e76d8f96304c Add `sitemap` processor. diff -r 62c7a97c8340 -r bdb103c57168 piecrust/plugins/builtin.py --- a/piecrust/plugins/builtin.py Tue Aug 19 15:36:28 2014 -0700 +++ b/piecrust/plugins/builtin.py Wed Aug 20 14:55:23 2014 -0700 @@ -9,6 +9,7 @@ from piecrust.plugins.base import PieCrustPlugin from piecrust.processing.base import CopyFileProcessor from piecrust.processing.less import LessProcessor +from piecrust.processing.sitemap import SitemapProcessor from piecrust.sources.base import DefaultPageSource from piecrust.sources.posts import (FlatPostsSource, ShallowPostsSource, HierarchyPostsSource) @@ -60,5 +61,6 @@ def getProcessors(self): return [ CopyFileProcessor(), - LessProcessor()] + LessProcessor(), + SitemapProcessor()] diff -r 62c7a97c8340 -r bdb103c57168 piecrust/processing/sitemap.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/piecrust/processing/sitemap.py Wed Aug 20 14:55:23 2014 -0700 @@ -0,0 +1,96 @@ +import time +import logging +import yaml +from piecrust.processing.base import SimpleFileProcessor + + +logger = logging.getLogger(__name__) + + +SITEMAP_HEADER = \ +""" + +""" +SITEMAP_FOOTER = "\n" + +SITEURL_HEADER = " \n" +SITEURL_LOC = " %s\n" +SITEURL_LASTMOD = " %s\n" +SITEURL_CHANGEFREQ = " %s\n" +SITEURL_PRIORITY = " %f\n" +SITEURL_FOOTER = " \n" + + +class SitemapProcessor(SimpleFileProcessor): + PROCESSOR_NAME = 'sitemap' + + def __init__(self): + super(SitemapProcessor, self).__init__({'sitemap': 'xml'}) + self._start_time = None + + def onPipelineStart(self, pipeline): + self._start_time = time.time() + + def _doProcess(self, in_path, out_path): + with open(in_path, 'r') as fp: + sitemap = yaml.load(fp) + + with open(out_path, 'w') as fp: + fp.write(SITEMAP_HEADER) + self._writeManualLocs(sitemap, fp) + self._writeAutoLocs(sitemap, fp) + fp.write(SITEMAP_FOOTER) + + return True + + def _writeManualLocs(self, sitemap, fp): + locs = sitemap.setdefault('locations', None) + if not locs: + return + + logger.debug("Generating manual sitemap entries.") + for loc in locs: + self._writeEntry(loc, fp) + + def _writeAutoLocs(self, sitemap, fp): + source_names = sitemap.setdefault('autogen', None) + if not source_names: + return + + for name in source_names: + logger.debug("Generating automatic sitemap entries for '%s'." % + name) + source = self.app.getSource(name) + if source is None: + raise Exception("No such source: %s" % name) + + for fac in source.getPageFactories(): + route = self.app.getRoute(source.name, fac.metadata) + uri = route.getUri(fac.metadata) + + page = fac.buildPage() + t = page.datetime.timestamp() + sm_cfg = page.config.get('sitemap') + + args = {'url': uri, 'lastmod': strftime_iso8601(t)} + if sm_cfg: + args.update(cm_cfg) + + self._writeEntry(args, fp) + + def _writeEntry(self, args, fp): + fp.write(SITEURL_HEADER) + fp.write(SITEURL_LOC % args['url']) + if 'lastmod' in args: + fp.write(SITEURL_LASTMOD % args['lastmod']) + if 'changefreq' in args: + fp.write(SITEURL_CHANGEFREQ % args['changefreq']) + if 'priority' in args: + fp.write(SITEURL_PRIORITY % args['priority']) + fp.write(SITEURL_FOOTER) + + +def strftime_iso8601(t): + return time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(t)) +