Created
May 14, 2020 18:55
-
-
Save qgustavor/a1359f474ec388dd2bc5e7368a132375 to your computer and use it in GitHub Desktop.
Replace text in ASS subtitles ignoring formatting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function replaceAssText (source, replacerFn) { | |
// Detect all non-dialogue tokens and mark those with null characters | |
const lines = source.replace(/\0/g, '').split('\n') | |
const nonReplaceableTokens = [] | |
const replaceableTokens = [] | |
let nonTextAccumulator = '' | |
for (let line of lines) { | |
if (!line.startsWith('Dialogue: ')) { | |
nonTextAccumulator += line + '\n' | |
continue | |
} | |
const lineParts = line.split(',') | |
const nonTextLine = lineParts.slice(0, 9).join(',') | |
nonTextAccumulator += nonTextLine + ',' | |
nonReplaceableTokens.push(nonTextAccumulator) | |
nonTextAccumulator = '' | |
const dialogueText = lineParts.slice(9).join(',') | |
const nonTextTokens = dialogueText.match(/\{.*?\}|\\N/g) | |
const textTokens = dialogueText.split(/\{.*?\}|\\N/g) | |
if (nonTextTokens) { | |
for (let token of textTokens) replaceableTokens.push(token) | |
for (let token of nonTextTokens) nonReplaceableTokens.push(token) | |
} else { | |
replaceableTokens.push(dialogueText) | |
} | |
replaceableTokens[replaceableTokens.length - 1] += '\n' | |
} | |
nonReplaceableTokens.push(nonTextAccumulator) | |
replaceableText = '\0' + replaceableTokens.join('\0') + '\0' | |
// Replace text with null characters using the replacer function | |
replaceableText = replacerFn(replaceableText) | |
// Place back the non-dialogue tokens | |
let replaceIndex = 0 | |
return replaceableText.replace(/\0/g, () => nonReplaceableTokens[replaceIndex++]) | |
} | |
// Function usage | |
const result = replaceAssText(assText, (text) => text.replace(/"([\s\S]*?)"/g, '“$1”')) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important: the replacer function must not add or remove null characters.