-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-pages.js
78 lines (64 loc) · 2.14 KB
/
generate-pages.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
const { readFile } = require('fs').promises;
const fs = require('fs');
const ejs = require('ejs');
// Read the EJS template file
const templatePath = 'new-test.ejs';
const mainPageTemplatePath = 'main.ejs';
const template = fs.readFileSync(templatePath, 'utf8');
const mainPageTemplate = fs.readFileSync(
mainPageTemplatePath,
'utf8'
);
// Output directory for HTML files
const outputDir = 'output/';
// Create the output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
async function readAndParseJSON() {
try {
const data = await readFile('pages.json', 'utf8');
const jsonObject = JSON.parse(data);
const mainContent = ejs.render(mainPageTemplate, {
data: jsonObject,
});
fs.writeFileSync('main.html', mainContent, 'utf8');
for (let idx = 1; idx < jsonObject.length - 1; idx++) {
if (idx > 0 && idx < jsonObject.length - 1) {
let id = jsonObject[idx].id;
let title = jsonObject[idx].title;
let label = jsonObject[idx].label;
let waybackUrl = jsonObject[idx].waybackUrl;
let url = jsonObject[idx].url;
let description = jsonObject[idx].description;
let prevId = jsonObject[idx - 1].id;
let nextId = jsonObject[idx + 1].id;
let year = jsonObject[idx].year;
// Render the template with the current object's data
const htmlContent = ejs.render(template, {
id: id,
title: title,
label: label,
waybackUrl: waybackUrl,
url: url,
description: description,
prevId: prevId,
nextId: nextId,
year: year,
});
console.log('filename', htmlContent);
// Generate a unique filename for each HTML file
const filename = `${id}.html`;
// Write the HTML content to the file
fs.writeFileSync(filename, htmlContent, 'utf8');
console.log(
`HTML file "${filename}" generated successfully.`
);
}
}
// console.log('Parsed JSON object:', jsonObject);
} catch (error) {
console.error('Error reading or parsing JSON:', error);
}
}
readAndParseJSON();