-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-html.js
66 lines (55 loc) · 2.07 KB
/
generate-html.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
const fs = require('fs');
const path = require('path');
// Constants for easy editing
const JSON_FILE_PATH = 'app/data/variant-rules.json';
const OUTPUT_DIR = 'output';
const HTML_TEMPLATE_PATH = 'templates/html-template.html';
// Path to the JSON file
const jsonFilePath = path.join(__dirname, JSON_FILE_PATH);
// Read the JSON file
fs.readFile(jsonFilePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading JSON file:', err);
return;
}
// Parse the JSON data
const variantRules = JSON.parse(data);
// Read the HTML template
fs.readFile(path.join(__dirname, HTML_TEMPLATE_PATH), 'utf8', (templateErr, templateData) => {
if (templateErr) {
console.error('Error reading HTML template:', templateErr);
return;
}
// Iterate over each item in the list
variantRules.forEach(item => {
// Create a dictionary of variables and their values
const variables = {
'{{allograph}}': item['allograph'],
'{{variant-name}}': item['variant-name'],
'{{component-features}}': item['component-features'].map(feature => ` <li>${feature.component} is ${feature.feature}</li>`).join('\n')
};
// Replace variables in the template
let htmlContent = templateData;
for (const [key, value] of Object.entries(variables)) {
htmlContent = htmlContent.replace(new RegExp(key, 'g'), value);
}
// Create the file name using allograph and variant-name
const fileName = `${item['allograph']}-${item['variant-name']}.html`;
// Define the output directory
const outputDir = path.join(__dirname, OUTPUT_DIR);
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// Write the HTML content to a file
const outputPath = path.join(outputDir, fileName);
fs.writeFile(outputPath, htmlContent, 'utf8', err => {
if (err) {
console.error('Error writing HTML file:', err);
} else {
console.log(`HTML file created: ${outputPath}`);
}
});
});
});
});