-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-html.php
executable file
·324 lines (290 loc) · 9.12 KB
/
build-html.php
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env php
<?php
use Carnet\AspectRatio;
use Carnet\Clip;
use Carnet\Movie;
use League\Plates\Engine;
/**
* Delete folder and all it's content
*/
function delTree(string $dir)
{
if (is_dir($dir)) {
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
}
require('./vendor/autoload.php');
if (is_file('config/basePath')) {
$basePath = trim(file_get_contents('config/basePath'), " \n\r\t\v\0/");
$basePath = empty($basePath) ? '' : "/$basePath";
} else {
$basePath = '';
}
$templates = new Engine('generator/templates');
$templates->addData(['stylesheet' => null, 'basePath' => $basePath]);
/**
* COLLECTIONS
*
* @return array[] Associative array where key is collection string ID of collection and value is list of clips ID
**/
function getCollections() : array
{
$collections = [];
$paths = glob('src/collections/*');
foreach ($paths as $path) {
$id = basename($path);
$collectionClipIds = array_unique(file("src/collections/$id", FILE_IGNORE_NEW_LINES));
$collectionClipIds = array_map(function($id) : int {
return intval($id);
}, $collectionClipIds);
$collections[$id] = $collectionClipIds;
}
ksort($collections);
return $collections;
}
/**
* @param array[] $collections
* @return array[] associative array where key is clip int ID and value is list of collections string IDs
**/
function getCollectionsIdIndex(array $collections) : array
{
$collectionsIndex = [];
foreach ($collections as $collection => $clips) {
foreach ($clips as $id) {
$collectionsIndex[$id][] = $collection;
}
}
return $collectionsIndex;
}
/**
* TAGS
*
* @return string[] Associative array where key is string ID and value is exactly the same
**/
function getTags() : array
{
$tags = file('src/tags', FILE_IGNORE_NEW_LINES);
$tags = array_combine($tags, $tags);
ksort($tags);
return $tags;
}
/**
* ASPECT RATIO
*
* @return AspectRatio[]
**/
function getAspectRatios() : array
{
$ratios = file('src/aspectRatios', FILE_IGNORE_NEW_LINES);
$ratios = array_map(function($ratio) : float {
return floatval($ratio);
}, $ratios);
sort($ratios);
foreach ($ratios as $i => $ratio) {
$prev = isset($ratios[$i - 1]) ? $ratios[$i - 1] : 0;
$next = isset($ratios[$i + 1]) ? $ratios[$i + 1] : 10;
$aspectRatios[] = new AspectRatio($ratio, $prev, $next);
}
return $aspectRatios;
}
/**
* MOVIES
*
* @return array[] Associative array where key is movie int ID and value is an array containting metadata
**/
function getMovies() : array
{
$movies = [];
$paths = glob('src/movies/*.json');
foreach ($paths as $path) {
$id = basename($path, '.json');
$movieJson = json_decode(file_get_contents($path), true);
$movies[$id] = new Movie($id, $movieJson);
}
ksort($movies);
return $movies;
}
/**
* CLIPS
*
* @return Clip[] Associative array where key is clip int ID and value is an array containting metadata
**/
function getClips() : array
{
$clips = [];
$paths = glob('src/clips/*.json');
foreach ($paths as $path) {
$id = intval(basename($path, '.json'));
$json = json_decode(file_get_contents($path), true);
$clips[$id] = new Clip($id, $json);
}
krsort($clips);
return $clips;
}
function removeHtmlBuild(): void
{
delTree('build/clip');
delTree('build/collection');
delTree('build/movie');
delTree('build/tag');
delTree('build/ar');
}
/**
* @param AspectRatio[] $aspectRatios
* @param Clip[] $clips
*/
function buildClips(array $clips, array $collectionsIndex, array $movies, array $aspectRatios): void
{
global $templates;
mkdir('build/clip');
foreach ($clips as $id => $clip) {
$collections = isset($collectionsIndex[$id]) ? $collectionsIndex[$id] : [];
$movie = $clip->movie !== null && isset($movies[$clip->movie]) ? $movies[$clip->movie] : null;
if (($clip->aspectRatio) !== null) {
foreach ($aspectRatios as $aspectRatio) {
if ($aspectRatio->isInRange($clip->aspectRatio)) {
$matchedAspectRatio = $aspectRatio;
}
}
} else {
$matchedAspectRatio = null;
}
$html = $templates->render('clip', ['clip' => $clip, 'collections' => $collections, 'movie' => $movie, 'aspectRatio' => $matchedAspectRatio]);
if (!is_dir("build/clip/$id")) {
mkdir("build/clip/$id", 0777, true);
}
file_put_contents("build/clip/$id/index.html", $html);
}
$html = $templates->render('clipIndex', ['clips' => $clips]);
file_put_contents("build/clip/index.html", $html);
}
/**
* @param Clip[] $clips
*/
function buildTags(array $tags, array $clips): void
{
global $templates;
mkdir('build/tag');
foreach ($tags as $tag) {
$filteredClips = [];
foreach ($clips as $clip) {
if (in_array($tag, $clip->tags)) {
$filteredClips[] = $clip;
}
}
$html = $templates->render('tag', ['tag' => $tag, 'clips' => $filteredClips]);
if (!is_dir("build/tag/$tag")) {
mkdir("build/tag/$tag");
}
file_put_contents("build/tag/$tag/index.html", $html);
}
$html = $templates->render('tagIndex', ['tags' => $tags]);
file_put_contents("build/tag/index.html", $html);
}
/**
* @param AspectRatio[] $aspectRatios
* @param Clip[] $clips
*/
function buildAspectRatios(array $aspectRatios, array $clips) : void
{
global $templates;
mkdir('build/ar');
$filteredAspectRatios = [];
foreach ($aspectRatios as $aspectRatio) {
$filteredClips = [];
foreach ($clips as $id => $clip) {
if ($clip->aspectRatio === null) {
unset($clips[$id]);
continue;
}
if ($aspectRatio->isInRange($clip->aspectRatio)) {
$filteredClips[] = $clip;
unset($clips[$id]);
}
}
if (empty($filteredClips)) {
continue;
}
$html = $templates->render('aspectRatio', ['aspectRatio' => $aspectRatio, 'clips' => $filteredClips]);
$filteredAspectRatios[] = $aspectRatio;
if (!is_dir("build/ar/$aspectRatio->slug")) {
mkdir("build/ar/$aspectRatio->slug");
}
file_put_contents("build/ar/$aspectRatio->slug/index.html", $html);
}
$html = $templates->render('aspectRatioIndex', ['aspectRatios' => $filteredAspectRatios]);
file_put_contents("build/ar/index.html", $html);
}
/**
* @param Clip[] $clips
*/
function buildCollections(array $collections, array $clips): void
{
global $templates;
mkdir('build/collection');
foreach ($collections as $collection => $collectionClipIds) {
$collectionClipIds = array_flip($collectionClipIds);
$collectionClips = array_intersect_key($clips, $collectionClipIds);
$html = $templates->render('collection', ['collection' => $collection, 'clips' => $collectionClips]);
if (!is_dir("build/collection/$collection")) {
mkdir("build/collection/$collection", 0777, true);
}
file_put_contents("build/collection/$collection/index.html", $html);
}
$html = $templates->render('collectionIndex', ['collections' => $collections]);
file_put_contents("build/collection/index.html", $html);
}
/**
* @param Clip[] $clips
*/
function buildMovies(array $movies, array $clips): void
{
global $templates;
mkdir('build/movie');
foreach ($movies as $id => $movie) {
$filteredClips = array_filter($clips, function (Clip $clip) use ($id): bool {
return isset($clip->movie) && $clip->movie === $id;
});
$html = $templates->render('movie', ['movie' => $movie, 'clips' => $filteredClips]);
if (!is_dir("build/movie/$id")) {
mkdir("build/movie/$id", 0777, true);
}
file_put_contents("build/movie/$id/index.html", $html);
}
$html = $templates->render('movieIndex', ['movies' => $movies]);
file_put_contents("build/movie/index.html", $html);
}
$clips = getClips();
$collections = getCollections();
$collectionsIndex = getCollectionsIdIndex($collections);
$movies = getMovies();
$aspectRatios = getAspectRatios();
$tags = getTags();
print "🚀 \033[1mbuilding HTML\033[0m";
removeHtmlBuild();
print '.';
buildClips($clips, $collectionsIndex, $movies, $aspectRatios);
print '.';
buildTags($tags, $clips);
print '.';
buildCollections($collections, $clips);
print '.';
buildMovies($movies, $clips);
print '.';
buildAspectRatios($aspectRatios, $clips);
print '.';
$html = $templates->render('home');
file_put_contents("build/index.html", $html);
print '.';
if (!is_dir("build/assets")) {
mkdir("build/assets");
}
copy('generator/style/base.css', 'build/assets/base.css');
copy('generator/favicon.png', 'build/assets/favicon.png');
copy('generator/javascript/script.js', 'build/assets/script.js');
print '.';
print "done!\n";