Pythonbuild.py -rwxr-xr-x 3.5 KiB
1#!/usr/bin/env python
2
3import pathlib
4import subprocess
5
6import yaml
7
8import ninja_syntax
9
10
11def _nth(n):
12 return f"$$(echo $in | cut -d' ' -f{n})"
13
14
15def _render_rule(w: ninja_syntax.Writer, name, link):
16 w.rule(
17 f"render_{name}",
18 " ".join(
19 [
20 "./render.py",
21 "-stylesheet",
22 "gen/main.css",
23 "-template",
24 _nth(1),
25 "-sitemap",
26 "sitemap.yaml",
27 "-link",
28 link,
29 "-content",
30 _nth(2) + "| htmlmin > $out",
31 ]
32 ),
33 )
34 return f"render_{name}"
35
36
37def _render_rss_rule(w: ninja_syntax.Writer):
38 w.rule(f"render_rss", " ".join(["./render.py", "-rss", "> $out"]))
39 return f"render_rss"
40
41
42def _compile_sm(w: ninja_syntax.Writer, entries: list, path: pathlib.Path):
43 entries = list(
44 filter(lambda entry: "enabled" not in entry or entry["enabled"], entries)
45 )
46 for entry in entries:
47 next_path = path.joinpath(entry["name"])
48 if "others" in entry and entry["others"]: # its a dir
49 w.build(str(next_path), "mkdir")
50 _compile_sm(w, entry["others"], next_path)
51 else:
52 source = list(next_path.glob("README.md"))
53 if len(source) == 1:
54 source = str(source[0])
55 link = source.replace("content/", "", 1)
56 link = source.replace("/README.md", "", 1)
57 target = source.replace("content/", "", 1)
58 target_dir = target.replace("/README.md", "", 1)
59 w.build(target_dir, "mkdir")
60 target = target.replace("README.md", "index.html", 1)
61 target = "gen/" + target
62 cmd = _render_rule(w, source.replace("/", "_", -1), link)
63 w.build(
64 target,
65 cmd,
66 inputs=["index.html.jinja", source, "sitemap.yaml", "render.py"],
67 )
68
69
70if __name__ == "__main__":
71 with open("sitemap.yaml", "r") as fp:
72 sitemap = yaml.safe_load(fp.read())
73 with open("build.ninja", "w") as fp:
74 w = ninja_syntax.Writer(fp)
75 w.comment("automatically generated, do not edit.")
76 w.variable("sass_flags", "--style compressed")
77 w.rule("mkdir", command="mkdir -p $out")
78 w.rule("compile_sass", command="sassc $sass_flags $in $out")
79 w.rule("make_qr_code", command="cat $in | qrencode -o $out")
80 w.rule("copy", command="cp $in $out")
81 w.build("index.html.jinja", "phony")
82 w.build("render.py", "phony")
83 w.build("sitemap.yaml", "phony")
84 w.build("gen", "mkdir")
85 w.build("gen/contact.png", "make_qr_code", inputs=["contact.txt"])
86 w.build("gen/ks_stylized.png", "copy", inputs=["assets/ks_stylized.png"])
87 w.build("gen/favicon.ico", "copy", inputs=["assets/favicon.ico"])
88 w.build("gen/main.css", "compile_sass", inputs=["main.scss"])
89 cmd = _render_rule(w, "index", "/")
90 w.build(
91 "gen/index.html",
92 cmd,
93 inputs=[
94 "index.html.jinja",
95 "content/index.md",
96 "sitemap.yaml",
97 "render.py",
98 ],
99 )
100 cmd = _render_rss_rule(w)
101 w.build("gen/index.xml", cmd)
102 if sitemap["enabled"]:
103 _compile_sm(w, sitemap["entries"], pathlib.Path("./content"))
104 w.close()
105
106 subprocess.call(args=["ninja"])