Files
felhom.eu/documentation/audits/website-globe-2026-10-08/globe.py
T

44 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
"""Replace the nav's text language switch with the globe on every page (2026-10-08 ruling: a globe with a list of
languages, matching the controller). One language list; each page's own URL and its twins' URLs."""
import io, os, re, sys
W = "/mnt/5_hdd/felhom.eu/git/felhom.eu/website"
LANGS = [("hu", "Magyar"), ("en", "English")] # order on every page
LABEL = {"hu": "Nyelv", "en": "Language"}
# page -> {lang: url}; NO_TWIN pages point their foreign item at the other language's home
SETS = [("index.html", "/", "en/index.html", "/en/"), ("alkalmazasok.html", "/alkalmazasok", "en/apps.html", "/en/apps"),
("technologiak.html", "/technologiak", "en/technology.html", "/en/technology"),
("biztonsagimentes.html", "/biztonsagimentes", "en/backups.html", "/en/backups"),
("gyik.html", "/gyik", "en/faq.html", "/en/faq"), ("kapcsolat.html", "/kapcsolat", "en/contact.html", "/en/contact"),
("letoltes.html", "/letoltes", "en/download.html", "/en/download")]
PAGES = {}
for hu, hu_u, en, en_u in SETS:
PAGES[hu] = ("hu", {"hu": hu_u, "en": en_u})
PAGES[en] = ("en", {"hu": hu_u, "en": en_u})
PAGES["404.html"] = ("hu", {"hu": "/", "en": "/en/"})
PAGES["szolgaltatasok-nonpublic.html"] = ("hu", {"hu": "/szolgaltatasok-nonpublic", "en": "/en/"})
def globe(lang, urls):
items = []
for code, name in LANGS:
cur = code == lang
items.append('<li><a href="%s" lang="%s" hreflang="%s" class="lang-globe-item%s"%s>%s</a></li>'
% (urls[code], code, code, " current" if cur else "", ' aria-current="true"' if cur else "", name))
return ('<li class="lang-globe-li"><details class="lang-globe"><summary class="lang-globe-btn" aria-label="%s" title="%s">'
'<svg class="ico-lg" aria-hidden="true"><use href="/assets/icons.svg?v=1#i-globe"/></svg></summary>'
'<ul class="lang-globe-menu">%s</ul></details></li>') % (LABEL[lang], LABEL[lang], "".join(items))
SW = re.compile(r'<li><a href="[^"]*" lang="(?:hu|en)" hreflang="(?:hu|en)" class="lang-switch">[^<]*</a></li>')
for p, (lang, urls) in PAGES.items():
path = os.path.join(W, p)
s = io.open(path, encoding="utf-8-sig").read()
s, n = SW.subn(lambda m: globe(lang, urls), s)
assert n == 1, (p, n)
s, k = re.subn(r"/assets/site\.css\?v=2\b", "/assets/site.css?v=3", s)
assert k == 1, (p, k)
with io.open(path, "w", encoding="utf-8-sig", newline="\n") as f:
f.write(s)
print(p, "globe + css v3")