-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-archive.js
More file actions
108 lines (98 loc) · 2.24 KB
/
template-archive.js
File metadata and controls
108 lines (98 loc) · 2.24 KB
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
import fs from 'fs'
import { getMetaData, sortMetaData } from './generate.js'
import { formatDate } from './utils.js'
const dir = './programs'
const exports = {
parent: {
name: './archive'
},
html: {
name: 'archive.html',
},
js: {
name: '',
},
css: {
name: 'style.css',
},
}
const data = sortMetaData(getMetaData('./metadata.json'), true)
const programStringT = fs.readFileSync('template-program.html', 'utf-8')
let contentString = ''
Object.entries(data).forEach(([file, time])=> {
if (file == 'programs.js') return
contentString += wrapAroundRow([file, time])
let programString = replace(
programStringT,
{
f: 'cssFile',
v: `../${exports.css.name}`
}
)
programString = replace(
programString,
{
f: 'title',
v: `${file.replace('.js', '')}`
}
)
programString = replace(
programString,
{
f: '0',
v: `'${file.replace('.js', '')}'`
}
)
fs.writeFileSync(
`${exports.parent.name}/${file.replace('.js', '.html')}`,
programString
)
})
const archiveStringT = fs.readFileSync('template-archive.html', 'utf-8')
let archiveString = ''
archiveString = replace(
archiveStringT,
{
f: 'cssFile',
v: exports.css.name
}
)
archiveString = replace(
archiveString,
{
f: 'jsFile',
v: exports.js.name
}
)
archiveString = replace(
archiveString,
{
f: '0',
v: contentString
}
)
fs.writeFileSync(
`./${exports.html.name}`,
archiveString
)
function replace(dst, patch) {
// f => what to [f]ind, v => which [v]alue to use
let pattern = `"{${patch.f}}"`
dst = dst.replace(pattern, patch.v)
return dst
}
function capitalize(s) {
// TODO: capitalize first letter of the word
return s
}
function wrapAroundRow(arr) {
const f = arr[0].replace('.js', '')
const d = formatDate(arr[1], 'hr:mdy')
const str = `` +
`<div id='${f}' class='program'>` +
`[${d}] ` +
`Link to <a href='./archive/${f}.html'> ` +
`${capitalize(f.replace('_', ' '))}` +
`</a></div>`
return str
}