Pythonbuild.py -rwxr-xr-x 2.9 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(f"render_{name}", " ".join([
17 "./render.py",
18 "-stylesheet",
19 "gen/main.css",
20 "-template",
21 _nth(1),
22 "-sitemap",
23 "sitemap.yaml",
24 "-link", link,
25 "-content", _nth(2) + "| htmlmin > $out",
26 ]))
27 return f"render_{name}"
28
29
30def _compile_sm(w: ninja_syntax.Writer, entries: list, path: pathlib.Path):
31 entries = list(filter(
32 lambda entry: "enabled" not in entry or entry["enabled"], entries))
33 for entry in entries:
34 next_path = path.joinpath(entry["name"])
35 if "others" in entry and entry["others"]: # its a dir
36 w.build(str(next_path), "mkdir")
37 _compile_sm(w, entry["others"], next_path)
38 else:
39 source = list(next_path.glob("README.md"))
40 if len(source) == 1:
41 source = str(source[0])
42 link = source.replace("content/", "", 1)
43 link = source.replace("/README.md", "", 1)
44 target = source.replace("content/", "", 1)
45 target_dir = target.replace("/README.md", "", 1)
46 w.build(target_dir, "mkdir")
47 target = target.replace("README.md", "index.html", 1)
48 target = "gen/" + target
49 cmd = _render_rule(w, source.replace("/", "_", -1), link)
50 w.build(target, cmd, inputs=[
51 "index.jinja", source, "sitemap.yaml", "render.py"])
52
53
54if __name__ == "__main__":
55 with open("sitemap.yaml", "r") as fp:
56 sitemap = yaml.safe_load(fp.read())
57 with open("build.ninja", "w") as fp:
58 w = ninja_syntax.Writer(fp)
59 w.comment("automatically generated, do not edit.")
60 w.variable("sass_flags", "--style compressed")
61 w.rule("mkdir", command="mkdir -p $out")
62 w.rule("compile_sass", command="sassc $sass_flags $in $out")
63 w.rule("make_qr_code", command="cat $in | qrencode -o $out")
64 w.rule("copy", command="cp $in $out")
65 w.build("index.jinja", "phony")
66 w.build("render.py", "phony")
67 w.build("sitemap.yaml", "phony")
68 w.build("gen", "mkdir")
69 w.build("gen/contact.png", "make_qr_code", inputs=["contact.txt"])
70 w.build("gen/ks_stylized.png", "copy", inputs=["assets/ks_stylized.png"])
71 w.build("gen/favicon.ico", "copy", inputs=["assets/favicon.ico"])
72 w.build("gen/main.css", "compile_sass", inputs=["main.scss"])
73 cmd = _render_rule(w, "index", "/")
74 w.build("gen/index.html", cmd, inputs=[
75 "index.jinja", "content/index.md", "sitemap.yaml", "render.py"])
76 if sitemap["enabled"]:
77 _compile_sm(w, sitemap["entries"], pathlib.Path("./content"))
78 w.close()
79
80 subprocess.call(args=["ninja"])