# HG changeset patch # User Ludovic Chabant # Date 1451515557 28800 # Node ID ff404adfcf4536f79744cdf5c7763a012662c4b4 # Parent 0c74a6c4533d87e9246cad7a00b47763c2322d44 sources: Add method to get a page factory from a path. diff -r 0c74a6c4533d -r ff404adfcf45 piecrust/sources/base.py --- a/piecrust/sources/base.py Sat Dec 19 18:07:21 2015 -0800 +++ b/piecrust/sources/base.py Wed Dec 30 14:45:57 2015 -0800 @@ -109,6 +109,9 @@ def buildPageFactories(self): raise NotImplementedError() + def buildPageFactory(self, path): + raise NotImplementedError() + def resolveRef(self, ref_path): """ Returns the full path and source metadata given a source (relative) path, like a ref-spec. diff -r 0c74a6c4533d -r ff404adfcf45 piecrust/sources/default.py --- a/piecrust/sources/default.py Sat Dec 19 18:07:21 2015 -0800 +++ b/piecrust/sources/default.py Wed Dec 30 14:45:57 2015 -0800 @@ -58,6 +58,17 @@ self._populateMetadata(fac_path, metadata) yield PageFactory(self, fac_path, metadata) + def buildPageFactory(self, path): + if not path.startswith(self.fs_endpoint_path): + raise Exception("Page path '%s' isn't inside '%s'." % ( + path, self.fs_enpoint_path)) + rel_path = path[len(self.fs_endpoint_path):].lstrip('\\/') + slug = self._makeSlug(rel_path) + metadata = {'slug': slug} + fac_path = rel_path.replace('\\', '/') + self._populateMetadata(fac_path, metadata) + return PageFactory(self, fac_path, metadata) + def resolveRef(self, ref_path): path = os.path.normpath( os.path.join(self.fs_endpoint_path, ref_path.lstrip("\\/"))) diff -r 0c74a6c4533d -r ff404adfcf45 piecrust/sources/posts.py --- a/piecrust/sources/posts.py Sat Dec 19 18:07:21 2015 -0800 +++ b/piecrust/sources/posts.py Wed Dec 30 14:45:57 2015 -0800 @@ -10,6 +10,7 @@ from piecrust.sources.interfaces import ( IPreparingSource, IInteractiveSource, InteractiveField) from piecrust.sources.mixins import SimplePaginationSourceMixin +from piecrust.uriutil import multi_replace logger = logging.getLogger(__name__) @@ -35,6 +36,36 @@ metadata = self._parseMetadataFromPath(ref_path) return path, metadata + def buildPageFactory(self, path): + if not path.startswith(self.fs_endpoint_path): + raise Exception("Page path '%s' isn't inside '%s'." % ( + path, self.fs_endpoint_path)) + rel_path = path[len(self.fs_endpoint_path):].lstrip('\\/') + pat = self.PATH_FORMAT % { + 'year': 'YEAR', + 'month': 'MONTH', + 'day': 'DAY', + 'slug': 'SLUG', + 'ext': 'EXT'} + pat = re.escape(pat) + pat = multi_replace(pat, { + 'YEAR': '(\d{4})', + 'MONTH': '(\d{2})', + 'DAY': '(\d{2})', + 'SLUG': '(.*)', + 'EXT': '(.*)'}) + m = re.match(pat, rel_path) + if m is None: + raise Exception("'%s' isn't a proper %s page path." % ( + rel_path, self.SOURCE_NAME)) + return self._makeFactory( + rel_path, + m.group(4), + int(m.group(1)), + int(m.group(2)), + int(m.group(3))) + + def findPageFactory(self, metadata, mode): year = metadata.get('year') month = metadata.get('month')