-
Notifications
You must be signed in to change notification settings - Fork 844
/
Copy pathautopages.py
64 lines (53 loc) · 2.11 KB
/
autopages.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
import logging
import os
import os.path
from pelican import signals
from pelican.contents import Page
logger = logging.getLogger("autopages")
def yield_files(root):
root = os.path.realpath(os.path.abspath(root))
for dirpath, dirnames, filenames in os.walk(root):
for dirname in list(dirnames):
try:
if dirname.startswith("."):
dirnames.remove(dirname)
except IndexError:
# duplicate already removed?
pass
for filename in filenames:
if filename.startswith("."):
continue
yield os.path.join(dirpath, filename)
def make_page(readers, context, filename):
base_path, filename = os.path.split(filename)
page = readers.read_file(base_path, filename, Page, None, context)
slug, _ = os.path.splitext(filename)
return slug, page
def make_pages(readers, context, path):
pages = {}
for filename in yield_files(path):
try:
slug, page = make_page(readers, context, filename)
except Exception:
logger.exception("Could not make autopage for %r", filename)
continue
pages[slug] = page
return pages
def create_autopages(article_generator):
settings = article_generator.settings
readers = article_generator.readers
context = article_generator.context
authors_path = settings.get("AUTHOR_PAGE_PATH", "authors")
categories_path = settings.get("CATEGORY_PAGE_PATH", "categories")
tags_path = settings.get("TAG_PAGE_PATH", "tags")
author_pages = make_pages(readers, context, authors_path)
category_pages = make_pages(readers, context, categories_path)
tag_pages = make_pages(readers, context, tags_path)
for author, _ in article_generator.authors:
author.page = author_pages.get(author.slug, "")
for category, _ in article_generator.categories:
category.page = category_pages.get(category.slug, "")
for tag in article_generator.tags:
tag.page = tag_pages.get(tag.slug, "")
def register():
signals.article_generator_finalized.connect(create_autopages)