Mercurial > piecrust2
annotate piecrust/sources/posts.py @ 892:c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 02 Jul 2017 22:20:32 -0700 |
parents | d9059257743c |
children | 86b684cc0551 |
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) |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
13 from piecrust.sources.mixins import SimpleAssetsSubDirMixin |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
14 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
|
15 |
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 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
20 class PostsSource(FSContentSource, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
21 SimpleAssetsSubDirMixin, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
22 IPreparingSource, IInteractiveSource): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 PATH_FORMAT = None |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
24 DEFAULT_PIPELINE_NAME = 'page' |
3
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): |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
27 super().__init__(app, name, config) |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
28 |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
29 config.setdefault('data_type', 'page_iterator') |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
30 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 @property |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 def path_format(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 return self.__class__.PATH_FORMAT |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
39 def _finalizeContent(self, groups): |
866
d9059257743c
refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
40 SimpleAssetsSubDirMixin._removeAssetGroups(self, groups) |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
41 |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
42 def getParentGroup(self, item): |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
43 return None |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
577
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 getRelatedContents(self, item, relationship): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
46 if relationship == REL_ASSETS: |
862
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
47 return SimpleAssetsSubDirMixin._getRelatedAssetsContents( |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
48 self, item) |
fddaf43424e2
refactor: Get the page assets to work again in the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
49 return FSContentSource.getRelatedContents(self, item, relationship) |
577
ff404adfcf45
sources: Add method to get a page factory from a path.
Ludovic Chabant <ludovic@chabant.com>
parents:
576
diff
changeset
|
50 |
866
d9059257743c
refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
51 def findGroup(self, spec): |
d9059257743c
refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
52 return None |
d9059257743c
refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
53 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 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
|
58 slug = route_params.get('slug') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 |
281
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
60 try: |
0641fe5c3ef9
serve: Don't crash when a post URL doesn't match our expectations.
Ludovic Chabant <ludovic@chabant.com>
parents:
244
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 except ValueError: |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
281
diff
changeset
|
68 return None |
54
a46354306738
Use properly formatted date components for the blog sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
69 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
70 ext = route_params.get('ext') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 if ext is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 if len(self.supported_extensions) == 1: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 ext = self.supported_extensions[0] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 replacements = { |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
76 '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
|
77 '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
|
78 '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
|
79 'slug': slug, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
80 'ext': ext |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
81 } |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 needs_recapture = False |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 if year 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['year'] = '????' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 if month 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['month'] = '??' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 if day is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 replacements['day'] = '??' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 if slug is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 replacements['slug'] = '*' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 if ext is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 needs_recapture = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 replacements['ext'] = '*' |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
98 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
|
99 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
|
100 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 if needs_recapture: |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
102 possible_paths = osutil.glob(path) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 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
|
104 return None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 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
|
106 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
|
107 return None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
109 metadata = self._parseMetadataFromPath(path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
110 return ContentItem(path, metadata) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
111 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
112 def _parseMetadataFromPath(self, path): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
113 regex_repl = { |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
114 '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
|
115 '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
|
116 '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
|
117 'slug': '(?P<slug>.*)', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
118 'ext': '(?P<ext>.*)' |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
119 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 if not m: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
124 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
|
125 "format: %s" % path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
126 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 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
|
131 metadata = { |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
132 'route_params': { |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
133 'year': year, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
134 'month': month, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
135 'day': day, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
136 'slug': m.group('slug') |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
137 }, |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
138 'date': timestamp |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
139 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
140 return metadata |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
141 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
142 def getSupportedRouteParameters(self): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
143 return [ |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 RouteParameter('year', RouteParameter.TYPE_INT4)] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 def setupPrepareParser(self, parser, app): |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
150 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
151 '-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
|
152 "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
|
153 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
|
154 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
155 def createContent(self, args): |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
156 dt = datetime.date.today() |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
157 date = args.get('date') |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
158 if isinstance(date, str): |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
159 if date == 'today': |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
160 pass # Keep the default we had. |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
161 elif date == 'tomorrow': |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
162 dt += datetime.timedelta(days=1) |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
163 elif date.startswith('+'): |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
164 try: |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
165 dt += datetime.timedelta(days=int(date.lstrip('+'))) |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
166 except ValueError: |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
167 raise Exception("Date offsets must be numbers.") |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
168 else: |
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
169 try: |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
170 year, month, day = [int(s) for s in date.split('/')] |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
171 except ValueError: |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
172 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
|
173 "YEAR/MONTH/DAY.") |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
174 dt = datetime.date(year, month, day) |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
175 elif isinstance(date, datetime.datetime): |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
176 year = date.year |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
177 month = date.month |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
178 day = date.day |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
179 else: |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
180 raise Exception("Unknown date: %s" % date) |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
181 |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
182 slug, ext = os.path.splitext(args.get('slug')) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
183 if not ext: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
184 ext = self.default_auto_format |
80
838a9dd0e23c
Better date creation for blog post scaffolding.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
185 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
|
186 tokens = { |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
187 'slug': args.get('slug'), |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
188 'ext': ext, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
189 'year': '%04d' % year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
190 'month': '%02d' % month, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
191 'day': '%02d' % day |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
192 } |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
193 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
|
194 path = os.path.join(self.fs_endpoint_path, rel_path) |
892
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
195 metadata = self._parseMetadataFromPath(path) |
c445a3d5d950
internal: Make `createContent` use a dictionary-like object.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
196 metadata['config'] = {'title': uri_to_title(slug)} |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
197 return ContentItem(path, metadata) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
198 |
576
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
199 def getInteractiveFields(self): |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
200 dt = datetime.date.today() |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
201 return [ |
0c74a6c4533d
sources: Add code to support "interactive" metadata acquisition.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
202 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
|
203 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
|
204 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
|
205 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
|
206 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
207 def _checkFsEndpointPath(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
208 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
|
209 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
|
210 return False |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
211 raise InvalidFileSystemEndpointError(self.name, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
212 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
|
213 return True |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
214 |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
215 def _makeContentItem(self, rel_path, slug, year, month, day): |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
216 path = os.path.join(self.fs_endpoint_path, rel_path) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
217 timestamp = datetime.date(year, month, day) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
218 metadata = { |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
219 'route_params': { |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
220 'slug': slug, |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
221 'year': year, |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
222 'month': month, |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
223 'day': day}, |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
224 'date': timestamp |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
225 } |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
226 |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
227 _, ext = os.path.splitext(path) |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
228 if ext: |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
229 fmt = self.auto_formats.get(ext.lstrip('.')) |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
230 if fmt: |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
231 metadata['config'] = {'format': fmt} |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
232 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
233 return ContentItem(path, metadata) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
234 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
235 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
236 class FlatPostsSource(PostsSource): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
237 SOURCE_NAME = 'posts/flat' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
238 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
|
239 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
|
240 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
241 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
|
242 super().__init__(app, name, config) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
243 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
244 def getContents(self, group): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
245 if not self._checkFSEndpoint(): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
246 return None |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
247 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
248 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
|
249 self.fs_endpoint_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
250 pattern = FlatPostsSource.PATTERN |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
251 _, __, 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
|
252 for f in filenames: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
253 match = pattern.match(f) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
254 if match is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
255 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
|
256 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
257 "'%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
|
258 "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
|
259 continue |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
260 yield self._makeContentItem( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
261 f, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
262 match.group(4), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
263 int(match.group(1)), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
264 int(match.group(2)), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
265 int(match.group(3))) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
266 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
267 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
268 class ShallowPostsSource(PostsSource): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
269 SOURCE_NAME = 'posts/shallow' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
270 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
|
271 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
|
272 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
|
273 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
274 def __init__(self, app, name, config): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
275 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
|
276 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
277 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
|
278 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
|
279 return |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
280 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
281 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
|
282 self.fs_endpoint_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
283 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
|
284 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
|
285 _, year_dirs, __ = next(osutil.walk(self.fs_endpoint_path)) |
5 | 286 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
|
287 for yd in year_dirs: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
288 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
|
289 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
290 "'%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
|
291 "Is that a typo?") |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
292 continue |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
293 year = int(yd) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
294 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
|
295 |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
296 _, __, 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
|
297 for f in filenames: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
298 match = file_pattern.match(f) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
299 if match is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
300 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
|
301 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
302 "'%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
|
303 "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
|
304 continue |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
305 yield self._makeContentItem( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
306 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
|
307 match.group(3), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
308 year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
309 int(match.group(1)), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
310 int(match.group(2))) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
311 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
312 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
313 class HierarchyPostsSource(PostsSource): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
314 SOURCE_NAME = 'posts/hierarchy' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
315 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
|
316 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
|
317 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
|
318 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
|
319 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
320 def __init__(self, app, name, config): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
321 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
|
322 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
323 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
|
324 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
|
325 return |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
326 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
327 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
|
328 self.fs_endpoint_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
329 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
|
330 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
|
331 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
|
332 _, year_dirs, __ = next(osutil.walk(self.fs_endpoint_path)) |
5 | 333 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
|
334 for yd in year_dirs: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
335 year = int(yd) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
336 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
|
337 |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
338 _, month_dirs, __ = next(osutil.walk(year_dir)) |
5 | 339 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
|
340 for md in month_dirs: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
341 month = int(md) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
342 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
|
343 |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
516
diff
changeset
|
344 _, __, 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
|
345 for f in filenames: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
346 match = file_pattern.match(f) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
347 if match is None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
348 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
|
349 logger.warning( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
350 "'%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
|
351 "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
|
352 continue |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
353 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
|
354 yield self._makeContentItem( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
355 rel_name, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
356 match.group(2), |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
357 year, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
358 month, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
841
diff
changeset
|
359 int(match.group(1))) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
360 |