(text)
| 119 | } |
| 120 | |
| 121 | export function parseScript(text) { |
| 122 | const out = []; |
| 123 | const src = String(text || '') |
| 124 | .replace(/\r\n/g, '\n') |
| 125 | .trim(); |
| 126 | if (!src) return out; |
| 127 | |
| 128 | // Tagged scripts (`[Alice] …`) are unambiguous — handle them first so a |
| 129 | // pasted podcast/audiobook script auto-casts without any format toggle (#487). |
| 130 | if (hasSpeakerTags(src)) return parseTaggedScript(src); |
| 131 | |
| 132 | const paras = src |
| 133 | .split(/\n\s*\n/) |
| 134 | .flatMap((p) => p.split(/\n/)) |
| 135 | .map((s) => s.trim()) |
| 136 | .filter(Boolean); |
| 137 | |
| 138 | for (const para of paras) { |
| 139 | // 1. Screenplay "NAME: dialogue" (avoid matching URLs like http://…) |
| 140 | const sp = para.match(/^([A-Za-z][A-Za-z0-9 ._'-]{0,30}):\s+(.+)$/); |
| 141 | if (sp && !/^https?$/i.test(sp[1].trim())) { |
| 142 | out.push({ speaker: normalizeSpeaker(sp[1]), text: sp[2].trim() }); |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | // 2. Prose with quoted dialogue (straight + curly double quotes) |
| 147 | const quoteRe = /["“„]([^"“”„]+)["”]/g; |
| 148 | let last = 0; |
| 149 | let m; |
| 150 | let found = false; |
| 151 | const segs = []; |
| 152 | while ((m = quoteRe.exec(para)) !== null) { |
| 153 | found = true; |
| 154 | const before = para.slice(last, m.index); |
| 155 | const quote = (m[1] || '').trim(); |
| 156 | const after = para.slice(quoteRe.lastIndex); |
| 157 | if (before.trim()) segs.push({ speaker: 'Narrator', text: before.trim() }); |
| 158 | const name = attributionName(before, after); |
| 159 | if (quote) segs.push({ speaker: name ? normalizeSpeaker(name) : 'Narrator', text: quote }); |
| 160 | last = quoteRe.lastIndex; |
| 161 | } |
| 162 | if (!found) { |
| 163 | out.push({ speaker: 'Narrator', text: para }); |
| 164 | continue; |
| 165 | } |
| 166 | const tail = para.slice(last).trim(); |
| 167 | if (tail) segs.push({ speaker: 'Narrator', text: tail }); |
| 168 | out.push(...segs.filter((s) => s.text)); |
| 169 | } |
| 170 | return out; |
| 171 | } |
no test coverage detected