-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate.js
168 lines (135 loc) · 4.77 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
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
const fs = require("fs").promises;
const path = require("path");
const { save, getTemplate, insert, DefaultListMap } = require("./utils");
const Handlebars = require("handlebars");
start();
async function start() {
await Promise.all([generate("norge"), generate("sverige")]);
}
async function generate(country) {
try {
const template = await getTemplate("index_template");
const documents = await getDocuments(country);
const headerList = [
createCountryRadioGroup(country),
createCollapsibleLists(documents),
];
const result = insert(template, {
content: headerList.join("\n"),
});
save(country === "norge" ? "index" : "sverige", result);
console.log(`Generated ${country}.html`);
} catch (e) {
console.error("Error:", e);
}
}
function createCountryRadioGroup(country) {
const norgeActive = country === "norge" ? " active" : "";
const sverigeActive = country === "sverige" ? " active" : "";
return `
<div class="countryRadioGroup">
<a href="/" class="toggleOption toggleOptionLeft${norgeActive}" aria-current="${country === "norge" ? "page" : ""}" tabindex="0" aria-label="Klikk for å gå til norske avtaler">
Norge
<span class="countryRadioButton${norgeActive}" aria-hidden='true'></span>
</a>
<a href="/sverige.html" class="toggleOption toggleOptionRight${sverigeActive}" aria-current="${country === "sverige" ? "page" : ""}" tabindex="0" aria-label="Klikk for å gå til svenske avtaler">
Sverige
<span class="countryRadioButton${sverigeActive}" aria-hidden='true'></span>
</a>
</div>
`;
}
function createCollapsibleLists(documentsByType) {
Handlebars.registerHelper('createItem', function(item) {
return new Handlebars.SafeString(`
<article>
<h3><a href="${item.url}" title="${item.title}">${item.title} <span class="ml-10px" aria-hidden="true">→</span></a></h3>
</article>
`);
});
const templateSource = `
{{#each documentsByType}}
<button class="collapsibleHeader collapsibleButton" aria-label="{{this.type}} utvid/skjul">
<h2 class="collapsibleHeaderLeft" id="{{this.type}}">{{this.type}}</h2>
<span class="arrow"></span>
</button>
<div class="collapsibleContent" role="region" aria-labelledby="{{this.type}}">
{{#each this.items}}
<div class="collapsibleItem">
{{createItem this}}
</div>
{{/each}}
</div>
{{/each}}
`;
const template = Handlebars.compile(templateSource);
return template({
documentsByType: documentsByType.entryList().sort(
([type1, items1], [type2, items2]) =>
(items1[0].order || Infinity) - (items2[0].order || Infinity)
).map(([type, items]) => ({ type, items })),
});
}
async function getDocuments(country) {
const dir = path.join(__dirname, "..", "avtaler", country);
const typeDirs = (await fs.readdir(dir)).map((i) => path.join(dir, i));
const documentsByType = new DefaultListMap();
for (let typeDir of typeDirs) {
const typeDirStat = await fs.stat(typeDir);
if (!typeDirStat.isDirectory()) {
continue;
}
const metaFile = path.join(typeDir, "meta.json");
if (await fileExists(metaFile)) {
const metaContent = await fs.readFile(metaFile, "utf-8");
const { name, order } = JSON.parse(metaContent);
const type = name || path.basename(typeDir);
const contents = (await fs.readdir(typeDir)).map((i) =>
path.join(typeDir, i)
);
for (let content of contents) {
if (path.basename(content) === "meta.json") {
continue;
}
const title = await getAttribute(content, "title");
const url = `/avtaler/${country}/${path.basename(
typeDir
)}/${path.basename(content)}`;
documentsByType.set(type, { title, url, order });
}
} else {
const type = path.basename(typeDir);
const contents = (await fs.readdir(typeDir)).map((i) =>
path.join(typeDir, i)
);
for (let content of contents) {
if (path.basename(content) === "meta.json") {
continue;
}
const title = await getAttribute(content, "title");
const url = `/avtaler/${country}/${path.basename(
typeDir
)}/${path.basename(content)}`;
documentsByType.set(type, { title, url });
}
}
}
return documentsByType;
}
async function getAttribute(filename, attribute) {
const regex = new RegExp(`<${attribute}>(.*)<\/${attribute}>`);
const content = await fs.readFile(filename);
const result = regex.exec(content);
if (!result) {
return path.basename(filename);
}
return result[1];
}
async function fileExists(path) {
try {
await fs.access(path);
return true;
} catch {
return false;
}
}