#!/usr/bin/env python3 """One-off migration of the kmalloc.org site from build.py to motorino. Usage: migrate.py [--site DIR] [--old-out DIR] It builds the current site with build.py (the reference, saved in --old-out), then writes the new layout in place: pages as Markdown in content//, news with the new header, the reduced site.json and lang files, and the new templates. The pages are converted from the HTML that build.py produced, so their text is the same by construction; HTML that Markdown cannot express (a table, an image, a heading with a class) is kept as raw HTML in the file. Delete this tool once the new site is live.""" import argparse import ast import json import os import re import shutil import subprocess import sys # ---------------------------------------------------------------- HTML to Markdown BLOCK = re.compile(r"\s*(?:()|<(p|h[1-6]|ul|ol|table|blockquote|div)\b([^>]*)>(.*?))", re.S) ID_ONLY = re.compile(r'^\s*id="([^"]+)"\s*$') def inline_to_md(inner): """Inline HTML as Markdown, or None when it has markup that Markdown cannot say.""" s = inner.strip() s = re.sub(r'(.*?)', r"[\2](\1)", s, flags=re.S) s = re.sub(r"(.*?)", r"**\1**", s, flags=re.S) s = re.sub(r"(.*?)", r"*\1*", s, flags=re.S) s = re.sub(r"(.*?)", r"`\1`", s, flags=re.S) if "<" in s: return None return "\n".join(line.strip() for line in s.splitlines()) def block_to_md(match): """One top-level block as Markdown, or as the raw HTML it came from.""" raw = match.group(0).strip() comment, tag, attrs, inner = match.groups() if comment: return raw if tag == "p" and not attrs.strip(): text = inline_to_md(inner) return text if text is not None else raw if re.fullmatch(r"h[1-6]", tag): text = inline_to_md(inner) heading_id = ID_ONLY.match(attrs) if text is not None and (not attrs.strip() or heading_id): line = "#" * int(tag[1]) + " " + text return line + (" {#" + heading_id.group(1) + "}" if heading_id else "") return raw if tag == "ul" and not attrs.strip(): items = re.findall(r"
  • (.*?)
  • ", inner, flags=re.S) texts = [inline_to_md(i) for i in items] if items and all(t is not None for t in texts) and not re.sub(r"
  • .*?
  • |\s", "", inner, flags=re.S): return "\n".join("- " + t for t in texts) return raw def html_to_md(html): """Convert the body of a page: the blocks of build.py output become Markdown, the rest stays HTML.""" parts, pos = [], 0 while True: if not html[pos:].strip(): break m = BLOCK.match(html, pos) if not m: raise ValueError(f"unconvertible HTML near: {html[pos:pos + 80].strip()!r}") parts.append(block_to_md(m)) pos = m.end() return "\n\n".join(parts) + "\n" def restore(md, values): """Put {{key}} back where a value of site.json appears as text, longest values first.""" for key, value in sorted(values.items(), key=lambda kv: -len(kv[1])): if value: md = md.replace(value, "{{" + key + "}}") return md def mark_latest_news(html): """The news block of the home page becomes the {{latest_news}} placeholder.""" return re.sub(r'

    .*?\n?', "

    {{latest_news}}

    \n", html, count=1, flags=re.S) def mark_news_list(html): """The list of the news page becomes the {{news_list}} placeholder.""" return re.sub(r"", "

    {{news_list}}

    ", html, count=1, flags=re.S) # ---------------------------------------------------------------- pages and news def extract_content(page_html): """The part of a built page between the header and the footer.""" m = re.search(r"\n\n(.*)\n