diff options
Diffstat (limited to 'blog/footers.py')
| -rwxr-xr-x | blog/footers.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/blog/footers.py b/blog/footers.py new file mode 100755 index 00000000..ca0977ae --- /dev/null +++ b/blog/footers.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +import pathlib +import subprocess +import sys + +content = pathlib.Path(sys.argv[1]) + +print("Generating footers...") + +CONTACT = { + "es": "=> {relative_root}/contacto Contacta conmigo.", + "en": "=> {relative_root}/contact Contact me.", +} + +EDIT_IN_GITHUB = { + "es": "Edita esta página.", + "en": "Edit this page.", +} + +PARENT = { + "es": "Subir.", + "en": "Go to parent." +} + +ENGLISH_PAGES = ('about.gmi', 'gemini.gmi', "contact.gmi",) + +content_gmi_files = [str(p.relative_to(content)) for p in pathlib.Path(content).glob("**/*.gmi")] + +for gmi in pathlib.Path.cwd().glob("**/*.gmi"): + relative_gmi = str(gmi.relative_to(pathlib.Path.cwd())) + if relative_gmi == "index.gmi": + # index.gmi is endless, no footer for that one + continue + + language = "en" if relative_gmi in ENGLISH_PAGES or relative_gmi.startswith("notes/") else "es" + + footer = [] + + relative_root = "/".join([".."] * (len(gmi.parts) - len(pathlib.Path.cwd().parts) - 1)) + + if relative_gmi not in ("contact.gmi", "contacto.gmi"): + footer.append(CONTACT[language].format(relative_root=relative_root)) + + content_source = None + if relative_gmi in content_gmi_files: + content_source = "blog/content/" + relative_gmi + + if relative_gmi == "notes/interesting-projects.gmi": + content_source = "interesting-projects/interesting-projects.gmi" + + if content_source: + footer.append(f"=> https://github.com/alexpdp7/alexpdp7/edit/master/{content_source} {EDIT_IN_GITHUB[language]}") + + closest_index = gmi.with_name("index.gmi") + while closest_index == gmi or closest_index.is_relative_to(pathlib.Path.cwd()) and not closest_index.exists(): + closest_index = closest_index.parent.parent / "index.gmi" + + closest_index = closest_index if closest_index.exists() else None + if closest_index: + index_link = "/".join([".."] * (len(gmi.parts) - len(closest_index.parts))) + "/" + footer.append(f"=> {index_link} {PARENT[language]}") + + gmi_content = gmi.read_text() + assert gmi_content.endswith("\n"), f"{gmi} has no trailing new line" + assert not gmi_content.endswith("\n\n"), f"{gmi} has extra empty blank line" + gmi.write_text(gmi_content + "\n" + "\n".join(footer) + "\n") |
