123 lines
6.6 KiB
Python
123 lines
6.6 KiB
Python
#!/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<b),'Elias')
|
||
segment={'speaker':speaker,'text':part,'pauseMs':700 if part.endswith('\n\n') else 220}
|
||
if speaker in ('The borrowed voice','The echo') or (page_id=='midnight.feather.memory' and speaker=='Iris'):
|
||
segment['perspective']='behind-door'
|
||
scene = performance['scenes'][page_id]
|
||
segment['direction'] = scene.get(speaker, scene['Elias'])
|
||
action=actions.get(end)
|
||
if isinstance(action,str): segment.update(afterEffect=action,pauseMs=1350)
|
||
elif action: segment['pauseMs']=action
|
||
if part.strip() in ('I kept walking.','It did not strike.','The mouth closed.'):
|
||
segment['pauseMs']=1200
|
||
segments.append(segment)
|
||
# Keep short paragraphs together for connected acting and voice continuity.
|
||
# Explicit silences and Foley boundaries always remain separate takes.
|
||
joined=[]
|
||
for segment in segments:
|
||
previous=joined[-1] if joined else None
|
||
if (previous and previous['speaker']==segment['speaker']
|
||
and previous['direction']==segment['direction']
|
||
and previous.get('perspective')==segment.get('perspective')
|
||
and previous['pauseMs']<=700 and not previous.get('afterEffect')
|
||
and len((previous['text']+segment['text']).split())<=90):
|
||
previous['text']+=segment['text']
|
||
previous['pauseMs']=segment['pauseMs']
|
||
if segment.get('afterEffect'): previous['afterEffect']=segment['afterEffect']
|
||
else:
|
||
joined.append(segment.copy())
|
||
segments=joined
|
||
if page_id=='midnight.chair.memory':
|
||
for segment in segments:
|
||
if segment['speaker']=='Iris':
|
||
segment['direction'] = ('Her mother is about to leave forever. Let the name falter with grief; quiet and audible, no sobbing sounds.'
|
||
if '...' in segment['text'] else 'Gather courage and finish her mother\'s name clearly. Tender, grieving, determined; no sobbing sounds.')
|
||
assert ''.join(s['text'] for s in segments)==text
|
||
records.append({'pageId':page_id,'resource':page_id.replace('.','_'),'text':text,
|
||
'speaker':'Elias','production':'diary-qwen-v3','segments':segments})
|
||
write(PACKAGE/'narration/scripts.json',records)
|
||
library=read(ROOT/'assets/library.json')
|
||
story=next(s for s in library['stories'] if s['id']=='midnight-invitation')
|
||
story['description']='A postman’s Halloween diary: twelve missing guests, a stolen voice, and a letter that must never be delivered.'
|
||
story['paperAsset']='assets/stories/midnight-invitation/artwork/diary-page-v1.png'
|
||
write(ROOT/'assets/library.json',library)
|
||
print(f'Compiled {len(entries)} diary entries, {sum(len(t.split()) for _,t in entries.values())} words; puzzle identity preserved.')
|