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>
|
||||||
|
|||||||
+20
-16
@@ -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);
|
||||||
}
|
}
|
||||||
+56
-51
@@ -5,8 +5,8 @@ function checkBoxes() {
|
|||||||
const checkboxes = document.querySelectorAll('input[name="docFormat"]');
|
const checkboxes = document.querySelectorAll('input[name="docFormat"]');
|
||||||
let isChecked = false;
|
let isChecked = false;
|
||||||
var checkedCounter = 0;
|
var checkedCounter = 0;
|
||||||
checkboxes.forEach(function(checkbox){
|
checkboxes.forEach(function (checkbox) {
|
||||||
if(checkbox.checked){
|
if (checkbox.checked) {
|
||||||
isChecked = true;
|
isChecked = true;
|
||||||
checkedCounter++;
|
checkedCounter++;
|
||||||
}
|
}
|
||||||
@@ -15,24 +15,24 @@ function checkBoxes() {
|
|||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
//Code to submit the video
|
//Code to submit the video
|
||||||
var selectedCheckboxes = {};
|
var selectedCheckboxes = {};
|
||||||
checkboxes.forEach(function(checkbox){
|
checkboxes.forEach(function (checkbox) {
|
||||||
if(checkbox.checked){
|
if (checkbox.checked) {
|
||||||
selectedCheckboxes[checkbox.nextElementSibling.textContent] = "";
|
selectedCheckboxes[checkbox.nextElementSibling.textContent] = "";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const testEndings = [".mp4", ".mov", ".avi", ".mkv"];
|
const testEndings = [".mp4", ".mov", ".avi", ".mkv"];
|
||||||
var pathTest = window.electronAPI.getFilePath(videoUpload.files[0]);
|
var pathTest = window.electronAPI.getFilePath(videoUpload.files[0]);
|
||||||
var pathToLower = pathTest.toLowerCase();
|
var pathToLower = pathTest.toLowerCase();
|
||||||
if(testEndings.some(e => pathToLower.endsWith(e))){
|
if (testEndings.some(e => pathToLower.endsWith(e))) {
|
||||||
//assembly of the json for the main
|
//assembly of the json for the main
|
||||||
|
|
||||||
var typeCheckbox;
|
var typeCheckbox;
|
||||||
if(document.getElementById("docFormat").checked) typeCheckbox = document.getElementById("docFormat").value;
|
if (document.getElementById("docFormat").checked) typeCheckbox = document.getElementById("docFormat").value;
|
||||||
if(document.getElementById("docFormatSummary1").checked) typeCheckbox = document.getElementById("docFormatSummary1").value;
|
if (document.getElementById("docFormatSummary1").checked) typeCheckbox = document.getElementById("docFormatSummary1").value;
|
||||||
if(document.getElementById("docFormatSummary2").checked) typeCheckbox = document.getElementById("docFormatSummary2").value;
|
if (document.getElementById("docFormatSummary2").checked) typeCheckbox = document.getElementById("docFormatSummary2").value;
|
||||||
if(document.getElementById("docFormatSummary3").checked) typeCheckbox = document.getElementById("docFormatSummary3").value;
|
if (document.getElementById("docFormatSummary3").checked) typeCheckbox = document.getElementById("docFormatSummary3").value;
|
||||||
if(document.getElementById("docFormatCustom").checked) typeCheckbox = document.getElementById("docFormatCustom").value;
|
if (document.getElementById("docFormatCustom").checked) typeCheckbox = document.getElementById("docFormatCustom").value;
|
||||||
|
|
||||||
document.getElementById("testy").style.visibility = "visible"
|
document.getElementById("testy").style.visibility = "visible"
|
||||||
document.getElementById("box1").style.backgroundColor = "red";
|
document.getElementById("box1").style.backgroundColor = "red";
|
||||||
@@ -45,20 +45,20 @@ function checkBoxes() {
|
|||||||
const aiType = document.getElementById("ai_type");
|
const aiType = document.getElementById("ai_type");
|
||||||
const sendingPackage = {
|
const sendingPackage = {
|
||||||
"video": {
|
"video": {
|
||||||
"module":"extraction-video-to-audio",
|
"module": "extraction-video-to-audio",
|
||||||
"inputVideoPath": pathTest
|
"inputVideoPath": pathTest
|
||||||
},
|
},
|
||||||
"transcription": {
|
"transcription": {
|
||||||
"module": transcriptionType.value
|
"module": transcriptionType.value
|
||||||
},
|
},
|
||||||
"document": {
|
"document": {
|
||||||
"module":aiType.value,
|
"module": aiType.value,
|
||||||
"type": typeCheckbox,
|
"type": typeCheckbox,
|
||||||
"outputType": outputType.value
|
"outputType": outputType.value
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
window.submit.submit(sendingPackage)
|
window.submit.submit(sendingPackage)
|
||||||
}else{
|
} else {
|
||||||
alert('The given file is not compatible. These are the available types: [".mp4", ".mov", ".avi", ".mkv"].');
|
alert('The given file is not compatible. These are the available types: [".mp4", ".mov", ".avi", ".mkv"].');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ function checkBoxes() {
|
|||||||
console.log("Error in script.js checkBoxes function");
|
console.log("Error in script.js checkBoxes function");
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//language changing feature
|
//language changing feature
|
||||||
@@ -95,7 +95,7 @@ function changeLanguage(language) {
|
|||||||
console.log("Error in script.js changeLanguage function");
|
console.log("Error in script.js changeLanguage function");
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//function to display the file path in the drop down box
|
//function to display the file path in the drop down box
|
||||||
@@ -129,17 +129,17 @@ function updateProgressBar(bar, value) {
|
|||||||
console.log("Error in scripts.js updateProgressBar function");
|
console.log("Error in scripts.js updateProgressBar function");
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//function to load ai options to the drop down list
|
//function to load ai options to the drop down list
|
||||||
function loadAiOptions(options){
|
function loadAiOptions(options) {
|
||||||
try {
|
try {
|
||||||
var menu = document.getElementById('ai_type');
|
var menu = document.getElementById('ai_type');
|
||||||
var object_holdy;
|
var object_holdy;
|
||||||
var choice ;
|
var choice;
|
||||||
object_holdy = options
|
object_holdy = options
|
||||||
for(i = 0; i < options.length; i++){
|
for (i = 0; i < options.length; i++) {
|
||||||
choice = document.createElement('option');
|
choice = document.createElement('option');
|
||||||
choice.textContent = object_holdy[i].displayname;
|
choice.textContent = object_holdy[i].displayname;
|
||||||
choice.value = object_holdy[i].name;
|
choice.value = object_holdy[i].name;
|
||||||
@@ -152,13 +152,13 @@ function loadAiOptions(options){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//function to load transcription options to the drop down list
|
//function to load transcription options to the drop down list
|
||||||
function loadTranscriptionOptions(options){
|
function loadTranscriptionOptions(options) {
|
||||||
try {
|
try {
|
||||||
var menu = document.getElementById('transkript_type');
|
var menu = document.getElementById('transkript_type');
|
||||||
var object_holdy;
|
var object_holdy;
|
||||||
var choice ;
|
var choice;
|
||||||
object_holdy = options
|
object_holdy = options
|
||||||
for(i = 0; i < options.length; i++){
|
for (i = 0; i < options.length; i++) {
|
||||||
choice = document.createElement('option');
|
choice = document.createElement('option');
|
||||||
choice.textContent = object_holdy[i].displayname;
|
choice.textContent = object_holdy[i].displayname;
|
||||||
choice.value = object_holdy[i].name;
|
choice.value = object_holdy[i].name;
|
||||||
@@ -171,13 +171,13 @@ function loadTranscriptionOptions(options){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//function to load data type options to the drop down list
|
//function to load data type options to the drop down list
|
||||||
function loadDataTypeOptions(options){
|
function loadDataTypeOptions(options) {
|
||||||
try {
|
try {
|
||||||
var menu = document.getElementById('output_type');
|
var menu = document.getElementById('output_type');
|
||||||
var object_holdy;
|
var object_holdy;
|
||||||
var choice ;
|
var choice;
|
||||||
object_holdy = options
|
object_holdy = options
|
||||||
for(i = 0; i < options.length; i++){
|
for (i = 0; i < options.length; i++) {
|
||||||
choice = document.createElement('option');
|
choice = document.createElement('option');
|
||||||
choice.textContent = object_holdy[i].displayname;
|
choice.textContent = object_holdy[i].displayname;
|
||||||
choice.value = object_holdy[i].name;
|
choice.value = object_holdy[i].name;
|
||||||
@@ -190,13 +190,13 @@ function loadDataTypeOptions(options){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//function to load language options to the drop down list
|
//function to load language options to the drop down list
|
||||||
function loadLanguageOptions(){
|
function loadLanguageOptions() {
|
||||||
try {
|
try {
|
||||||
var menu = document.getElementById('language_option');
|
var menu = document.getElementById('language_option');
|
||||||
var object_holdy;
|
var object_holdy;
|
||||||
var choice ;
|
var choice;
|
||||||
object_holdy = Object.keys(languageOptions);
|
object_holdy = Object.keys(languageOptions);
|
||||||
for(i = 0; i < object_holdy.length; i++){
|
for (i = 0; i < object_holdy.length; i++) {
|
||||||
choice = document.createElement('option');
|
choice = document.createElement('option');
|
||||||
choice.textContent = object_holdy[i];
|
choice.textContent = object_holdy[i];
|
||||||
choice.value = object_holdy[i];
|
choice.value = object_holdy[i];
|
||||||
@@ -209,17 +209,17 @@ function loadLanguageOptions(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//function to load speaker options to the drop down list
|
//function to load speaker options to the drop down list
|
||||||
function loadSpeakerOptions(options){
|
function loadSpeakerOptions(options) {
|
||||||
try {
|
try {
|
||||||
var menu = document.getElementById('cur_speaker');
|
var menu = document.getElementById('cur_speaker');
|
||||||
var l = document.getElementById('cur_speaker').options.length -1;
|
var l = document.getElementById('cur_speaker').options.length - 1;
|
||||||
for(i = l; i >= 0; i--){
|
for (i = l; i >= 0; i--) {
|
||||||
menu.remove(i);
|
menu.remove(i);
|
||||||
}
|
}
|
||||||
var object_holdy;
|
var object_holdy;
|
||||||
var choice;
|
var choice;
|
||||||
object_holdy = Object.keys(options);
|
object_holdy = Object.keys(options);
|
||||||
for(i = 0; i < object_holdy.length; i++){
|
for (i = 0; i < object_holdy.length; i++) {
|
||||||
choice = document.createElement('option');
|
choice = document.createElement('option');
|
||||||
choice.textContent = options[object_holdy[i]].name;
|
choice.textContent = options[object_holdy[i]].name;
|
||||||
choice.value = object_holdy[i];
|
choice.value = object_holdy[i];
|
||||||
@@ -233,7 +233,7 @@ function loadSpeakerOptions(options){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//function to load speaker audio file options to the drop down list
|
//function to load speaker audio file options to the drop down list
|
||||||
function loadSpeakerAudio(option){
|
function loadSpeakerAudio(option) {
|
||||||
try {
|
try {
|
||||||
var menu = document.getElementById('speakerAudioViewer');
|
var menu = document.getElementById('speakerAudioViewer');
|
||||||
var aud = document.createElement("source");
|
var aud = document.createElement("source");
|
||||||
@@ -246,7 +246,7 @@ function loadSpeakerAudio(option){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function activateSubmitBtn(hasFile){
|
function activateSubmitBtn(hasFile) {
|
||||||
try {
|
try {
|
||||||
document.getElementById("submitButton").disabled = !hasFile;
|
document.getElementById("submitButton").disabled = !hasFile;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -254,7 +254,7 @@ function activateSubmitBtn(hasFile){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function deaktivateProgressbar(){
|
function deaktivateProgressbar() {
|
||||||
try {
|
try {
|
||||||
document.getElementById("progressbar").style.visibility = "hidden";
|
document.getElementById("progressbar").style.visibility = "hidden";
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -263,7 +263,7 @@ function deaktivateProgressbar(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Video thumbnail generation
|
//Video thumbnail generation
|
||||||
function generateThumbnail(path){
|
function generateThumbnail(path) {
|
||||||
const videoElement = document.getElementById("previewThumbnail");
|
const videoElement = document.getElementById("previewThumbnail");
|
||||||
while (videoElement.firstChild) videoElement.removeChild(videoElement.firstChild);
|
while (videoElement.firstChild) videoElement.removeChild(videoElement.firstChild);
|
||||||
videoElement.src = path;
|
videoElement.src = path;
|
||||||
@@ -281,7 +281,7 @@ let currentStep = 1;
|
|||||||
const totalSteps = steps.length;
|
const totalSteps = steps.length;
|
||||||
|
|
||||||
function showStep(stepNumber) {
|
function showStep(stepNumber) {
|
||||||
if (stepNumber < 1 || stepNumber > totalSteps){
|
if (stepNumber < 1 || stepNumber > totalSteps) {
|
||||||
console.error("StepNumber out of Bounds", stepNumber);
|
console.error("StepNumber out of Bounds", stepNumber);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -292,7 +292,7 @@ function showStep(stepNumber) {
|
|||||||
document.querySelector(`.step-item[data-step="${stepNumber}"]`).classList.add("active");
|
document.querySelector(`.step-item[data-step="${stepNumber}"]`).classList.add("active");
|
||||||
const activeBtn = document.querySelector(`.step-item[data-step="${stepNumber}"]`);
|
const activeBtn = document.querySelector(`.step-item[data-step="${stepNumber}"]`);
|
||||||
|
|
||||||
if(activeBtn) activeBtn.classList.add("active");
|
if (activeBtn) activeBtn.classList.add("active");
|
||||||
|
|
||||||
prevBtn.disabled = stepNumber == 1;
|
prevBtn.disabled = stepNumber == 1;
|
||||||
nextBtn.disabled = stepNumber === totalSteps;
|
nextBtn.disabled = stepNumber === totalSteps;
|
||||||
@@ -303,31 +303,40 @@ function showStep(stepNumber) {
|
|||||||
//Audio value setter
|
//Audio value setter
|
||||||
var speakerAudios = {};
|
var speakerAudios = {};
|
||||||
var speakerEndValues = {};
|
var speakerEndValues = {};
|
||||||
function setSpeakerAudiosValue(valy){
|
function setSpeakerAudiosValue(valy) {
|
||||||
try {
|
try {
|
||||||
speakerAudios = valy;
|
speakerAudios = valy;
|
||||||
speakerRewriten = valy;
|
speakerRewriten = valy;
|
||||||
document.getElementById("speakerAudioViewer").src = valy.speakerA.src;
|
document.getElementById("speakerAudioViewer").src = valy.speakerA.src;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendSpeakerPackages(){
|
function sendSpeakerPackages() {
|
||||||
try {
|
try {
|
||||||
window.submitSpeaker.speaker_submit(speakerAudios);
|
window.submitSpeaker.speaker_submit(speakerAudios);
|
||||||
} catch (error) {
|
} catch (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 {
|
||||||
|
const htmlContent = await fs.promises.readFile(inputHtmlPath, "utf-8");
|
||||||
|
const mappingData = await fs.promises.readFile(speakerMappingPath, "utf-8");
|
||||||
|
|
||||||
|
let speakerMap = {};
|
||||||
try {
|
try {
|
||||||
const htmlContent = await fs.promises.readFile(inputHtmlPath, "utf-8"); // read HTML file
|
const parsed = JSON.parse(mappingData);
|
||||||
const mappingData = await fs.promises.readFile(speakerMappingPath, "utf-8"); // read mapping file
|
if (parsed.speakerId && parsed.speakerName) {
|
||||||
|
speakerMap[parsed.speakerId] = parsed.speakerName;
|
||||||
// Parse mapping - supports JSON or simple format
|
} else {
|
||||||
let speakerMap = {};
|
Object.assign(speakerMap, parsed);
|
||||||
try {
|
|
||||||
speakerMap = JSON.parse(mappingData); // Try to parse as JSON
|
|
||||||
} catch (e) {
|
|
||||||
// If not JSON, try simple format: "Speaker A,Mike\nSpeaker B,Stefan"
|
|
||||||
const lines = mappingData.trim().split('\n');
|
|
||||||
lines.forEach(line => {
|
|
||||||
const [placeholder, realName] = line.split(',').map(s => s.trim());
|
|
||||||
if (placeholder && realName) {
|
|
||||||
speakerMap[placeholder] = realName;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
// Replace all speaker names in HTML content
|
const lines = mappingData.trim().split('\n');
|
||||||
let outputContent = htmlContent;
|
lines.forEach(line => {
|
||||||
Object.entries(speakerMap).forEach(([placeholder, realName]) => {
|
const [placeholder, realName] = line.split(',').map(s => s.trim());
|
||||||
// Create regex to replace all occurrences (case-sensitive)
|
if (placeholder && realName) speakerMap[placeholder] = realName;
|
||||||
const regex = new RegExp(`\\b${placeholder}\\b`, 'g');
|
|
||||||
outputContent = outputContent.replace(regex, realName);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Generate output file path based on input file name
|
|
||||||
const inputFileName = path.basename(inputHtmlPath, path.extname(inputHtmlPath));
|
|
||||||
const outPath = path.join(outputDir, `${inputFileName}_replaced.html`);
|
|
||||||
|
|
||||||
// Write output to file
|
|
||||||
fs.writeFileSync(outPath, outputContent, "utf8");
|
|
||||||
|
|
||||||
// console.log("Replaced HTML file written to:", outPath);
|
|
||||||
resolve(outPath)
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
// console.error("Error replacing speaker names:", error);
|
|
||||||
reject(error)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
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;
|
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