diff piecrust/sources/autoconfig.py @ 363:dd25bd3ce1f9

serve: Refactoring and fixes to be able to serve taxonomy pages. * Page sources' `findPagePath` is renamed to `findPageFactory`, so that it also returns source metadata. * Page refs now store possible hits more properly, and use the previous point to also store metadata. As a result, they can also return a proper factory. * Implement `findPageFactory` correctly in all built-in sources. * When the Chef server matches a taxonomy page, get the source metadata from the page ref in order to make a more proper page. * Make the `getRoute(s)` functions explicitely say if they want taxonomy routes or not.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 26 Apr 2015 15:07:40 -0700
parents 95874699ec2e
children 883a5544cd7f
line wrap: on
line diff
--- a/piecrust/sources/autoconfig.py	Sun Apr 26 08:34:27 2015 -0700
+++ b/piecrust/sources/autoconfig.py	Sun Apr 26 15:07:40 2015 -0700
@@ -67,9 +67,21 @@
                 yield PageFactory(self, fac_path, metadata)
 
     def resolveRef(self, ref_path):
-        return os.path.normpath(
+        path = os.path.normpath(
                 os.path.join(self.fs_endpoint_path, ref_path.lstrip("\\/")))
 
+        config = None
+        if self.capture_mode == 'dirname':
+            config = self._extractConfigFragment(os.path.dirname(ref_path))
+        elif self.capture_mode == 'path':
+            config = self._extractConfigFragment(ref_path)
+        elif self.capture_mode == 'filename':
+            config = self._extractConfigFragment(os.path.basename(ref_path))
+
+        slug = self._makeSlug(ref_path)
+        metadata = {'slug': slug, 'config': config}
+        return path, metadata
+
     def listPath(self, rel_path):
         raise NotImplementedError()
 
@@ -140,7 +152,7 @@
 
         return {self.setting_name: values}
 
-    def findPagePath(self, metadata, mode):
+    def findPageFactory(self, metadata, mode):
         # Pages from this source are effectively flattened, so we need to
         # find pages using a brute-force kinda way.
         for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path):
@@ -151,7 +163,8 @@
                     rel_path = os.path.relpath(path, self.fs_endpoint_path)
                     config = self._extractConfigFragment(rel_path)
                     metadata = {'slug': slug, 'config': config}
-                    return rel_path, metadata
+                    return PageFactory(self, rel_path, metadata)
+        return None
 
     def listPath(self, rel_path):
         rel_path = rel_path.lstrip('\\/')
@@ -196,7 +209,7 @@
         self.supported_extensions = list(
                 app.config.get('site/auto_formats').keys())
 
-    def findPagePath(self, metadata, mode):
+    def findPageFactory(self, metadata, mode):
         uri_path = metadata.get('slug', '')
         if uri_path == '':
             uri_path = '_index'
@@ -221,7 +234,7 @@
                         found = True
                         break
                 if not found:
-                    return None, None
+                    return None
             else:
                 # Find each sub-directory. It can either be a directory with
                 # the name itself, or the name with a number prefix.
@@ -233,13 +246,13 @@
                         found = True
                         break
                 if not found:
-                    return None, None
+                    return None
 
         fac_path = os.path.relpath(path, self.fs_endpoint_path)
         config = self._extractConfigFragment(fac_path)
         metadata = {'slug': uri_path, 'config': config}
 
-        return fac_path, metadata
+        return PageFactory(self, fac_path, metadata)
 
     def getSorterIterator(self, it):
         accessor = self.getSettingAccessor()