Mercurial > piecrust2
annotate piecrust/sources/posts.py @ 852:4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
* Everything is a `ContentSource`, including assets directories.
* Most content sources are subclasses of the base file-system source.
* A source is processed by a "pipeline", and there are 2 built-in pipelines,
one for assets and one for pages. The asset pipeline is vaguely functional,
but the page pipeline is completely broken right now.
* Rewrite the baking process as just running appropriate pipelines on each
content item. This should allow for better parallelization.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 17 May 2017 00:11:48 -0700 |
parents | f0930178fd01 |
children | f070a4fc033c |
rev | line source |
---|---|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os.path |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import re |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import logging |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import datetime |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
6 from piecrust import osutil |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
577
diff
changeset
|
7 from piecrust.routing import RouteParameter |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
8 from piecrust.sources.base import REL_ASSETS, ContentItem |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
9 from piecrust.sources.fs import ( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
10 FSContentSource, InvalidFileSystemEndpointError) |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
11 from piecrust.sources.interfaces import ( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
12 IPreparingSource, IInteractiveSource, InteractiveField) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
13 from piecrust.sources.mixins import ( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
14 SimplePaginationSourceMixin, SimpleAssetsSubDirMixin) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
15 from piecrust.uriutil import uri_to_title |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
21 class PostsSource(FSContentSource, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
22 SimpleAssetsSubDirMixin, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
23 IPreparingSource, IInteractiveSource): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 PATH_FORMAT = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 def __init__(self, app, name, config): |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
27 FSContentSource.__init__(self, app, name, config) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
28 self.auto_formats = app.config.get('site/auto_formats') |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
29 self.default_auto_format = app.config.get('site/default_auto_format') |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
30 self.supported_extensions = list(self.auto_formats) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 @property |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 def path_format(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 return self.__class__.PATH_FORMAT |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
36 def _finalizeContent(self, parent_group, items, groups): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
37 SimpleAssetsSubDirMixin._onFinalizeContent( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
38 parent_group, items, groups) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
577
diff
changeset
|
39 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
40 def getRelatedContents(self, item, relationship): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
41 if relationship == REL_ASSETS: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
42 SimpleAssetsSubDirMixin._getRelatedAssetsContents(item) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
43 raise NotImplementedError() |
577
ff404adfcf45
sources: Add method to get a page factory from a path.
Ludovic Chabant <ludovic@chabant.com>
parents:
576
diff
changeset
|
44 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
45 def findContent(self, route_params): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
46 year = route_params.get('year') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
47 month = route_params.get('month') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
48 day = route_params.get('day') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
49 slug = route_params.get('slug') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
281
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
51 try: |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
52 if year is not None: |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
53 year = int(year) |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
54 if month is not None: |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
55 month = int(month) |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
56 if day is not None: |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
57 day = int(day) |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
58 except ValueError: |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
281
diff
changeset
|
59 return None |
54
a46354306738
Use properly formatted date components for the blog sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
60 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
61 ext = route_params.get('ext') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 if ext is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 if len(self.supported_extensions) == 1: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 ext = self.supported_extensions[0] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 replacements = { |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
67 'year': '%04d' % year if year is not None else None, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
68 'month': '%02d' % month if month is not None else None, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
69 'day': '%02d' % day if day is not None else None, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
70 'slug': slug, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
71 'ext': ext |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
72 } |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 needs_recapture = False |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 if year is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 replacements['year'] = '????' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 if month is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 replacements['month'] = '??' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 if day is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 replacements['day'] = '??' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 if slug is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 replacements['slug'] = '*' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 if ext is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 replacements['ext'] = '*' |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
89 path = os.path.normpath(os.path.join( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
90 self.fs_endpoint_path, self.path_format % replacements)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 if needs_recapture: |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
93 possible_paths = osutil.glob(path) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 if len(possible_paths) != 1: |
516
73bd408caebc
internal: Return `None` instead of raising an exception when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
363
diff
changeset
|
95 return None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 path = possible_paths[0] |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
97 elif not os.path.isfile(path): |
516
73bd408caebc
internal: Return `None` instead of raising an exception when finding pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
363
diff
changeset
|
98 return None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
100 metadata = self._parseMetadataFromPath(path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
101 return ContentItem(path, metadata) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
102 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
103 def _parseMetadataFromPath(self, path): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
104 regex_repl = { |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
105 'year': '(?P<year>\d{4})', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
106 'month': '(?P<month>\d{2})', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
107 'day': '(?P<day>\d{2})', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
108 'slug': '(?P<slug>.*)', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
109 'ext': '(?P<ext>.*)' |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
110 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
111 path_format_re = re.sub(r'([\-\.])', r'\\\1', self.path_format) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
112 pattern = path_format_re % regex_repl + '$' |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
113 m = re.search(pattern, path.replace('\\', '/')) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
114 if not m: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
115 raise Exception("Expected to be able to match path with path " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
116 "format: %s" % path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
117 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
118 year = int(m.group('year')) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
119 month = int(m.group('month')) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
120 day = int(m.group('day')) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
121 timestamp = datetime.date(year, month, day) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
122 metadata = { |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
123 'year': year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
124 'month': month, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
125 'day': day, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
126 'slug': m.group('slug'), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
127 'date': timestamp |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
128 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
129 return metadata |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
130 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
131 def getSupportedRouteParameters(self): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
132 return [ |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
133 RouteParameter('slug', RouteParameter.TYPE_STRING), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
134 RouteParameter('day', RouteParameter.TYPE_INT2), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
135 RouteParameter('month', RouteParameter.TYPE_INT2), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
136 RouteParameter('year', RouteParameter.TYPE_INT4)] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 |
841
f0930178fd01
internal: Make `posts` sources cache their list of pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
138 def getSourceIterator(self): |
f0930178fd01
internal: Make `posts` sources cache their list of pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
139 if self._source_it_cache is None: |
f0930178fd01
internal: Make `posts` sources cache their list of pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
140 it = SimplePaginationSourceMixin.getSourceIterator(self) |
f0930178fd01
internal: Make `posts` sources cache their list of pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
141 self._source_it_cache = list(it) |
f0930178fd01
internal: Make `posts` sources cache their list of pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
142 return self._source_it_cache |
f0930178fd01
internal: Make `posts` sources cache their list of pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
143 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 def setupPrepareParser(self, parser, app): |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
145 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
146 '-d', '--date', help="The date of the post, " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
147 "in `year/month/day` format (defaults to today).") |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 parser.add_argument('slug', help="The URL slug for the new post.") |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
150 def createContent(self, args): |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
151 dt = datetime.date.today() |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
152 if args.date: |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
153 if args.date == 'today': |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
154 pass # Keep the default we had. |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
155 elif args.date == 'tomorrow': |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
156 dt += datetime.timedelta(days=1) |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
157 elif args.date.startswith('+'): |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
158 try: |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
159 dt += datetime.timedelta(days=int(args.date.lstrip('+'))) |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
160 except ValueError: |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
161 raise Exception("Date offsets must be numbers.") |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
162 else: |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
163 try: |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
164 year, month, day = [int(s) for s in args.date.split('/')] |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
165 except ValueError: |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
166 raise Exception("Dates must be of the form: " |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
167 "YEAR/MONTH/DAY.") |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
168 dt = datetime.date(year, month, day) |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
169 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
170 slug, ext = os.path.splitext(args.slug) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
171 if not ext: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
172 ext = self.default_auto_format |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
173 year, month, day = dt.year, dt.month, dt.day |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
174 tokens = { |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
175 'slug': args.slug, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
176 'ext': ext, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
177 'year': '%04d' % year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
178 'month': '%02d' % month, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
179 'day': '%02d' % day |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
180 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
181 rel_path = self.path_format % tokens |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
182 path = os.path.join(self.fs_endpoint_path, rel_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
183 metadata = { |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
184 'config': {'title': uri_to_title(slug)} |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
185 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
186 return ContentItem(path, metadata) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
187 |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
188 def getInteractiveFields(self): |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
189 dt = datetime.date.today() |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
190 return [ |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
191 InteractiveField('year', InteractiveField.TYPE_INT, dt.year), |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
192 InteractiveField('month', InteractiveField.TYPE_INT, dt.month), |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
193 InteractiveField('day', InteractiveField.TYPE_INT, dt.day), |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
194 InteractiveField('slug', InteractiveField.TYPE_STRING, 'new-post')] |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
195 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
196 def _checkFsEndpointPath(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
197 if not os.path.isdir(self.fs_endpoint_path): |
84
b3ce11b2cf36
Don't complain about missing `pages` or `posts` directories by default.
Ludovic Chabant <ludovic@chabant.com>
parents:
80
diff
changeset
|
198 if self.ignore_missing_dir: |
b3ce11b2cf36
Don't complain about missing `pages` or `posts` directories by default.
Ludovic Chabant <ludovic@chabant.com>
parents:
80
diff
changeset
|
199 return False |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
200 raise InvalidFileSystemEndpointError(self.name, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
201 self.fs_endpoint_path) |
84
b3ce11b2cf36
Don't complain about missing `pages` or `posts` directories by default.
Ludovic Chabant <ludovic@chabant.com>
parents:
80
diff
changeset
|
202 return True |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
203 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
204 def _makeContentItem(self, path, slug, year, month, day): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
205 path = path.replace('\\', '/') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
206 timestamp = datetime.date(year, month, day) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
207 metadata = { |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
208 'slug': slug, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
209 'year': year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
210 'month': month, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
211 'day': day, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
212 'date': timestamp} |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
213 return ContentItem(path, metadata) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
214 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
215 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
216 class FlatPostsSource(PostsSource): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
217 SOURCE_NAME = 'posts/flat' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
218 PATH_FORMAT = '%(year)s-%(month)s-%(day)s_%(slug)s.%(ext)s' |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
219 PATTERN = re.compile(r'(\d{4})-(\d{2})-(\d{2})_(.*)\.(\w+)$') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
220 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
221 def __init__(self, app, name, config): |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
222 super().__init__(app, name, config) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
223 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
224 def getContents(self, group): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
225 if not self._checkFSEndpoint(): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
226 return None |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
227 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
228 logger.debug("Scanning for posts (flat) in: %s" % |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
229 self.fs_endpoint_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
230 pattern = FlatPostsSource.PATTERN |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
231 _, __, filenames = next(osutil.walk(self.fs_endpoint_path)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
232 for f in filenames: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
233 match = pattern.match(f) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
234 if match is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
235 name, ext = os.path.splitext(f) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
236 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
237 "'%s' is not formatted as 'YYYY-MM-DD_slug-title.%s' " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
238 "and will be ignored. Is that a typo?" % (f, ext)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
239 continue |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
240 yield self._makeContentItem( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
241 f, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
242 match.group(4), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
243 int(match.group(1)), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
244 int(match.group(2)), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
245 int(match.group(3))) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
246 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
247 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
248 class ShallowPostsSource(PostsSource): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
249 SOURCE_NAME = 'posts/shallow' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
250 PATH_FORMAT = '%(year)s/%(month)s-%(day)s_%(slug)s.%(ext)s' |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
251 YEAR_PATTERN = re.compile(r'(\d{4})$') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
252 FILE_PATTERN = re.compile(r'(\d{2})-(\d{2})_(.*)\.(\w+)$') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
253 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
254 def __init__(self, app, name, config): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
255 super(ShallowPostsSource, self).__init__(app, name, config) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
256 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
257 def getContents(self, group): |
84
b3ce11b2cf36
Don't complain about missing `pages` or `posts` directories by default.
Ludovic Chabant <ludovic@chabant.com>
parents:
80
diff
changeset
|
258 if not self._checkFsEndpointPath(): |
b3ce11b2cf36
Don't complain about missing `pages` or `posts` directories by default.
Ludovic Chabant <ludovic@chabant.com>
parents:
80
diff
changeset
|
259 return |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
260 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
261 logger.debug("Scanning for posts (shallow) in: %s" % |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
262 self.fs_endpoint_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
263 year_pattern = ShallowPostsSource.YEAR_PATTERN |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
264 file_pattern = ShallowPostsSource.FILE_PATTERN |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
265 _, year_dirs, __ = next(osutil.walk(self.fs_endpoint_path)) |
5 | 266 year_dirs = [d for d in year_dirs if year_pattern.match(d)] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
267 for yd in year_dirs: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
268 if year_pattern.match(yd) is None: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
269 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
270 "'%s' is not formatted as 'YYYY' and will be ignored. " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
271 "Is that a typo?") |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
272 continue |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
273 year = int(yd) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
274 year_dir = os.path.join(self.fs_endpoint_path, yd) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
275 |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
276 _, __, filenames = next(osutil.walk(year_dir)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
277 for f in filenames: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
278 match = file_pattern.match(f) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
279 if match is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
280 name, ext = os.path.splitext(f) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
281 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
282 "'%s' is not formatted as 'MM-DD_slug-title.%s' " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
283 "and will be ignored. Is that a typo?" % (f, ext)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
284 continue |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
285 yield self._makeContentItem( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
286 os.path.join(yd, f), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
287 match.group(3), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
288 year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
289 int(match.group(1)), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
290 int(match.group(2))) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
291 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
292 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
293 class HierarchyPostsSource(PostsSource): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
294 SOURCE_NAME = 'posts/hierarchy' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
295 PATH_FORMAT = '%(year)s/%(month)s/%(day)s_%(slug)s.%(ext)s' |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
296 YEAR_PATTERN = re.compile(r'(\d{4})$') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
297 MONTH_PATTERN = re.compile(r'(\d{2})$') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
298 FILE_PATTERN = re.compile(r'(\d{2})_(.*)\.(\w+)$') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
299 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
300 def __init__(self, app, name, config): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
301 super(HierarchyPostsSource, self).__init__(app, name, config) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
302 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
303 def getContents(self, group): |
84
b3ce11b2cf36
Don't complain about missing `pages` or `posts` directories by default.
Ludovic Chabant <ludovic@chabant.com>
parents:
80
diff
changeset
|
304 if not self._checkFsEndpointPath(): |
b3ce11b2cf36
Don't complain about missing `pages` or `posts` directories by default.
Ludovic Chabant <ludovic@chabant.com>
parents:
80
diff
changeset
|
305 return |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
306 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
307 logger.debug("Scanning for posts (hierarchy) in: %s" % |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
308 self.fs_endpoint_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
309 year_pattern = HierarchyPostsSource.YEAR_PATTERN |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
310 month_pattern = HierarchyPostsSource.MONTH_PATTERN |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
311 file_pattern = HierarchyPostsSource.FILE_PATTERN |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
312 _, year_dirs, __ = next(osutil.walk(self.fs_endpoint_path)) |
5 | 313 year_dirs = [d for d in year_dirs if year_pattern.match(d)] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
314 for yd in year_dirs: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
315 year = int(yd) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
316 year_dir = os.path.join(self.fs_endpoint_path, yd) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
317 |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
318 _, month_dirs, __ = next(osutil.walk(year_dir)) |
5 | 319 month_dirs = [d for d in month_dirs if month_pattern.match(d)] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
320 for md in month_dirs: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
321 month = int(md) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
322 month_dir = os.path.join(year_dir, md) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
323 |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
324 _, __, filenames = next(osutil.walk(month_dir)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
325 for f in filenames: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
326 match = file_pattern.match(f) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
327 if match is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
328 name, ext = os.path.splitext(f) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
329 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
330 "'%s' is not formatted as 'DD_slug-title.%s' " |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
331 "and will be ignored. Is that a typo?" % (f, ext)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
332 continue |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
333 rel_name = os.path.join(yd, md, f) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
334 yield self._makeContentItem( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
335 rel_name, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
336 match.group(2), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
337 year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
338 month, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
339 int(match.group(1))) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
340 |