code.pablomurad.com

docs.pablomurad.com

Source of docs.pablomurad.com, personal guides and how-tos built with Zensical

scripts/prepare_footer.py raw
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Escreve a linha do rodapé (data, SHA, versão)."""

from __future__ import annotations

import subprocess
from datetime import datetime
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "overrides" / "partials" / "build-meta.html"
PERFIL = "https://git.pablomurad.com/pablo/docs.pablomurad.com"
VERSION = "1.00"
_MESES = (
    "",
    "janeiro",
    "fevereiro",
    "março",
    "abril",
    "maio",
    "junho",
    "julho",
    "agosto",
    "setembro",
    "outubro",
    "novembro",
    "dezembro",
)


def _fmt_pt(dt: datetime) -> str:
    return f"{dt.day} de {_MESES[dt.month]} de {dt.year}"


def _git(*args: str) -> str:
    try:
        return subprocess.check_output(
            ["git", *args],
            cwd=ROOT,
            stderr=subprocess.DEVNULL,
            text=True,
        ).strip()
    except (OSError, subprocess.CalledProcessError):
        return ""


def main() -> None:
    out = _git("log", "-1", "--format=%ci")
    try:
        date = _fmt_pt(datetime.strptime(out[:19], "%Y-%m-%d %H:%M:%S"))
    except (TypeError, ValueError):
        date = _fmt_pt(datetime.now())

    sha = _git("rev-parse", "--short", "HEAD") or "local"
    href = PERFIL

    OUT.parent.mkdir(parents=True, exist_ok=True)
    OUT.write_text(
        f'<span class="footer-meta">Atualizado {date}</span>'
        f'<span class="footer-sep"> · </span>'
        f'<a class="footer-build" href="{href}">{sha}</a>'
        f'<span class="footer-sep"> · </span>'
        f'<span class="footer-version">v{VERSION} <em>estável</em></span>\n',
        encoding="utf-8",
    )
    print(f"Wrote {OUT.relative_to(ROOT)}")


if __name__ == "__main__":
    main()