-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcompile.py
53 lines (44 loc) · 1.63 KB
/
compile.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
from jinja2 import Environment, PackageLoader, select_autoescape
from recipe_compiler.read import read_recipe_file
from recipe_compiler.parse import parse_to_recipe
from recipe_compiler.render import (
render_home_page,
render_recipe_page,
render_contribute_page,
)
from recipe_compiler.write import write_home_page, write_page
import argparse
import glob
import os
parser = argparse.ArgumentParser()
parser.add_argument(
"--prod", help="Create github pages compatible format when rendering", required=False, action="store_true"
)
if __name__ == "__main__":
target = "dev"
if parser.parse_args().prod:
target = "prod"
# Read
recipe_files = glob.glob("./recipes/*.md")
recipe_contents = [read_recipe_file(recipe_file) for recipe_file in recipe_files]
# Parse
recipes = [parse_to_recipe(recipe_content) for recipe_content in recipe_contents]
# Render
env = Environment(
loader=PackageLoader("recipe_compiler", "templates"),
autoescape=select_autoescape(["html"]),
)
# Handles the path setting for production versus local
# (Production has /DevCookbook/ prepended to the path)
env.globals = {"path_base": "/DevCookbook/" if target == "prod" else "/"}
home_page = render_home_page(recipes, env)
contribute_page = render_contribute_page(env)
recipe_pages = zip(
[recipe.slug for recipe in recipes],
[render_recipe_page(recipe, env) for recipe in recipes],
)
# Write
write_home_page(home_page)
write_page("contribute", contribute_page)
for recipe_slug, recipe_page in recipe_pages:
write_page(recipe_slug, recipe_page)