Skip to content

Commit 48fe8df

Browse files
committed
modified index.js to properly handle updated repository structure. Added try catch when parsing xml to json to report failed parsing possibly attributed to unsound xml structure.
1 parent b37b503 commit 48fe8df

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

javascript/index.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DOMParser as dom } from "xmldom";
88
const readdir = util.promisify(fs.readdir);
99
const open = util.promisify(fs.open);
1010
const readFile = util.promisify(fs.readFile);
11+
const errors = [];
1112

1213
// latex (pdf version)
1314
import {
@@ -43,6 +44,8 @@ import { setupSnippetsJson } from "./processingFunctions/processSnippetJson";
4344
import { createTocJson } from "./generateTocJson";
4445
import { setupReferencesJson } from "./processingFunctions/processReferenceJson";
4546
import { SourceTextModule } from "vm";
47+
import { threadId } from "worker_threads";
48+
import { exitCode } from "process";
4649

4750
export let parseType;
4851
let version;
@@ -163,7 +166,7 @@ async function translateXml(filepath, filename, option) {
163166
}
164167

165168
if (parseType == "json") {
166-
const relativeFilePath = path.join(
169+
try {const relativeFilePath = path.join(
167170
filepath,
168171
filename.replace(/\.xml$/, "") + ".html"
169172
);
@@ -188,6 +191,8 @@ async function translateXml(filepath, filename, option) {
188191
stream.write(JSON.stringify(jsonObj));
189192
stream.end();
190193
});
194+
} } catch (error) {
195+
errors.push(filepath + " " + error);
191196
}
192197
return;
193198
}
@@ -215,9 +220,11 @@ async function recursiveTranslateXml(filepath, option, lang = "en") {
215220
}
216221

217222
const fullPath = path.join(inputDir, filepath);
223+
console.log(fullPath);
218224
files = await readdir(fullPath);
219225
const promises = [];
220226
files.forEach(file => {
227+
console.log(file);
221228
if (file.match(/\.xml$/)) {
222229
// console.log(file + " being processed");
223230
if (
@@ -364,27 +371,47 @@ async function main() {
364371
recursiveTranslateXml("", "parseXml");
365372
} else if (parseType == "json") {
366373
const languages = await getDirectories(path.join(__dirname, "../xml"));
367-
console.dir(languages)
368-
374+
console.dir(languages);
369375

370-
languages.forEach(async lang => {
376+
for (const lang of languages) {
371377
outputDir = path.join(__dirname, "../json", lang);
378+
allFilepath = [];
379+
tableOfContent = {};
380+
372381
createMain();
373382

374-
console.log("\ngenerate table of content\n");
383+
console.log(`\ngenerate table of content for ${lang}\n`);
375384
await recursiveTranslateXml("", "generateTOC", lang);
376385
allFilepath = sortTOC(allFilepath);
377386
createTocJson(outputDir);
378387

379388
console.log("setup snippets and references\n");
380389
await recursiveXmlToHtmlInOrder("setupSnippet");
381390
console.log("setup snippets and references done\n");
382-
383391
await recursiveXmlToHtmlInOrder("parseXml");
384392
writeRewritedSearchData();
385393
// this is meant to be temp; also, will remove the original "generateSearchData" after the updation at the frontend is completed.
386394
//testIndexSearch();
387-
});
395+
}
396+
}
397+
try {
398+
let summaryLog = "Parsing failed for: ";
399+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
400+
for (const err of errors) {
401+
summaryLog += "\n" + err;
402+
}
403+
const logDir = path.resolve(__dirname, "../logs");
404+
if (!fs.existsSync(logDir)) {
405+
fs.mkdirSync(logDir, { recursive: true });
406+
}
407+
408+
const logPath = path.join(logDir, `json-summary-${timestamp}.log`);
409+
fs.writeFileSync(logPath, summaryLog);
410+
console.log(
411+
`Summary log saved to logs/translation-summary-${timestamp}.log`
412+
);
413+
} catch (logError) {
414+
console.error("Failed to save log file:", logError);
388415
}
389416
}
390417

0 commit comments

Comments
 (0)