Mercurial > piecrust2
comparison piecrust/sources/posts.py @ 577:ff404adfcf45
sources: Add method to get a page factory from a path.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 30 Dec 2015 14:45:57 -0800 |
parents | 0c74a6c4533d |
children | 58ebf50235a5 |
comparison
equal
deleted
inserted
replaced
576:0c74a6c4533d | 577:ff404adfcf45 |
---|---|
8 PageSource, InvalidFileSystemEndpointError, PageFactory, | 8 PageSource, InvalidFileSystemEndpointError, PageFactory, |
9 MODE_CREATING, MODE_PARSING) | 9 MODE_CREATING, MODE_PARSING) |
10 from piecrust.sources.interfaces import ( | 10 from piecrust.sources.interfaces import ( |
11 IPreparingSource, IInteractiveSource, InteractiveField) | 11 IPreparingSource, IInteractiveSource, InteractiveField) |
12 from piecrust.sources.mixins import SimplePaginationSourceMixin | 12 from piecrust.sources.mixins import SimplePaginationSourceMixin |
13 from piecrust.uriutil import multi_replace | |
13 | 14 |
14 | 15 |
15 logger = logging.getLogger(__name__) | 16 logger = logging.getLogger(__name__) |
16 | 17 |
17 | 18 |
32 | 33 |
33 def resolveRef(self, ref_path): | 34 def resolveRef(self, ref_path): |
34 path = os.path.normpath(os.path.join(self.fs_endpoint_path, ref_path)) | 35 path = os.path.normpath(os.path.join(self.fs_endpoint_path, ref_path)) |
35 metadata = self._parseMetadataFromPath(ref_path) | 36 metadata = self._parseMetadataFromPath(ref_path) |
36 return path, metadata | 37 return path, metadata |
38 | |
39 def buildPageFactory(self, path): | |
40 if not path.startswith(self.fs_endpoint_path): | |
41 raise Exception("Page path '%s' isn't inside '%s'." % ( | |
42 path, self.fs_endpoint_path)) | |
43 rel_path = path[len(self.fs_endpoint_path):].lstrip('\\/') | |
44 pat = self.PATH_FORMAT % { | |
45 'year': 'YEAR', | |
46 'month': 'MONTH', | |
47 'day': 'DAY', | |
48 'slug': 'SLUG', | |
49 'ext': 'EXT'} | |
50 pat = re.escape(pat) | |
51 pat = multi_replace(pat, { | |
52 'YEAR': '(\d{4})', | |
53 'MONTH': '(\d{2})', | |
54 'DAY': '(\d{2})', | |
55 'SLUG': '(.*)', | |
56 'EXT': '(.*)'}) | |
57 m = re.match(pat, rel_path) | |
58 if m is None: | |
59 raise Exception("'%s' isn't a proper %s page path." % ( | |
60 rel_path, self.SOURCE_NAME)) | |
61 return self._makeFactory( | |
62 rel_path, | |
63 m.group(4), | |
64 int(m.group(1)), | |
65 int(m.group(2)), | |
66 int(m.group(3))) | |
67 | |
37 | 68 |
38 def findPageFactory(self, metadata, mode): | 69 def findPageFactory(self, metadata, mode): |
39 year = metadata.get('year') | 70 year = metadata.get('year') |
40 month = metadata.get('month') | 71 month = metadata.get('month') |
41 day = metadata.get('day') | 72 day = metadata.get('day') |