import yaml import dominate from dominate.tags import * import os import copy from pathlib import Path def load_yaml(data: str) -> any: return yaml.load(data, Loader=yaml.CFullLoader) def main(): with open("vanity.yml") as f: conf = load_yaml(f.read()) assert "host" in conf assert "paths" in conf conf["paths"] = { sanitise_path(p): preprocess_path_object(conf["paths"][p]) for p in conf["paths"] } print(conf["paths"]) output_path = Path("_dist") if not os.path.exists(output_path): os.makedirs(output_path) with open(output_path / "index.html", "w") as f: f.write(generate_index(conf)) for path in conf["paths"]: fs_path = output_path / path.removeprefix("/") os.makedirs(fs_path) with open(fs_path / "index.html", "w") as f: f.write(generate_page(conf, path, conf["paths"][path])) def generate_index(conf): doc = dominate.document(title=conf["host"]) with doc.head: style(open("styles.css").read()) with doc.add(div(cls="projects")): for path in conf.get("paths", {}): path_details = conf["paths"][path] with div(cls="project", id=path): h2( span(conf["host"], cls="secondary"), path ) # path always leads with a / if "byline" in path_details: p(path_details["byline"]) a("Docs", cls="button", href=f"https://pkg.go.dev/{conf['host']}{path}") a("Source", cls="button", href=path_details["repo"]) return str(doc) def generate_page(conf, path, obj): doc = dominate.document(title=conf["host"] + path) # https://pkg.go.dev/cmd/go#hdr-Remote_import_paths # https://github.com/golang/gddo/wiki/Source-Code-Links with doc.head: meta( name="go-import", content=f"{conf['host']}{path} {obj['vcs']} {obj['repo']}" ) if "display" in obj: meta(name="go-source", content=obj["display"]) meta(http_equiv="refresh", content=f"0;url=//{conf['host']}#{path}") return str(doc) def sanitise_path(path): if not path.startswith("/"): path = "/" + path return path def preprocess_path_object(obj): obj = copy.copy(obj) assert "repo" in obj if "vcs" not in obj: obj["vcs"] = "git" return obj if __name__ == "__main__": main()