Skip to content

Commit dffc559

Browse files
committed
5 types
1 parent 56a2ac2 commit dffc559

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+36330
-22
lines changed

index.js

+36-9
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,50 @@
22
'use strict';
33
// index.js
44

5+
6+
// modules
57
const fs = require('fs');
8+
const path = require('path');
69
const child_process = require('child_process');
710

811

9-
// 1.
10-
// child_process.execSync(`python ./pdf/tools/pdf2txt.py -o ${xmlPath} ${pdfPath}`);
12+
// command line tools
13+
const pythonTool = path.join(__dirname, 'tools', 'pdf2txt.py');
14+
15+
16+
// translation source files
17+
const sourceFiles = fs.readdirSync(path.join(__dirname, 'pdf'))
18+
.filter(base => path.parse(base).ext === '.pdf');
19+
20+
21+
// translating
22+
sourceFiles.forEach(file => {
23+
// path
24+
const name = path.parse(file).name;
25+
const pdfPath = path.join(__dirname, 'pdf', name + '.pdf');
26+
const xmlPath = path.join(__dirname, 'xml', name + '.xml');
27+
const jsonPath = path.join(__dirname, 'json', name + '.json');
28+
const txtPath = path.join(__dirname, 'txt', name + '.txt');
29+
const mdPath = path.join(__dirname, 'md', name + '.md');
30+
31+
32+
// 1. pdf to xml
33+
console.log(`Transforming and parsing ${name}.pdf ...`);
34+
child_process.execSync(`python ${pythonTool} -o ${xmlPath} ${pdfPath}`);
35+
1136

37+
// 2. xml to txt
38+
console.log(`Extracting structural information from ${name}.pdf ...`);
39+
child_process.execSync(`node ./scripts/xml2txt.js ${xmlPath} ${jsonPath} ${txtPath}`);
1240

1341

14-
// 2.
15-
// child_process.execSync('node xml2txt.js');
1642

43+
// 3. translation,两种写法均可
44+
// const stream = child_process.execSync('node ./scripts/translate.js', { encoding: 'utf8' });
45+
// console.log(stream);
1746

1847

19-
// 3. 翻译,两种写法均可
20-
const stream = child_process.execSync('node ./scripts/translate.js', { encoding: 'utf8' });
21-
console.log(stream);
2248

23-
// const stream = child_process.spawnSync('node', ['./scripts/translate.js'], { encoding: 'utf8' }); // 这是一个流
24-
// console.log(stream.stdout); // 持续输出这个流
49+
// const stream = child_process.spawnSync('node', ['./scripts/translate.js', txtPath, 'en', 'zh', 'md'], { encoding: 'utf8' }); // 这是一个流
50+
// console.log(stream.stdout); // 持续输出这个流
51+
});

0 commit comments

Comments
 (0)