forked from lumeland/lume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.js
281 lines (231 loc) Β· 6.07 KB
/
source.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { basename, dirname, extname, join } from "./deps/path.js";
import { existsSync } from "./deps/fs.js";
import { Directory, Page } from "./filesystem.js";
import { concurrent } from "./utils.js";
export default class Source {
root = new Directory({ path: "/" });
data = new Map();
pages = new Map();
staticFiles = new Map();
assets = new Set();
ignored = new Set();
constructor(site) {
this.site = site;
}
/**
* Returns the Directory instance of a path
*/
getDirectory(path, create = false) {
let dir = this.root;
path.split("/").forEach((name) => {
if (!name || !dir) {
return;
}
if (create && !dir.dirs.has(name)) {
dir.createDirectory(name);
}
dir = dir.dirs.get(name);
});
return dir;
}
/**
* Check whether a file is included in the static files
*/
isStatic(file) {
for (const entry of this.staticFiles) {
const [from] = entry;
if (file.startsWith(from)) {
return entry;
}
}
return false;
}
/**
* Load a directory recursively
*/
async loadDirectory(directory = this.root) {
const path = this.site.src(directory.src.path);
return concurrent(
Deno.readDir(path),
(entry) => this.#loadEntry(directory, entry),
);
}
/**
* Reload some files
*/
async loadFile(file) {
const entry = {
name: basename(file),
isFile: true,
isDirectory: false,
isSymlink: false,
};
//Is a file inside _data folder
if (file.match(/\/_data\//)) {
const dir = file.split("/_data/", 2).shift();
const directory = this.getDirectory(dir, true);
return this.#loadDataFolderEntry(
join(directory.src.path, "_data"),
entry,
directory.data,
);
}
const directory = this.getDirectory(dirname(file), true);
await this.#loadEntry(directory, entry);
}
/**
* Load an entry from a directory
*/
async #loadEntry(directory, entry) {
if (entry.isSymlink || entry.name.startsWith(".")) {
return;
}
const path = join(directory.src.path, entry.name);
if (this.staticFiles.has(path) || this.ignored.has(path)) {
return;
}
if (entry.isDirectory && entry.name === "_data") {
directory.data = await this.#loadDataFolder(path);
return;
}
if (entry.isFile && entry.name.match(/^_data\.\w+$/)) {
directory.data = await this.#loadData(path);
return;
}
if (entry.name.startsWith("_")) {
return;
}
if (entry.isFile) {
const page = await this.#loadPage(path);
if (page) {
directory.setPage(entry.name, page);
} else {
directory.unsetPage(entry.name);
}
return;
}
if (entry.isDirectory) {
const subDirectory = directory.createDirectory(entry.name);
await this.loadDirectory(subDirectory);
return;
}
}
/**
* Create and returns a Page
*/
async #loadPage(path) {
let ext, load;
for (const [key, value] of this.pages) {
if (path.endsWith(key)) {
ext = key;
load = value;
break;
}
}
if (!load) {
return;
}
const fullPath = this.site.src(path);
if (!existsSync(fullPath)) {
return;
}
const info = await Deno.stat(fullPath);
const src = {
path: path.slice(0, -ext.length),
lastModified: info.mtime,
created: info.birthtime,
ext,
};
const page = new Page(src);
page.data = await load(fullPath);
const subext = extname(page.src.path);
if (subext && !this.assets.has(ext)) {
page.dest.path = page.src.path.slice(0, -subext.length);
page.dest.ext = subext;
} else {
page.dest.path = page.src.path;
page.dest.ext = this.assets.has(ext) ? ext : ".html";
}
if (!page.data.date) {
page.data.date = getDate(page.src, page.dest);
} else if (!(page.data.date instanceof Date)) {
throw new Error(
'Invalid date. Use "yyyy-mm-dd" or "yyy-mm-dd hh:mm:ss" formats',
);
}
return page;
}
/**
* Load a _data.* file and return the content
*/
async #loadData(path) {
for (const [ext, loader] of this.data) {
if (path.endsWith(ext)) {
return loader(this.site.src(path));
}
}
}
/**
* Load a _data folder and return the content of all files
*/
async #loadDataFolder(path) {
const data = {};
for (const entry of Deno.readDirSync(this.site.src(path))) {
await this.#loadDataFolderEntry(path, entry, data);
}
return data;
}
/**
* Load a data file inside a _data folder
*/
async #loadDataFolderEntry(path, entry, data) {
if (
entry.isSymlink || entry.name.startsWith(".") ||
entry.name.startsWith("_")
) {
return;
}
if (entry.isFile) {
const name = basename(entry.name, extname(entry.name));
data[name] = Object.assign(
data[name] || {},
await this.#loadData(join(path, entry.name)),
);
return;
}
if (entry.isDirectory) {
data[entry.name] = await this.#loadDataFolder(join(path, entry.name));
}
}
}
function getDate(src, dest) {
const fileName = basename(src.path);
const dayInPath = fileName.match(/^(\d{4})-(\d{2})-(\d{2})_/);
if (dayInPath) {
const [found, year, month, day] = dayInPath;
dest.path = dest.path.replace(found, "");
return new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
}
const timeInPath = fileName.match(
/^(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})_/,
);
if (timeInPath) {
const [found, year, month, day, hour, minute, second] = timeInPath;
dest.path = dest.path.replace(found, "");
return new Date(
parseInt(year),
parseInt(month) - 1,
parseInt(day),
parseInt(hour),
parseInt(minute),
parseInt(second),
);
}
const orderInPath = fileName.match(/^(\d+)_/);
if (orderInPath) {
const [found, timestamp] = orderInPath;
dest.path = dest.path.replace(found, "");
return new Date(parseInt(timestamp));
}
return src.created || src.lastModified;
}