|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Format a Jekyll term Markdown file by performing a full MD -> JSON -> MD roundtrip |
| 6 | +conversion, which normalizes the file's structure and front matter. |
| 7 | +
|
| 8 | +Requires: |
| 9 | +- md2json.py (must be in the same directory or accessible on the Python path) |
| 10 | +- json2md.py (must be in the same directory or accessible on the Python path) |
| 11 | +
|
| 12 | +Usage: |
| 13 | + python database/pyscripts/md_format.py -i _wiki/automatic-generation-control.md |
| 14 | +""" |
| 15 | + |
| 16 | +import argparse |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +# Assuming md2json.py and json2md.py are in the same directory |
| 21 | +# We import the main conversion functions directly. |
| 22 | +try: |
| 23 | + from md2json import build_json_from_md |
| 24 | + from json2md import convert_term_to_md |
| 25 | +except ImportError: |
| 26 | + print("ERROR: Could not import 'build_json_from_md' from md2json.py or 'convert_term_to_md' from json2md.py.", file=sys.stderr) |
| 27 | + print("Ensure these files are in the same directory as md_format.py.", file=sys.stderr) |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + |
| 31 | +def format_markdown_file(md_path: Path): |
| 32 | + """Performs the MD -> JSON -> MD roundtrip formatting.""" |
| 33 | + print(f"--- Processing {md_path.name} ---") |
| 34 | + |
| 35 | + # 1. MD -> JSON (in memory) |
| 36 | + try: |
| 37 | + # Note: We pass None for override_id to use the default title-derived ID |
| 38 | + term_json = build_json_from_md(md_path, override_id=None) |
| 39 | + print("✅ MD converted to internal JSON structure.") |
| 40 | + except Exception as e: |
| 41 | + print(f"❌ ERROR: Failed MD -> JSON conversion for {md_path.name}: {e}", file=sys.stderr) |
| 42 | + return |
| 43 | + |
| 44 | + # 2. Normalize fields for json2md's expectations |
| 45 | + # This block ensures fields like 'tags', 'related', 'authors', and 'dates' |
| 46 | + # are present and correctly structured, even if they were missing or None in the MD. |
| 47 | + term_json.setdefault("tags", []) |
| 48 | + term_json.setdefault("related", []) |
| 49 | + term_json.setdefault("authors", []) |
| 50 | + # Dates are already derived in build_json_from_md, but we ensure the structure exists |
| 51 | + if not term_json.get("dates"): |
| 52 | + # This is unlikely to happen with the current md2json logic, but safe to include |
| 53 | + term_json["dates"] = {} |
| 54 | + |
| 55 | + # 3. JSON -> MD (formatted string) |
| 56 | + try: |
| 57 | + # We need a temporary structure to simulate derive_file_dates, |
| 58 | + # but since we are writing back to the *same* file, we use the original path. |
| 59 | + # json2md's convert_term_to_md doesn't rely on file system stats, only the CLI does. |
| 60 | + formatted_md = convert_term_to_md(term_json) |
| 61 | + print("✅ JSON structure converted back to formatted MD string.") |
| 62 | + except Exception as e: |
| 63 | + print(f"❌ ERROR: Failed JSON -> MD conversion for {md_path.name}: {e}", file=sys.stderr) |
| 64 | + return |
| 65 | + |
| 66 | + # 4. Overwrite original MD file |
| 67 | + try: |
| 68 | + md_path.write_text(formatted_md, encoding="utf-8") |
| 69 | + print(f"🎉 Successfully wrote standardized format to: {md_path}") |
| 70 | + except Exception as e: |
| 71 | + print(f"❌ ERROR: Failed to write output file {md_path.name}: {e}", file=sys.stderr) |
| 72 | + return |
| 73 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + ap = argparse.ArgumentParser( |
| 77 | + description="Format a Jekyll term Markdown file via MD -> JSON -> MD roundtrip." |
| 78 | + ) |
| 79 | + ap.add_argument("-i", "--input", required=True, help="Path to the term Markdown file to format.") |
| 80 | + args = ap.parse_args() |
| 81 | + |
| 82 | + md_path = Path(args.input) |
| 83 | + |
| 84 | + if not md_path.exists(): |
| 85 | + print(f"ERROR: Markdown file not found: {md_path}", file=sys.stderr) |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + format_markdown_file(md_path) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + main() |
0 commit comments