-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.js
119 lines (106 loc) · 4.11 KB
/
site.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
const fs = require('fs');
const jsrender = require('jsrender');
function readable(file) {
try {
fs.accessSync(file, fs.constants.R_OK)
return true;
} catch (err) {
return false;
}
}
function main(){
let logger = console.log
logger = () => {} // Comment this out to see logging.
// https://stackoverflow.com/a/58188006
const argv = (() => {
const arguments = {};
arguments.loose = [];
process.argv.slice(2).map( (element) => {
const simple = element.match('^--([a-zA-Z0-9]+)$');
if (simple) {
arguments[simple[1]] = true;
return;
}
const matches = element.match( '^--([a-zA-Z0-9]+)=(.*)$');
if ( matches ){
arguments[matches[1]] = matches[2]
.replace(/^['"]/, '').replace(/['"]$/, '');
return;
}
arguments.loose.push(element);
});
return arguments;
})();
if (!argv.dir) {
console.log("--dir=<dir> required.");
return;
}
logger("dir:", argv.dir);
// Load all of the data. rootInfo is just the main Info file loaded.
// For each album, the album-specific Info file is loaded as ._info.
// For each photo, the photo-specific json file is loaded as ._info
// on the photo.
const rootInfoFile = argv.dir + "/Info.json";
let rootInfo = JSON.parse(fs.readFileSync(rootInfoFile));
for (let album of rootInfo.albums) {
const albumDir = argv.dir + "/" + album.subdir;
const albumInfoFile = albumDir + "/Info.json";
if (argv.partial && !readable(albumInfoFile)) {
continue;
}
album._info = JSON.parse(fs.readFileSync(albumInfoFile));
for (let photo of album._info.photos) {
const photoInfoFile = albumDir + "/" + photo.id + ".json";
if (argv.partial && !readable(photoInfoFile)) {
continue;
}
photo._info = JSON.parse(fs.readFileSync(photoInfoFile));
}
}
logger("albums[0].photos[0]:", rootInfo.albums[0]._info.photos[0]);
const rootIndexFilename = argv.dir + "/index.html";
let rootIndexTmpl = jsrender.templates('./templates/root_index.html');
let rootHtml = rootIndexTmpl(rootInfo);
fs.writeFileSync(rootIndexFilename, rootHtml);
for (let i = 0; i < rootInfo.albums.length; i++) {
let albumInfo = rootInfo.albums[i];
if (argv.partial && !albumInfo._info) {
continue;
}
albumInfo.rootTitle = rootInfo.title;
albumInfo.i = i+1;
albumInfo.c = rootInfo.albums.length;
if (i-1 >= 0) {
albumInfo.prevAlbumDir = rootInfo.albums[i-1].subdir;
}
if (i+1 < rootInfo.albums.length) {
albumInfo.nextAlbumDir = rootInfo.albums[i+1].subdir;
}
const albumDir = argv.dir + "/" + albumInfo.subdir;
const albumIndexFilename = albumDir + "/index.html";
let albumIndexTmpl = jsrender.templates('./templates/album_index.html');
let albumHtml = albumIndexTmpl(albumInfo);
fs.writeFileSync(albumIndexFilename, albumHtml);
for (let j = 0; j < albumInfo._info.photos.length; j++) {
let photoInfo = albumInfo._info.photos[j];
if (argv.partial && !photoInfo._info) {
continue;
}
photoInfo.rootTitle = rootInfo.title;
photoInfo.albumTitle = albumInfo.title;
photoInfo.i = j+1;
photoInfo.c = albumInfo._info.photos.length;
if (j-1 >= 0) {
photoInfo.prevPhoto = albumInfo._info.photos[j-1].id;
}
if (j+1 < albumInfo._info.photos.length) {
photoInfo.nextPhoto = albumInfo._info.photos[j+1].id;
}
const photoIndexFilename = albumDir + "/" + photoInfo.id + "_photo.html";
let photoIndexTmpl = jsrender.templates('./templates/photo.html');
let photoHtml = photoIndexTmpl(photoInfo);
fs.writeFileSync(photoIndexFilename, photoHtml);
}
}
}
main();