-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.js
90 lines (84 loc) · 2.62 KB
/
generate.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
const fs = require('fs')
const path = require('path')
const credits = require('./credits.old.js')
const groupData = require('./groups.old.js')
const GenerateTemplate = (filename) =>
{
let target = null
for (let item of credits)
{
if (target != null) continue
if (item.validate != undefined && typeof item.validate == 'function')
{
if (item.validate(filename))
{
target = item
}
}
else if (item.files != undefined || typeof item.files == 'object')
{
if (item.files.includes(filename))
{
target = item
}
}
}
// console.log(target, filename)
let elementImage = `<img alt="${filename}" src="${filename}" height="31" targetLink="${target != null ? target.link != null ? target.link || '' : '' : ''}" />`
if (target != null && target.link != undefined)
{
return `<a href="${target.link}">${elementImage}</a>`
}
else
{
return elementImage
}
}
let elements = []
let filenameArray = fs.readdirSync('./img')
let generatedFilenames = {}
for (let group of groupData)
{
let groupElements = []
for (let filename of filenameArray)
{
console.log(filename, filename.match(group.expression))
if (filename.match(group.expression) != null && generatedFilenames[filename] == undefined)
{
groupElements.push(GenerateTemplate(filename))
generatedFilenames[filename] = true
}
}
if (groupElements.length > 0)
groupElements = [
`<h1>${group.name}</h1>`,
...groupElements
]
elements = elements.concat(groupElements)
}
let tempElementArr = []
for (let filename of filenameArray)
{
if (/\.(png|gif|jp(e|)g)$/i.test(filename) && generatedFilenames[filename] == undefined)
{
tempElementArr.push(GenerateTemplate(filename))
}
}
if (tempElementArr.length > 0)
tempElementArr = [
`<h1>Other</h1>`,
...tempElementArr
]
elements = elements.concat(tempElementArr)
if (fs.existsSync('dist/index.html'))
fs.unlinkSync('dist/index.html')
let content = fs.readFileSync('header.html').toString()
content += '<ul>' + elements.map(v => `<li>${v}</li>`).join('\n') + '</ul>'
content += `<h3>Generated at ${new Date(Date.now()).toLocaleString('en-AU', {timeZone: 'UTC'})} (UTC+00:00)</h3>`
content += fs.readFileSync('footer.html')
fs.writeFileSync('dist/index.html', content)
for (let filename of filenameArray)
{
if (/\.(png|gif|jp(e|)g)$/i.test(filename))
fs.copyFileSync(`./img/${filename}`, `./dist/${filename}`)
}