-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage-generator.js
58 lines (48 loc) · 2.22 KB
/
page-generator.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
const fs = require('fs');
const names = [];
const alphabet = {"Ё":"YO","Й":"I","Ц":"TS","У":"U","К":"K","Е":"E","Н":"N","Г":"G","Ш":"SH","Щ":"SCH","З":"Z","Х":"H","Ъ":"'","ё":"yo","й":"i","ц":"ts","у":"u","к":"k","е":"e","н":"n","г":"g","ш":"sh","щ":"sch","з":"z","х":"h","ъ":"'","Ф":"F","Ы":"I","В":"V","А":"a","П":"P","Р":"R","О":"O","Л":"L","Д":"D","Ж":"ZH","Э":"E","ф":"f","ы":"i","в":"v","а":"a","п":"p","р":"r","о":"o","л":"l","д":"d","ж":"zh","э":"e","Я":"Ya","Ч":"CH","С":"S","М":"M","И":"I","Т":"T","Ь":"'","Б":"B","Ю":"YU","я":"ya","ч":"ch","с":"s","м":"m","и":"i","т":"t","ь":"'","б":"b","ю":"yu"};
function createHtmlFile (element) {
return new Promise((resolve) => {
fs.readFile('./templates/index.html', function (err, buffer) {
if (err) {
throw new Error(err);
}
const contents = buffer.toString();
const data = {
extraHtmlClass: '',
name: element.name,
quote: '',
description: element.description
};
const name = generateName(data.name);
if (element.name.split(' ').length > 1 || element.name.length >= 9) {
data.extraHtmlClass = ' achieve__heading-text--small';
}
if (element.name.split(' ').length === 1 && element.name.length >= 14) {
data.extraHtmlClass = ' achieve__heading-text--super-small';
}
if (element.quote) {
data.quote = `«${element.quote}»`
}
fs.writeFile(`public/pages/${name}.html`, template(contents, data), () => {
resolve(name);
});
});
});
}
function template (template, data) {
for (const key in data) {
const templateKey = '{{' + key + '}}';
template = template.replace(templateKey, data[key]);
}
return template;
}
function transliterate(word){
return word.split('').map(function (char) {
return alphabet[char] || char;
}).join("");
}
function generateName (name) {
return transliterate(name.toLowerCase()).replace(/[\s,'\?\.]/g, '')
}
module.exports = createHtmlFile;