File size: 1,761 Bytes
93bbf17 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #!/usr/bin/env python3
"""Convert the Riverstone English/ language pack to JSON.
Usage:
python convert.py # English/ -> English_JSON/
python convert.py SRC DST # custom source / destination dirs
"""
import json
import sys
from pathlib import Path
from converter.utils import is_skippable, toSafeEntityName
from converter.docs import readLocalDoc
from converter.sheets import readLocalSheet
def _file_path_key(rel: Path) -> str:
"""Compute the normalised path key used by splitMarkers / avoidTransformMarkers."""
parts = [toSafeEntityName(p) for p in rel.with_suffix("").parts]
return "/".join(parts)
def main() -> None:
base = Path(__file__).resolve().parent
src_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else base / "English"
dst_dir = Path(sys.argv[2]) if len(sys.argv) > 2 else base / "English_JSON"
n_docx = n_xlsx = n_skipped = 0
for path in sorted(src_dir.rglob("*")):
if not path.is_file():
continue
ext = path.suffix.lower()
if path.name.startswith("~$") or ext not in (".docx", ".xlsx"):
continue
if is_skippable(path.name):
n_skipped += 1
continue
rel = path.relative_to(src_dir)
file_path = _file_path_key(rel)
if ext == ".docx":
data = readLocalDoc(path, file_path)
n_docx += 1
else:
data = readLocalSheet(path, file_path)
n_xlsx += 1
out_path = dst_dir / rel.with_suffix(".json")
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
total = n_docx + n_xlsx
print(f"Converted {n_docx} docx + {n_xlsx} xlsx = {total} files ({n_skipped} skipped)")
print(f"Output: {dst_dir}")
if __name__ == "__main__":
main()
|