Files
novelmaster/noma/scripts/extract_chapter_context.py
T
2026-06-23 20:29:02 +08:00

61 lines
1.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Extract Chapter Context
Extracts context from chapters for various processing tasks.
"""
import argparse
import json
import sys
from pathlib import Path
from typing import Optional
try:
from runtime_compat import enable_windows_utf8_stdio
except ImportError:
enable_windows_utf8_stdio = lambda: None
def main():
if __name__ == "__main__":
enable_windows_utf8_stdio()
parser = argparse.ArgumentParser(description="Extract chapter context")
parser.add_argument("--chapter", type=str, required=True, help="Chapter file path")
parser.add_argument("--project-root", type=str, default=".", help="Project root")
parser.add_argument("--output", type=str, help="Output file path")
args = parser.parse_args()
chapter_path = Path(args.chapter)
if not chapter_path.exists():
print(f"Error: Chapter file not found: {chapter_path}", file=sys.stderr)
sys.exit(1)
try:
content = chapter_path.read_text(encoding="utf-8")
# Simple extraction - just return the first N characters
result = {
"chapter": str(chapter_path),
"length": len(content),
"preview": content[:500] if len(content) > 500 else content
}
if args.output:
output_path = Path(args.output)
output_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
print(f"Context extracted to: {output_path}")
else:
print(json.dumps(result, ensure_ascii=False, indent=2))
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()