-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.py
65 lines (50 loc) · 2.04 KB
/
process.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
import frontmatter
from pathlib import Path
import logging
import markdown
import jinja2
import shutil
logging.basicConfig(level=logging.INFO)
template_loader = jinja2.FileSystemLoader(searchpath="templates")
template_env = jinja2.Environment(loader=template_loader)
class Page:
def __init__(self, path):
logging.info(f"Loading page at {path}")
self.path = path
self.data = frontmatter.load(path)
self.slug = self.data.get('slug', self.path.stem.lower().replace(" ", "-"))
self.title = self.data.get('title', self.path.stem)
def render(self, render_base, nav_links=[]):
content = markdown.markdown(self.data.content)
template = template_env.get_template("template.html")
rendered = template.render(
title=self.title,
content=content,
slug=self.slug,
nav_links=nav_links
)
target_path = render_base.joinpath(self.slug).joinpath("index.html")
target_path.parent.mkdir(parents=True, exist_ok=True)
logging.info(f"Rendering page to {target_path}")
target_path.write_text(rendered)
class Site:
def __init__(self, base_path):
self.base = Path(base_path)
logging.info(f"Processing site at {self.base.resolve()}")
page_files = list(self.base.glob("**/*.md"))
logging.info(f"Found {len(page_files)} page files")
self.pages = [Page(path) for path in page_files]
def render(self, output_path="docs"):
output_path = Path(output_path)
# Initialize the site's pages
self.pages.sort(key=lambda page: page.data.get("position", 999))
nav_links = [{"title": page.title, "slug": page.slug} for page in self.pages]
for page in self.pages:
page.render(output_path, nav_links=nav_links)
def move_statics(self):
logging.info("Copying statics")
shutil.rmtree("docs/static", ignore_errors=True)
shutil.copytree("statics", "docs/statics", dirs_exist_ok=True)
site = Site("content")
site.render()
site.move_statics()