from proofs import process as process_proofs
from status import process as process_status
from paths import process as process_paths
from margin import process as process_margin
import json
import sys
def process(content: str) -> str:
content = process_proofs(content)
content = process_margin(content)
content = process_status(content)
content = process_paths(content)
return content
def process_chapter(chapter):
if isinstance(chapter, dict) and 'Chapter' in chapter:
ch = chapter['Chapter']
else:
ch = chapter
if isinstance(ch, dict) and 'content' in ch and ch['content'] is not None:
ch['content'] = process(ch.get('content', ''))
for sub_item in ch.get('sub_items', []) if isinstance(ch, dict) else []:
process_chapter(sub_item)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'supports':
sys.exit(0)
context, book = json.load(sys.stdin)
if isinstance(book, dict) and 'sections' in book:
sections = book['sections']
elif isinstance(book, dict) and 'book' in book and isinstance(book['book'], dict) and 'sections' in book['book']:
sections = book['book']['sections']
elif isinstance(book, dict) and 'items' in book:
sections = book['items']
else:
print(f"preprocessor: unexpected book keys: {list(book.keys())}", file=sys.stderr)
json.dump(book, sys.stdout)
sys.exit(0)
for section in sections:
process_chapter(section)
json.dump(book, sys.stdout)