import re,json,hashlib from pathlib import Path B=Path('plans/extract-remaining-elements') batch=json.loads((B/'batch_2.json').read_text()) def subsections(t): ms=list(re.finditer(r'(?m)^(#{2,3}) (?:§)?(\d+(?:\.(?:P\d+|\d+))?)\.?[^\n]*',t)); out={} for i,m in enumerate(ms): out[m[2]]=(m[0],t[m.end():ms[i+1].start() if i+1|)]+','',t) # Source sections remain locators even when not carried over. t=re.sub(r'(?') o=Path(x['output']);o.parent.mkdir(exist_ok=True) if o.exists():raise RuntimeError('output already exists '+str(o)) o.write_text('\n\n'.join(chunks).strip()+'\n') print('DRAFTS',len(batch))