-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.node.js
145 lines (124 loc) · 4.84 KB
/
index.node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import gracefulFs from 'graceful-fs';
import path from 'path';
import fs from 'fs/promises';
import ejs from 'ejs';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
const rootPath = process.cwd();
console.log("Root path:", rootPath);
const sourcePath = path.resolve(rootPath, 'node_modules', 'poetryesm', 'source');
console.log('Source path:', sourcePath);
// 获取所有诗人路径
export async function getPoetPaths() {
try {
console.log('Checking source path:', sourcePath);
const directories = await fs.readdir(sourcePath);
console.log('Directories:', directories);
const poetPaths = [];
for (const dir of directories) {
const dynastyPath = path.join(sourcePath, dir);
console.log('Checking directory:', dynastyPath);
if ((await fs.stat(dynastyPath)).isDirectory()) {
const files = await fs.readdir(dynastyPath);
console.log('Files in directory:', files);
poetPaths.push(...files.map(file => path.join(dynastyPath, file)));
}
}
return poetPaths;
} catch (error) {
console.error('Error reading poets directory:', error);
throw error;
}
}
// 根据路径获取诗人信息
export async function getPoetByPath(filePath) {
try {
const content = await fs.readFile(filePath, 'utf-8');
return JSON.parse(content);
} catch (error) {
console.error('Error reading Poet file:', error);
throw error;
}
}
// 生成朝代的诗人清单
export async function generateDynastyPoetList(outputPath, dynastyTemplatePath = 'Dynasty.ejs') {
try {
console.log('Generating dynasty poet list...');
const poetPaths = await getPoetPaths();
console.log('Poet paths:', poetPaths);
const poets = await Promise.all(poetPaths.map(async poetPath => {
const poet = await getPoetByPath(poetPath);
return poet;
}));
const dynastyPoetMap = new Map();
poets.forEach(poet => {
if (!dynastyPoetMap.has(poet.Dynasty)) {
dynastyPoetMap.set(poet.Dynasty, []);
}
dynastyPoetMap.get(poet.Dynasty).push(poet);
});
const dynastyPoetList = Array.from(dynastyPoetMap.entries()).map(([dynasty, poetList]) => ({
dynasty,
poets: poetList
}));
const outputFilePath = path.resolve(outputPath, 'dynasties.html');
const templateContent = await fs.readFile(dynastyTemplatePath, 'utf-8');
const renderedContent = ejs.render(templateContent, { dynastyPoetList });
await fs.writeFile(outputFilePath, renderedContent, 'utf-8');
console.log('Dynasty poet list generated at:', outputFilePath);
} catch (error) {
console.error('Error generating dynasty poet list:', error);
throw error;
}
}
// 生成HTML页面
export async function generateHtmlPages(outputPath, poetTemplatePath = 'poet.ejs') {
try {
console.log('Generating HTML pages...');
const poetPaths = await getPoetPaths();
console.log('Poet paths:', poetPaths);
const htmlOutputPath = path.resolve(outputPath, 'poets');
if (!await fs.exists(htmlOutputPath)) {
await fs.mkdir(htmlOutputPath, { recursive: true });
}
for (const poet of poets) {
const templateContent = await fs.readFile(poetTemplatePath, 'utf-8');
const renderedContent = ejs.render(templateContent, { poet });
const poetHtmlFilePath = path.resolve(htmlOutputPath, `${poet.Name}.html`);
await fs.writeFile(poetHtmlFilePath, renderedContent, 'utf-8');
console.log('Generated HTML page for poet:', poet.Name);
}
console.log('HTML pages generated at:', htmlOutputPath);
} catch (error) {
console.error('Error generating HTML pages:', error);
throw error;
}
}
// 解析命令行参数
const argv = yargs(hideBin(process.argv))
.option('outputPath', {
alias: 'o',
describe: 'Output path for generated files',
type: 'string',
demandOption: false, // 不强制要求此选项
default: process.cwd() // 默认值为当前工作目录
})
.option('dynastyTemplatePath', {
alias: 'd',
describe: 'Path to the dynasty template file',
type: 'string',
default: 'Dynasty.ejs'
})
.option('poetTemplatePath', {
alias: 'p',
describe: 'Path to the poet template file',
type: 'string',
default: 'poet.ejs'
})
.help()
.alias('help', 'h')
.argv;
// 生成HTML页面
generateDynastyPoetList(argv.outputPath, argv.dynastyTemplatePath)
.then(() => generateHtmlPages(argv.outputPath, argv.poetTemplatePath))
.catch(error => console.error('Error generating files:', error));