-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake-html-page.js
169 lines (137 loc) · 5.67 KB
/
make-html-page.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { categories, examples } from './data/categories.js';
import fs from 'fs';
import path from 'path';
// Directory where the HTML files will be saved
const __dirname = path.dirname(new URL(import.meta.url).pathname);
//
// Generates the HTML index page for each category
//
function generateCategoryHTML(category) {
const categoryExamples = examples.filter(example => example.category === category.name);
let categoryHTML = `<div class="categories">`;
categoryExamples.forEach(example => {
categoryHTML += `
<a class="staticExampleLink" href="${example.url}">
<div class="category" style="background-color: ${category.color};">
<span class="categoryExample">${category.title}</span>
<p>${example.description}</p>
<h3>${example.title}</h3>
</div></a>`;
});
categoryHTML += `</div>`;
return categoryHTML;
}
const outputDir = path.join(__dirname, 'public');
//let outputDir = __dirname;
let indexTemplate = fs.readFileSync(path.join(__dirname, '/_template.html'), 'utf8');
// Ensure the output directory exists
if (!fs.existsSync(outputDir)){
fs.mkdirSync(outputDir, { recursive: true });
}
categories.forEach(category => {
let str = new String(indexTemplate); // Assuming indexTemplate is your base HTML template string
const categoryHTML = generateCategoryHTML(category);
// filePath should dervive from category.url, just remove the .html and replace with .html
const filePath = path.join(outputDir, `${category.url}`);
//const filePath = path.join(outputDir, `${category.title.toLowerCase().replace(/\s+/g, '-')}.html`);
let mantraGameDevFrameworksTags = [
'javascript game development',
'html5 game development',
'webgl game development',
'css game development',
'css3 game development',
'canvas game development',
'web game development',
'browser game development',
'browser based game development',
'browser games'
];
str = str.replace('$$$categories$$$', categoryHTML);
str = str.replace('$$$title$$$', 'Mantra - Examples - ' + category.title);
// Combine category-specific tags with general game development tags and create a single keywords meta tag
const combinedTags = [...category.tags, ...mantraGameDevFrameworksTags].join(', ');
const metaKeywordsTag = `<meta name="keywords" content="${combinedTags}">`;
// Create a description meta tag using the category description
const metaDescriptionTag = `<meta name="description" content="${category.description}">`;
// Insert meta tags into the template
str = str.replace('$$$meta$$$', `${metaKeywordsTag}\n${metaDescriptionTag}`);
// Generate simple html spans as tags for each tag in category.tags array
const htmlTags = category.tags.map(tag => `<span class="tag">${tag}</span>`).join('\n');
str = str.replace('$$tags$$$', htmlTags);
console.log(str); // For debugging, to see the final HTML string
fs.writeFile(filePath, str, err => {
if (err) {
console.error(`Error writing file for category ${category.title}:`, err);
} else {
console.log(`File written for category ${category.title}: ${filePath}`);
}
});
});
// Function to generate HTML content for the main categories
function generateMainCategoriesHTML(categories) {
let categoriesHTML = `<div class="categories">`;
categories.forEach(category => {
categoriesHTML += `
<a class="staticExampleLink" href="${category.url}">
<div class="category" style="background-color: ${category.color};">
<span class="categoryExample">${category.title}</span>
<p>${category.description}</p>
<h3>${category.title}</h3>
</div>
</a>`;
});
categoriesHTML += `</div>`;
return categoriesHTML;
}
// Generate the main categories HTML
const mainCategoriesHTML = generateMainCategoriesHTML(categories);
console.log("mainCategoriesHTML", mainCategoriesHTML)
// Create index.html content by replacing the placeholder with main categories HTML
let indexHTML = indexTemplate.replace('$$$categories$$$', mainCategoriesHTML);
indexHTML = indexHTML.replace('$$$title$$$', 'Mantra - Examples - Main Categories');
// Ensure the output directory exists
if (!fs.existsSync(outputDir)){
fs.mkdirSync(outputDir, { recursive: true });
}
// Write the index.html file
const indexPath = path.join(outputDir, 'index.html');
fs.writeFile(indexPath, indexHTML, err => {
if (err) {
console.error('Error writing index.html:', err);
} else {
console.log('index.html file written successfully.');
}
});
// read the contents of ./data/categories.js, remove the last line, and write to ./public/categories.js
const categoriesPath = path.join(__dirname, '/data/categories.js');
let categoriesData = fs.readFileSync(categoriesPath, 'utf8');
categoriesData = categoriesData.split('\n').slice(0, -1).join('\n');
fs.writeFileSync(path.join(outputDir, 'categories.js'), categoriesData, 'utf8');
// TODO: in addition we wish to generate a .html and .js file for each example ( assuming one doesn't already exist )
// the .html file will contain this template:
function exampleTemplateHolder (example) {
let html = ```
<html>
<link href="../prism.css" rel="stylesheet" />
<script src="../code-editor.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
loadEditor('./${example.name}js');
});
</script>
<iframe src="../demo.html?source=${example.path}" width="100%" height="50%"></iframe>
<script src="../prism.min.js"></script>
</html>
```;
return html;
}
// the .js file will contain this template:
function exampleTemplateJS (example) {
let js = ```
let game = new MANTRA.Game({
graphics: ['css'], // array enum, 'babylon', 'css', 'three'
});
game.start();
```;
return js;
}