@@ -8,6 +8,7 @@ import { DOMParser as dom } from "xmldom";
8
8
const readdir = util . promisify ( fs . readdir ) ;
9
9
const open = util . promisify ( fs . open ) ;
10
10
const readFile = util . promisify ( fs . readFile ) ;
11
+ const errors = [ ] ;
11
12
12
13
// latex (pdf version)
13
14
import {
@@ -43,6 +44,8 @@ import { setupSnippetsJson } from "./processingFunctions/processSnippetJson";
43
44
import { createTocJson } from "./generateTocJson" ;
44
45
import { setupReferencesJson } from "./processingFunctions/processReferenceJson" ;
45
46
import { SourceTextModule } from "vm" ;
47
+ import { threadId } from "worker_threads" ;
48
+ import { exitCode } from "process" ;
46
49
47
50
export let parseType ;
48
51
let version ;
@@ -163,7 +166,7 @@ async function translateXml(filepath, filename, option) {
163
166
}
164
167
165
168
if ( parseType == "json" ) {
166
- const relativeFilePath = path . join (
169
+ try { const relativeFilePath = path . join (
167
170
filepath ,
168
171
filename . replace ( / \. x m l $ / , "" ) + ".html"
169
172
) ;
@@ -188,6 +191,8 @@ async function translateXml(filepath, filename, option) {
188
191
stream . write ( JSON . stringify ( jsonObj ) ) ;
189
192
stream . end ( ) ;
190
193
} ) ;
194
+ } } catch ( error ) {
195
+ errors . push ( filepath + " " + error ) ;
191
196
}
192
197
return ;
193
198
}
@@ -215,9 +220,11 @@ async function recursiveTranslateXml(filepath, option, lang = "en") {
215
220
}
216
221
217
222
const fullPath = path . join ( inputDir , filepath ) ;
223
+ console . log ( fullPath ) ;
218
224
files = await readdir ( fullPath ) ;
219
225
const promises = [ ] ;
220
226
files . forEach ( file => {
227
+ console . log ( file ) ;
221
228
if ( file . match ( / \. x m l $ / ) ) {
222
229
// console.log(file + " being processed");
223
230
if (
@@ -364,27 +371,47 @@ async function main() {
364
371
recursiveTranslateXml ( "" , "parseXml" ) ;
365
372
} else if ( parseType == "json" ) {
366
373
const languages = await getDirectories ( path . join ( __dirname , "../xml" ) ) ;
367
- console . dir ( languages )
368
-
374
+ console . dir ( languages ) ;
369
375
370
- languages . forEach ( async lang => {
376
+ for ( const lang of languages ) {
371
377
outputDir = path . join ( __dirname , "../json" , lang ) ;
378
+ allFilepath = [ ] ;
379
+ tableOfContent = { } ;
380
+
372
381
createMain ( ) ;
373
382
374
- console . log ( " \ngenerate table of content\n" ) ;
383
+ console . log ( ` \ngenerate table of content for ${ lang } \n` ) ;
375
384
await recursiveTranslateXml ( "" , "generateTOC" , lang ) ;
376
385
allFilepath = sortTOC ( allFilepath ) ;
377
386
createTocJson ( outputDir ) ;
378
387
379
388
console . log ( "setup snippets and references\n" ) ;
380
389
await recursiveXmlToHtmlInOrder ( "setupSnippet" ) ;
381
390
console . log ( "setup snippets and references done\n" ) ;
382
-
383
391
await recursiveXmlToHtmlInOrder ( "parseXml" ) ;
384
392
writeRewritedSearchData ( ) ;
385
393
// this is meant to be temp; also, will remove the original "generateSearchData" after the updation at the frontend is completed.
386
394
//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 ) ;
388
415
}
389
416
}
390
417
0 commit comments