Mercurial > piecrust2
annotate piecrust/sources/prose.py @ 1189:bfa470063ee2 draft
cm: Update node packages.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 16 Jun 2021 09:28:42 -0700 |
parents | a4d7ff2cdc5c |
children |
rev | line source |
---|---|
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
1 import copy |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import logging |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
394
diff
changeset
|
3 from piecrust.sources.default import DefaultContentSource |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 logger = logging.getLogger(__name__) |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
394
diff
changeset
|
9 class ProseSource(DefaultContentSource): |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 SOURCE_NAME = 'prose' |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 def __init__(self, app, name, config): |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
13 super().__init__(app, name, config) |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 self.config_recipe = config.get('config', {}) |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
1089
a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
16 def _createItemMetadata(self, path): |
a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
17 metadata = super()._createItemMetadata(path) |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
18 config = metadata.setdefault('config', {}) |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
19 config.update(self._makeConfig(path)) |
866
d9059257743c
refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
20 return metadata |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
22 def _makeConfig(self, path): |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
23 c = copy.deepcopy(self.config_recipe) |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
24 if c.get('title') == '%first_line%': |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
25 c['title'] = get_first_line(path) |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 return c |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 def get_first_line(path): |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 with open(path, 'r') as f: |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 while True: |
1089
a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
32 line = f.readline() |
a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
33 if not line: |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 break |
1089
a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
35 line = line.strip() |
a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
36 if not line: |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 continue |
1089
a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
38 return line |
157
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 return None |
55910ab4bfea
First draft of the `prose` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 |