-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrender.py
83 lines (64 loc) · 2.99 KB
/
render.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import os
import sys
from pathlib import Path
from shutil import copyfile
from bin.projects.render import render_projects
from bin.render import Renderer
from bin.list import create_list
from digital_land_frontend.markdown.content_file import read_content_file
from bin.weeknotes.render import render_weeknotes
output_dir = "docs"
content_dir = "content"
url_root = ""
# setup jinja
jinja_renderer = Renderer(url_root)
content_template = jinja_renderer.get_template("content.html")
list_template = jinja_renderer.get_template("list.html")
feed_template = jinja_renderer.get_template("atom.xml.html")
def get_content_pages(directory):
return os.listdir(directory)
def markdown_files_only(files, file_ext=".md"):
return [f for f in files if f.endswith(file_ext)]
def create_output_path(fn, parent_dir=""):
if fn.stem == "index" or fn.stem == "_index":
return os.path.join(output_dir, parent_dir, "index.html")
return os.path.join(output_dir, parent_dir, fn.stem, "index.html")
def render_pages(parent_dir=""):
if parent_dir not in ["projects", "weeknotes"]:
path_to_directory = os.path.join(content_dir, parent_dir)
pages = get_content_pages(path_to_directory)
for page in pages:
p = os.path.join(path_to_directory, page)
if os.path.isdir(p):
# handle directories
print(p, "is a directory")
render_pages(os.path.join(parent_dir, page))
elif page.endswith(".md"):
if page == "_list.md":
# create an index page and feed for each _list
list = create_list(pages, path_to_directory)
content_output_path = os.path.join(output_dir, parent_dir, "index.html")
feed_output_path = os.path.join(output_dir, parent_dir, "atom.xml")
jinja_renderer.render_content_page(content_output_path, list_template, list)
jinja_renderer.render_content_feed(feed_output_path, feed_template, list, parent_dir)
else:
# compile and render markdown file
fn = Path(p)
contents = read_content_file(fn)
output_path = create_output_path(fn, parent_dir)
jinja_renderer.render_content_page(
output_path, content_template, contents
)
else:
# copy other pages to /docs
print("Moving: ", p, os.path.join(output_dir, parent_dir, page))
if not os.path.exists(os.path.join(output_dir, parent_dir)):
os.makedirs(os.path.join(output_dir, parent_dir))
copyfile(p, os.path.join(output_dir, parent_dir, page))
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--local":
jinja_renderer.set_global("staticPath", "")
render_projects("content/project")
render_weeknotes("content/weeknote")
render_pages()