#!/usr/bin/env python3 """Compile the editable diary manuscript into stable story pages and cast scripts. Puzzle grids, clues, IDs, progress keys and artwork associations are preserved. After running, render narration and run the story package checks before building. """ import json import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PACKAGE = ROOT / 'assets/stories/midnight-invitation' read = lambda p: json.loads(p.read_text()) write = lambda p, data: p.write_text(json.dumps(data, ensure_ascii=False, indent=2) + '\n') parts = re.split(r'^## (\S+) \| (.+)\n\n', (PACKAGE/'STORY.md').read_text(), flags=re.M) entries = {parts[i]: (parts[i+1], parts[i+2].strip()) for i in range(1,len(parts),3)} campaign = read(PACKAGE/'campaign.json') notebook = read(PACKAGE/'notebook.json') assert set(entries) == {p['id'] for p in notebook['pages'] if p['type'] != 'puzzle'} chapters = ['I · The road that would not end', 'II · The guest beneath the floor', 'III · Return to sender'] campaign['premise'] = 'The private diary of Elias Ward, a Blackthorn postman. On Halloween he carried a letter in his own handwriting to a house with twelve missing guests and a thirteenth place waiting for him. A complete Gothic mystery in three chapters.' for i, level in enumerate(campaign['levels']): prefix = 'midnight.' + level['id'] level['title'], level['before'] = entries[prefix+'.entry'] level['memoryTitle'], level['after'] = entries[prefix+'.memory'] level['chapter'] = chapters[i//4] for page in notebook['pages']: if page['id'] in entries: page['title'], text = entries[page['id']] if page.get('textSource') not in ('before','after'): page['text'] = text page['speaker'] = 'Elias Ward · private delivery diary' else: page['title'] = campaign['levels'][page['campaignIndex']]['title'] if 'caption' in page: if page['id'] == 'midnight.opening': page['caption'] = 'Blackthorn. An account written after the Halloween delivery.' else: page['caption'] = chapters[page['campaignIndex']//4] write(PACKAGE/'campaign.json',campaign) write(PACKAGE/'notebook.json',notebook) cast = read(PACKAGE/'narration/cast.json') performance = read(PACKAGE/'narration/performance.json') assert set(performance['scenes']) == set(entries), 'Every scene needs performance direction' roles = { 'midnight.opening':['Ada'], 'midnight.feather.memory':['Iris','Elias','Iris','Iris'], 'midnight.key.entry':['The borrowed voice','Iris'], 'midnight.needle.memory':['Iris','Iris','The echo'], } effects = { 'midnight.feather.memory':{'At the door I knocked twice.':'door-knock', 'A chair scraped over the floor on the other side.':'chair-shift'}, 'midnight.candle.entry':{'I spread the paper beneath the lamp.':'paper'}, 'midnight.ending':{'A few minutes ago, the drawer knocked twice.':'door-knock'}, } records=[] for page_id,(title,text) in entries.items(): quotes=list(re.finditer('“[^”]+”',text)) speakers=roles.get(page_id,['Iris']*len(quotes)) assert len(quotes)==len(speakers),(page_id,len(quotes),speakers) spans=[(m.start(),m.end(),speaker) for m,speaker in zip(quotes,speakers)] cuts={0,len(text)} cuts.update(m.end() for m in re.finditer('\n\n',text)) for a,b,_ in spans: cuts.update((a,b)) actions={} for phrase,name in effects.get(page_id,{}).items(): assert phrase in text end=text.index(phrase)+len(phrase) cuts.add(end);actions[end]=name if page_id=='midnight.feather.memory': end=text.index('Silence.')+len('Silence.');cuts.add(end);actions[end]=1500 segments=[] bounds=sorted(cuts) for start,end in zip(bounds,bounds[1:]): part=text[start:end] if not part.strip(): segments[-1]['text']+=part segments[-1]['pauseMs']=max(segments[-1]['pauseMs'],700) continue speaker=next((s for a,b,s in spans if a<=start