Files
endorphin-tabularium-docs/scripts/export-legacy-documents.py
T
codex-bot d57e9b77d7
Verify database-backed documents / verify (pull_request) Failing after 6m6s
feat: make the site database authoritative for documents and catalog
2026-10-30 16:23:54 +08:00

49 lines
2.4 KiB
Python

"""One-time export from an explicit historical Git revision, never a runtime seed."""
import argparse
import json
import os
from pathlib import Path
import re
import subprocess
ROOT = Path(__file__).resolve().parents[1]
BASELINE = '25c5e83d08eae4277fe429a692955bd3bb2e717f' # Last main revision before database-backed documents.
def export(ref):
commit = subprocess.check_output(['git', 'rev-parse', '--verify', ref + '^{commit}'], cwd=ROOT, text=True).strip()
def read(name):
return subprocess.check_output(['git', 'show', f'{commit}:{name}'], cwd=ROOT).decode()
catalog = json.loads(read('content/catalog.json'))
names = subprocess.check_output(['git', 'ls-tree', '-r', '--name-only', commit, '--', 'content/docs'], cwd=ROOT, text=True).splitlines()
documents = []
for name in sorted(n for n in names if n.endswith('.md')):
source = name.removeprefix('content/docs/').removesuffix('.md')
metadata = catalog['documents'][source]
collection = next(c for c in catalog['collections'] if not c['path'] or source.startswith(c['path'] + '/'))
content = read(name)
title = re.search(r'^# (.+)$', content, re.M)
documents.append(dict(slug=metadata['slug'], sourceSlug=source, category=metadata['category'],
title=title[1].strip() if title else source, content=content,
group=metadata.get('group', collection['title']), collection=metadata.get('collection', collection['title']),
description=metadata.get('description', collection['description']),
draft=metadata.get('draft', False), featured=metadata.get('featured', False), revision=1))
if not documents:
raise ValueError('Refusing empty migration')
return dict(format='tabularium-documents-v1', documents=documents)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--ref', default=BASELINE)
parser.add_argument('--output', type=Path, required=True)
args = parser.parse_args()
payload = export(args.ref)
args.output.parent.mkdir(parents=True, exist_ok=True)
descriptor = os.open(args.output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(descriptor, 'w') as output:
json.dump(payload, output, ensure_ascii=False)
print(f"Exported {len(payload['documents'])} documents; source is immutable Git history")