#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Generate the expanded, rebranded static site from the Chadnet mirror + curated additions. Every page is written as *.html and every internal page-link points to the .html file, so any static host renders it instead of offering a download.""" import json, os, re, shutil, html as H import content_extra as CE SITE_NAME = "Library of GigaChad" SITE_SHORT = "GigaChad" BLOB = "Become a GigaChad." MIRROR = "/home/hermes/chad-library/mirror" SITED = "/home/hermes/chad-library/site" TOP = [ ("arts-and-literature", "Arts & Literature", "ships.jpg"), ("computers-and-internet", "Computers & Internet", "matrix.png"), ("health", "Health", "pietro.png"), ("history", "History", "rome2.jpg"), ("politics-philosophy-and-society","Politics, Philosophy & Society","putin.jpg"), ("practical-skills", "Practical Skills", "forest.jpg"), ("religion-and-spirituality", "Religion & Spirituality", "religion.jpg"), ("science", "Science", "cocaine.jpg"), ] NEW_TOP = [ ("3d-printing-and-fabrication", "3D Printing & Fabrication", "matrix.png"), ("physical-culture", "Physical Culture", "putin.jpg"), ("homesteading-and-off-grid", "Homesteading & Off-Grid", "forest.jpg"), ("engineering-and-radio", "Engineering & Radio", "matrix.png"), ("other", "Other (WIP)", "xp.jpg"), ] ALL_TOP = TOP + NEW_TOP NEW_SET = {"3d-printing-and-fabrication","physical-culture","homesteading-and-off-grid","engineering-and-radio"} TITLE_BY_SLUG = {s: t for s, t, _ in ALL_TOP} BG_BY_SLUG = {s: "/icons/home/" + b for s, _, b in ALL_TOP} NEW_CATEGORY_OBJS = [CE.NEW_CATEGORY_3DPRINT, CE.NEW_CATEGORY_FITNESS, CE.NEW_CATEGORY_HOMESTEAD, CE.NEW_CATEGORY_ELECTRONICS] ICON = { "folder": "/icons/links/folder.png", "globe": "/icons/links/globe.svg", "document": "/icons/links/document.png", "painting": "/icons/links/painting.png", "youtube": "/icons/links/youtube.svg", "article": "/icons/links/article.png", "chadsquare": "/icons/links/chad-square.png", "link": "/icons/links/book.png", } def esc(t): return H.escape(str(t)) def page(slug): return slug + ".html" # ---------------------------------------------------------------- link fixing def fix_href(val): """Rewrite a single (relative) href/src to the flat *.html scheme. External, fragments, anchors and file-assets stay; internal page links get .html.""" val = val.strip() if not val: return val if val.startswith(("http", "//", "mailto", "tel", "data:", "javascript:", "ftp:")): return val frag = q = "" if "#" in val: val, frag = val.split("#", 1); frag = "#" + frag if "?" in val: val, q = val.split("?", 1); q = "?" + q if not val: return q + frag absolute = val.startswith("/") # strip leading dir prefixes; keep the real remaining subpath for files segments = [x for x in val.split("/") if x not in ("", ".", "..")] if not segments: return ("/" if absolute else "") + q + frag base = segments[-1] if "." in base: # file asset — keep its real subpath (files live under /files or root) rel = "/".join(segments) return "/" + rel + q + frag # internal page — flatten to a root *.html return "/" + base + ".html" + q + frag def rewrite_html_links(content): return re.sub(r'(href|src)=(["\'])([^"\']*)\2', lambda m: m.group(1) + "=" + m.group(2) + fix_href(m.group(3)) + m.group(2), content) def rebrand(html): html = html.replace("https://wiki.chadnet.org", "") html = html.replace("https://lib.chadnet.org", "") html = html.replace("Library of Chadnet", SITE_NAME) html = html.replace("— Chadnet", "— " + SITE_SHORT) html = html.replace("– Chadnet", "– " + SITE_SHORT) html = html.replace('>wiki.chadnet.org<', '>' + SITE_SHORT + '<') html = html.replace("Chadnet", SITE_SHORT) return html # ---------------------------------------------------------------- renderers def head(title): return f"""
{esc(SITE_NAME)} | {esc(SITE_SHORT)}
""" def nav(parent_slug): if not parent_slug: back = 'Home' else: back = f'{esc(TITLE_BY_SLUG.get(parent_slug, parent_slug))}' return f'' def render_item(it, extra=False): href = it["href"] text = it["text"] or "" note = it.get("note", "") itype = it["type"] icon = it.get("icon") or ICON.get(itype, ICON["link"]) if not href: a = esc(text) elif href.startswith("http") or href.startswith("//"): a = f'{esc(text)}' elif href.startswith("mailto"): a = f'{esc(text)}' else: a = f'{esc(text)}' if note: a += " " + note return f'{esc(cat["blurb"])}
') for h2key, items in cat["sections"]: out.append(render_section(h2key, items, extra=True)) out.append(footer()) return "\n".join(out) def homepage(): tiles = [] for slug, title, _ in ALL_TOP: newtag = ' (NEW)' if slug in NEW_SET else "" tiles.append(f'Welcome to {esc(SITE_NAME)}—a library of information. Moar info.
News: Everything Chadnet ever had, plus all-new categories — 3D Printing & Fabrication, Physical Culture, Homesteading & Off-Grid, Engineering & Radio — and hundreds more resources.
{tiles_html}{esc(SITE_NAME)} | {esc(SITE_SHORT)}
""" MSG = ["Because chad.net was taken.","A GigaChad owns his printer.","More power to the people.", "Print it. Assemble it. Carry it.","Respect the machine, or the machine respects you.","Veni, vidi, vici.", "Stronger every year.","Under the ice.","Become ungovernable.","Real men make things.","VICTORY TO THE GODS!", "Many articools!","Fueled by curiosity.","TALLY-HO!","Wake up.","Remember who you are.","Grug loves filament.", "A site for sore eyes!","Shop class will save America.","Self-reliance is the point.","Sun and steel.", "A man prepares.","Build your own.","The library never closes.","Handmade.","Operated from the garage.", "Yes hello.","Yarr!","Standing by.","Bring back the shop class.","Do it yourself, or find who does."] def msg_js(): return "let messages = " + json.dumps(MSG) + ";\n" + ( "function setMsg() {\n const el = document.getElementById('message');\n" " if (el) el.textContent = '« ' + messages[Math.floor(Math.random() * messages.length)] + ' »';\n}\n") def simple_page(title, body): o = head(title) + f'"
elif line.startswith("- "): o += f"{esc(line)}
\n" o += footer() return o # ----------------------------------------------------------------- main def main(): shutil.rmtree(SITED, ignore_errors=True) shutil.copytree(MIRROR, SITED) for f in ["_progress.log"]: p = os.path.join(SITED, f) if os.path.exists(p): os.remove(p) d = json.load(open(os.path.join(SITED, "..", "site_data.json"))) data_slugs = set(d["pages"].keys()) | set(TITLE_BY_SLUG) # 1) rename extensionless pages -> .html ; rewrite links + rebrand every real page renamed = {} for root, _, files in os.walk(SITED): for fn in files: if "." in fn: continue full = os.path.join(root, fn) try: data = open(full, encoding="utf-8", errors="ignore").read() except Exception: continue if "