-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_parts.py
66 lines (59 loc) · 2.61 KB
/
sub_parts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from pelican import signals
import logging
logger = logging.getLogger(__name__)
def patch_subparts(generator):
generator.subparts = []
slugs = {}
for article in generator.articles:
slugs[article.slug] = article
if '--' in article.slug:
generator.subparts.append(article)
for article in generator.subparts:
logger.info('sub_part: Detected %s', article.slug)
(pslug, _) = article.slug.rsplit('--', 1)
if pslug in slugs:
parent = slugs[pslug]
if not hasattr(parent, 'subparts'):
parent.subparts = []
parent.subparts.append(article)
article.subpart_of = parent
article.subtitle = article.title
article.title = article.title + ", " + parent.title
generator.dates.remove(article)
generator.articles.remove(article)
if article.category:
for cat, arts in generator.categories:
if cat.name == article.category.name:
arts.remove(article)
break
else:
logger.error(
'sub_part: Cannot remove sub-part from category %s',
article.category)
if (hasattr(article, 'subphotos') or
hasattr(article, 'photo_gallery')):
parent.subphotos = (
getattr(parent, 'subphotos',
len(getattr(parent, 'photo_gallery', []))) +
getattr(article, 'subphotos', 0) +
len(getattr(article, 'photo_gallery', [])))
else:
logger.error('sub_part: No parent for %s', pslug)
generator._update_context(('articles', 'dates', 'subparts'))
def write_subparts(generator, writer):
for article in generator.subparts:
signals.article_generator_write_article.send(generator,
content=article)
writer.write_file(
article.save_as, generator.get_template(article.template),
generator.context, article=article, category=article.category,
override_output=hasattr(article, 'override_save_as'),
relative_urls=generator.settings['RELATIVE_URLS'])
if len(generator.subparts) > 0:
print('sub_part: processed {} sub-parts.'.format(
len(generator.subparts)))
def register():
signals.article_generator_finalized.connect(patch_subparts)
signals.article_writer_finalized.connect(write_subparts)