mirror of
https://gitlab.rlp.net/proj-wise2526-video2document/video2document.git
synced 2026-06-15 18:01:52 +02:00
Refactor speaker management and replace functionality; add IPC for saving speaker mappings and update HTML structure
This commit is contained in:
Vendored
BIN
Binary file not shown.
@@ -19,7 +19,7 @@
|
|||||||
</label>
|
</label>
|
||||||
|
|
||||||
<nav class="menu1">
|
<nav class="menu1">
|
||||||
<a href="custom_document.html" class="li1">Custom document</a>
|
<a href="custom_document.html" class="li1">Manage document types</a>
|
||||||
<a href="" class="li1">Help</a>
|
<a href="" class="li1">Help</a>
|
||||||
</nav>
|
</nav>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
+19
-15
@@ -3,35 +3,39 @@ const { contextBridge, ipcRenderer, webUtils } = require('electron')
|
|||||||
try {
|
try {
|
||||||
contextBridge.exposeInMainWorld("explorer", {
|
contextBridge.exposeInMainWorld("explorer", {
|
||||||
onFileDrop: (file) => webUtils.getPathForFile(file)
|
onFileDrop: (file) => webUtils.getPathForFile(file)
|
||||||
})
|
});
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld("submit", {
|
contextBridge.exposeInMainWorld("submit", {
|
||||||
submit: (meeting_specifications) => {ipcRenderer.send("file_submit", meeting_specifications)}
|
submit: (meeting_specifications) => ipcRenderer.send("file_submit", meeting_specifications)
|
||||||
})
|
});
|
||||||
|
|
||||||
|
// ALLE electronAPI Funktionen in EINEM Objekt
|
||||||
contextBridge.exposeInMainWorld("electronAPI", {
|
contextBridge.exposeInMainWorld("electronAPI", {
|
||||||
getFilePath: (file) => {return webUtils.getPathForFile(file)}
|
getFilePath: (file) => webUtils.getPathForFile(file),
|
||||||
})
|
saveSpeakerMapping: (data) => ipcRenderer.send("save-speaker-mapping", data)
|
||||||
|
});
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld("onStartup", {
|
contextBridge.exposeInMainWorld("onStartup", {
|
||||||
getModuleNames: () => ipcRenderer.invoke('get-module-names')
|
getModuleNames: () => ipcRenderer.invoke('get-module-names')
|
||||||
})
|
});
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld('electron', {
|
contextBridge.exposeInMainWorld('electron', {
|
||||||
progress: (callback) => ipcRenderer.on('progress', callback)
|
progress: (callback) => ipcRenderer.on('progress', callback)
|
||||||
})
|
});
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld('audios', {
|
contextBridge.exposeInMainWorld('audios', {
|
||||||
speakerAudios: (callback) => ipcRenderer.on('speakerAudios', callback)
|
speakerAudios: (callback) => ipcRenderer.on('speakerAudios', callback)
|
||||||
})
|
});
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld("submitSpeaker", {
|
contextBridge.exposeInMainWorld("submitSpeaker", {
|
||||||
speaker_submit: (speaker_names) => {ipcRenderer.send("speaker_submit", speaker_names)}
|
speaker_submit: (speaker_names) => ipcRenderer.send("speaker_submit", speaker_names)
|
||||||
})
|
});
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld("download", {
|
contextBridge.exposeInMainWorld("download", {
|
||||||
file_download: () => {ipcRenderer.send("file_download")}
|
file_download: () => ipcRenderer.send("file_download")
|
||||||
})
|
});
|
||||||
|
|
||||||
|
ipcRenderer.on("error", (event, err) => { alert(err) });
|
||||||
ipcRenderer.on("error", (event, err) => {alert(err)})
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Error in preload.js");
|
console.log("Error in preload.js", error);
|
||||||
}
|
}
|
||||||
+12
-7
@@ -315,11 +315,20 @@ function setSpeakerAudiosValue(valy){
|
|||||||
|
|
||||||
function rewriteSpeakerName() {
|
function rewriteSpeakerName() {
|
||||||
try {
|
try {
|
||||||
var tempy = document.getElementById("cur_speaker").value;
|
const oldKey = document.getElementById("cur_speaker").value;
|
||||||
speakerAudios[tempy].name = document.getElementById("newSpeaker").value;
|
const newName = document.getElementById("newSpeaker").value;
|
||||||
|
|
||||||
|
speakerAudios[oldKey].name = newName;
|
||||||
loadSpeakerOptions(speakerAudios);
|
loadSpeakerOptions(speakerAudios);
|
||||||
|
|
||||||
|
// IPC-Aufruf an Electron main process
|
||||||
|
window.electronAPI.saveSpeakerMapping({
|
||||||
|
speakerId: oldKey,
|
||||||
|
speakerName: newName
|
||||||
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("\n\n\n" + error + "\n\n\n")
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,7 +347,3 @@ function fileDownload() {
|
|||||||
console.error("Download failed:", error);
|
console.error("Download failed:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function rewriteSpeakerName(){
|
|
||||||
console.log("Rewriting speaker name...");
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -103,7 +103,23 @@ electron.ipcMain.handle('get-module-names', async () => {
|
|||||||
return module_array
|
return module_array
|
||||||
});
|
});
|
||||||
|
|
||||||
|
electron.ipcMain.on("save-speaker-mapping", (event, data) => {
|
||||||
|
|
||||||
|
const filePath = "/Users/mikehughes/PROJ/video2document/storage/speakerMapping/speakerMapping.json";
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
speakerId: data.speakerId,
|
||||||
|
speakerName: data.speakerName,
|
||||||
|
updated: new Date().toISOString()
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), "utf8");
|
||||||
|
console.log("Speaker mapping saved!");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to save speaker mapping", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
// electron.ipcMain.on("get_modules", async (event, args) => {
|
// electron.ipcMain.on("get_modules", async (event, args) => {
|
||||||
// let module_array = {
|
// let module_array = {
|
||||||
// "ai_modules":[],
|
// "ai_modules":[],
|
||||||
|
|||||||
@@ -1,120 +1,70 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const outputDir = path.join(__dirname, "../../../storage/documents"); // path for output directory
|
|
||||||
|
|
||||||
if (!fs.existsSync(outputDir)) {
|
|
||||||
fs.mkdirSync(outputDir, { recursive: true }); // Create output directory if it doesn't exist
|
|
||||||
}
|
|
||||||
|
|
||||||
const module_exports = {
|
const module_exports = {
|
||||||
name: "replace_speaker",
|
name: "replace_speaker",
|
||||||
type: "processor",
|
type: "processor",
|
||||||
displayname: "Speaker Name Replacer",
|
displayname: "Speaker Name Replacer",
|
||||||
description: "Replaces speaker placeholder names with actual names based on a mapping in HTML files",
|
description: "Replaces speaker placeholder names with actual names based on a mapping in HTML files",
|
||||||
|
|
||||||
async function(parameter) {
|
async function({ inputHtmlPath, speakerMappingPath }) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return await this.replaceNames(inputHtmlPath, speakerMappingPath);
|
||||||
try {
|
|
||||||
// console.log("Speaker replacer module invoked with parameters:", parameter);
|
|
||||||
|
|
||||||
resolve(await this.replaceNames(
|
|
||||||
parameter.inputHtmlPath, // Path to input HTML file
|
|
||||||
parameter.speakerMappingPath // Path to speaker mapping file (JSON)
|
|
||||||
));
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
// console.error("Error in speaker replacer module:", error);
|
|
||||||
reject(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
|
|
||||||
replaceNames: async function(inputHtmlPath, speakerMappingPath) {
|
replaceNames: async function(inputHtmlPath, speakerMappingPath) {
|
||||||
return new Promise(async(resolve, reject) => {
|
|
||||||
try {
|
try {
|
||||||
const htmlContent = await fs.promises.readFile(inputHtmlPath, "utf-8"); // read HTML file
|
const htmlContent = await fs.promises.readFile(inputHtmlPath, "utf-8");
|
||||||
const mappingData = await fs.promises.readFile(speakerMappingPath, "utf-8"); // read mapping file
|
const mappingData = await fs.promises.readFile(speakerMappingPath, "utf-8");
|
||||||
|
|
||||||
// Parse mapping - supports JSON or simple format
|
|
||||||
let speakerMap = {};
|
let speakerMap = {};
|
||||||
try {
|
try {
|
||||||
speakerMap = JSON.parse(mappingData); // Try to parse as JSON
|
const parsed = JSON.parse(mappingData);
|
||||||
|
if (parsed.speakerId && parsed.speakerName) {
|
||||||
|
speakerMap[parsed.speakerId] = parsed.speakerName;
|
||||||
|
} else {
|
||||||
|
Object.assign(speakerMap, parsed);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// If not JSON, try simple format: "Speaker A,Mike\nSpeaker B,Stefan"
|
|
||||||
const lines = mappingData.trim().split('\n');
|
const lines = mappingData.trim().split('\n');
|
||||||
lines.forEach(line => {
|
lines.forEach(line => {
|
||||||
const [placeholder, realName] = line.split(',').map(s => s.trim());
|
const [placeholder, realName] = line.split(',').map(s => s.trim());
|
||||||
if (placeholder && realName) {
|
if (placeholder && realName) speakerMap[placeholder] = realName;
|
||||||
speakerMap[placeholder] = realName;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace all speaker names in HTML content
|
|
||||||
let outputContent = htmlContent;
|
let outputContent = htmlContent;
|
||||||
Object.entries(speakerMap).forEach(([placeholder, realName]) => {
|
Object.entries(speakerMap).forEach(([placeholder, realName]) => {
|
||||||
// Create regex to replace all occurrences (case-sensitive)
|
const regex = new RegExp(`[\$begin:math:text$\\\\\[\]\?\$\{placeholder\}\[\\$end:math:text$\\]]?`, 'g');
|
||||||
const regex = new RegExp(`\\b${placeholder}\\b`, 'g');
|
|
||||||
outputContent = outputContent.replace(regex, realName);
|
outputContent = outputContent.replace(regex, realName);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Generate output file path based on input file name
|
await fs.promises.writeFile(inputHtmlPath, outputContent, "utf-8");
|
||||||
const inputFileName = path.basename(inputHtmlPath, path.extname(inputHtmlPath));
|
|
||||||
const outPath = path.join(outputDir, `${inputFileName}_replaced.html`);
|
|
||||||
|
|
||||||
// Write output to file
|
return inputHtmlPath;
|
||||||
fs.writeFileSync(outPath, outputContent, "utf8");
|
|
||||||
|
|
||||||
// console.log("Replaced HTML file written to:", outPath);
|
|
||||||
resolve(outPath)
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.error("Error replacing speaker names:", error);
|
console.error("Error replacing speaker names:", error);
|
||||||
reject(error)
|
throw error;
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = module_exports;
|
module.exports = module_exports;
|
||||||
|
|
||||||
// CLI Mode: Allow direct execution
|
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
(async () => {
|
(async () => {
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
|
if (args.length < 2) process.exit(1);
|
||||||
if (args.length < 2) {
|
|
||||||
console.error("Usage: node string-replacer.js <inputHtmlPath> <speakerMappingPath>");
|
|
||||||
console.error("Example: node string-replacer.js ./document.html ./speaker_mapping.json");
|
|
||||||
console.error("\nMapping file formats:");
|
|
||||||
console.error("JSON: {\"Speaker A\": \"Mike\", \"Speaker B\": \"Stefan\"}");
|
|
||||||
console.error("or simple: Speaker A,Mike\\nSpeaker B,Stefan");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [inputHtmlPath, speakerMappingPath] = args;
|
const [inputHtmlPath, speakerMappingPath] = args;
|
||||||
|
|
||||||
// Check if files exist
|
if (!fs.existsSync(inputHtmlPath)) process.exit(1);
|
||||||
if (!fs.existsSync(inputHtmlPath)) {
|
if (!fs.existsSync(speakerMappingPath)) process.exit(1);
|
||||||
console.error(`ERROR: HTML file not found: ${inputHtmlPath}`);
|
|
||||||
|
try {
|
||||||
|
await module_exports.replaceNames(inputHtmlPath, speakerMappingPath);
|
||||||
|
} catch (err) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(speakerMappingPath)) {
|
|
||||||
console.error(`ERROR: Speaker mapping file not found: ${speakerMappingPath}`);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Starting speaker name replacement...");
|
|
||||||
console.log(`HTML file: ${inputHtmlPath}`);
|
|
||||||
console.log(`Mapping file: ${speakerMappingPath}`);
|
|
||||||
|
|
||||||
await module_exports.replaceNames(
|
|
||||||
inputHtmlPath,
|
|
||||||
speakerMappingPath
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log("Done!");
|
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"speakerId": "speakerB",
|
||||||
|
"speakerName": "Peter",
|
||||||
|
"updated": "2026-01-10T13:54:55.608Z"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user