changeset 170:c3831a762bc2

sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source. The `SimplePageSource` now calls a `_populateMetadata` function that subclasses can override to add/edit their custom metadata everwhere it would be returned to the system.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 03 Jan 2015 20:49:00 -0800
parents f3aa511eef99
children d5991525801d
files piecrust/sources/base.py piecrust/sources/prose.py
diffstat 2 files changed, 27 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/sources/base.py	Sat Jan 03 18:06:52 2015 -0800
+++ b/piecrust/sources/base.py	Sat Jan 03 20:49:00 2015 -0800
@@ -317,19 +317,13 @@
             rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path)
             dirnames[:] = list(filter(self._filterPageDirname, dirnames))
             for f in filter(self._filterPageFilename, filenames):
-                slug, ext = os.path.splitext(os.path.join(rel_dirpath, f))
-                slug = slug.replace('\\', '/')
-                if ext.lstrip('.') not in self.supported_extensions:
-                    slug += ext
-                if slug.startswith('./') or slug.startswith('.\\'):
-                    slug = slug[2:]
-                if slug == '_index':
-                    slug = ''
-                metadata = {'path': slug}
                 fac_path = f
                 if rel_dirpath != '.':
                     fac_path = os.path.join(rel_dirpath, f)
+                slug = self._makeSlug(fac_path)
+                metadata = {'path': slug}
                 fac_path = fac_path.replace('\\', '/')
+                self._populateMetadata(fac_path, metadata)
                 yield PageFactory(self, fac_path, metadata)
 
     def resolveRef(self, ref_path):
@@ -337,8 +331,8 @@
                 os.path.join(self.fs_endpoint_path, ref_path))
 
     def findPagePath(self, metadata, mode):
-        uri_path = metadata['path']
-        if uri_path == '':
+        uri_path = metadata.setdefault('path', '')
+        if not uri_path:
             uri_path = '_index'
         path = os.path.normpath(os.path.join(self.fs_endpoint_path, uri_path))
         _, ext = os.path.splitext(path)
@@ -348,10 +342,12 @@
                 path = '%s.%s' % (path, self.default_auto_format)
             rel_path = os.path.relpath(path, self.fs_endpoint_path)
             rel_path = rel_path.replace('\\', '/')
+            self._populateMetadata(rel_path, metadata)
             return rel_path, metadata
 
         if ext == '':
-            paths_to_check = ['%s.%s' % (path, e)
+            paths_to_check = [
+                    '%s.%s' % (path, e)
                     for e in self.supported_extensions]
         else:
             paths_to_check = [path]
@@ -359,19 +355,32 @@
             if os.path.isfile(path):
                 rel_path = os.path.relpath(path, self.fs_endpoint_path)
                 rel_path = rel_path.replace('\\', '/')
+                self._populateMetadata(rel_path, metadata)
                 return rel_path, metadata
 
         return None, None
 
+    def _makeSlug(self, rel_path):
+        slug, ext = os.path.splitext(rel_path)
+        slug = slug.replace('\\', '/')
+        if ext.lstrip('.') not in self.supported_extensions:
+            slug += ext
+        if slug.startswith('./') or slug.startswith('.\\'):
+            slug = slug[2:]
+        if slug == '_index':
+            slug = ''
+        return slug
+
     def _filterPageDirname(self, d):
         return not d.endswith('-assets')
 
     def _filterPageFilename(self, f):
-        name, ext = os.path.splitext(f)
         return (f[0] != '.' and   # .DS_store and other crap
                 f[-1] != '~' and  # Vim temp files and what-not
-                f not in ['Thumbs.db']) # Windows bullshit
+                f not in ['Thumbs.db'])  # Windows bullshit
 
+    def _populateMetadata(self, rel_path, metadata):
+        pass
 
 class DefaultPageSource(SimplePageSource, IPreparingSource,
         SimplePaginationSourceMixin):
--- a/piecrust/sources/prose.py	Sat Jan 03 18:06:52 2015 -0800
+++ b/piecrust/sources/prose.py	Sat Jan 03 20:49:00 2015 -0800
@@ -16,22 +16,13 @@
         super(ProseSource, self).__init__(app, name, config)
         self.config_recipe = config.get('config', {})
 
-    def buildPageFactories(self):
-        factories = super(ProseSource, self).buildPageFactories()
-        for f in factories:
-            f.metadata['config'] = self._makeConfig(f.path)
-            logger.debug(f.__dict__)
-            yield f
+    def _populateMetadata(self, rel_path, metadata):
+        metadata['config'] = self._makeConfig(rel_path)
 
-    def findPagePath(self, metadata, mode):
-        rel_path, metadata = super(ProseSource, self).findPagePath(metadata, mode)
-        if rel_path:
-            metadata['config'] = self._makeConfig(self.resolveRef(rel_path))
-        return rel_path, metadata
-
-    def _makeConfig(self, path):
+    def _makeConfig(self, rel_path):
         c = dict(self.config_recipe)
         if c.get('title') == '%first_line%':
+            path = os.path.join(self.fs_endpoint_path, rel_path)
             c['title'] = get_first_line(path)
         return c