From 11f7db1b1cd314021475c723b6ebb59ee7756e41 Mon Sep 17 00:00:00 2001 From: MikeHughes-BIN Date: Mon, 12 Jan 2026 19:07:42 +0100 Subject: [PATCH] speaker-renaming module created --- .../modules/replace_speaker/replaceSpeaker.js | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 services/modules/replace_speaker/replaceSpeaker.js diff --git a/services/modules/replace_speaker/replaceSpeaker.js b/services/modules/replace_speaker/replaceSpeaker.js new file mode 100644 index 0000000..34a0e44 --- /dev/null +++ b/services/modules/replace_speaker/replaceSpeaker.js @@ -0,0 +1,70 @@ +const fs = require('fs'); +const path = require('path'); + +const module_exports = { + name: "replace_speaker", + type: "processor", + displayname: "Speaker Name Replacer", + description: "Replaces speaker placeholder names with actual names based on a mapping in HTML files", + + async function({ inputHtmlPath, speakerMappingPath }) { + return await this.replaceNames(inputHtmlPath, speakerMappingPath); + }, + + replaceNames: async function(inputHtmlPath, speakerMappingPath) { + try { + const htmlContent = await fs.promises.readFile(inputHtmlPath, "utf-8"); + const mappingData = await fs.promises.readFile(speakerMappingPath, "utf-8"); + + let speakerMap = {}; + try { + const parsed = JSON.parse(mappingData); + if (parsed.speakerId && parsed.speakerName) { + speakerMap[parsed.speakerId] = parsed.speakerName; + } else { + Object.assign(speakerMap, parsed); + } + } catch (e) { + const lines = mappingData.trim().split('\n'); + lines.forEach(line => { + const [placeholder, realName] = line.split(',').map(s => s.trim()); + if (placeholder && realName) speakerMap[placeholder] = realName; + }); + } + + let outputContent = htmlContent; + Object.entries(speakerMap).forEach(([placeholder, realName]) => { + const regex = new RegExp(`[\$begin:math:text$\\\\\[\]\?\$\{placeholder\}\[\\$end:math:text$\\]]?`, 'g'); + outputContent = outputContent.replace(regex, realName); + }); + + await fs.promises.writeFile(inputHtmlPath, outputContent, "utf-8"); + + return inputHtmlPath; + + } catch (error) { + console.error("Error replacing speaker names:", error); + throw error; + } + } +}; + +module.exports = module_exports; + +if (require.main === module) { + (async () => { + const args = process.argv.slice(2); + if (args.length < 2) process.exit(1); + + const [inputHtmlPath, speakerMappingPath] = args; + + if (!fs.existsSync(inputHtmlPath)) process.exit(1); + if (!fs.existsSync(speakerMappingPath)) process.exit(1); + + try { + await module_exports.replaceNames(inputHtmlPath, speakerMappingPath); + } catch (err) { + process.exit(1); + } + })(); +} \ No newline at end of file