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
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/src/helpers.php';
if (in_array('--help', $argv, true) || in_array('-h', $argv, true)) {
echo "Usage: php build.php\n";
echo "Builds static HTML from src/ into public/ with no external dependencies.\n";
exit(0);
}
$root = __DIR__;
$sourceRoot = $root . '/src';
$outputRoot = $root . '/public';
$site = require $sourceRoot . '/site.php';
$pages = [];
foreach (glob($sourceRoot . '/pages/*.php') ?: [] as $pageFile) {
$page = require $pageFile;
if (!is_array($page)) {
throw new RuntimeException("Page file {$pageFile} must return an array.");
}
$pages[] = $page;
}
foreach ($site['projects'] as $index => $project) {
$pages[] = [
'key' => $project['route'],
'output' => $project['output'],
'title' => $project['title'] . ' | Sharkk',
'description' => $project['summary'],
'template' => 'project',
'order' => 100 + $index,
'project' => $project,
'slider' => [
'class' => 'work-header',
],
];
}
foreach ($site['blogPosts'] as $index => $post) {
$pages[] = [
'key' => $post['route'],
'output' => $post['output'],
'title' => $post['title'] . ' | Sharkk',
'description' => $post['description'],
'template' => 'blog-post',
'order' => 200 + $index,
'post' => $post,
];
}
$routes = [];
$outputs = [];
foreach ($pages as $page) {
foreach (['key', 'output', 'title', 'template'] as $field) {
if (!isset($page[$field]) || $page[$field] === '') {
throw new RuntimeException("Every page must define a non-empty {$field} field.");
}
}
if (isset($routes[$page['key']])) {
throw new RuntimeException("Duplicate route key: {$page['key']}");
}
$output = normalize_site_path($page['output']);
if (isset($outputs[$output])) {
throw new RuntimeException("Duplicate output path: {$output}");
}
$page['output'] = $output;
$routes[$page['key']] = $page;
$outputs[$output] = true;
}
usort($pages, static fn (array $a, array $b): int => ($a['order'] ?? 999) <=> ($b['order'] ?? 999));
$renderedPages = [];
foreach ($pages as $page) {
$currentOutput = $page['output'];
$link = static function (string $routeKey) use ($routes, $currentOutput): string {
if (!isset($routes[$routeKey])) {
throw new RuntimeException("Unknown route key: {$routeKey}");
}
return relative_url($currentOutput, $routes[$routeKey]['output']);
};
$asset = static fn (string $path): string => relative_url($currentOutput, normalize_site_path($path));
$renderPartial = null;
$shared = [
'site' => $site,
'page' => $page,
'routes' => $routes,
'link' => $link,
'asset' => $asset,
];
$renderPartial = static function (string $name, array $vars = []) use ($sourceRoot, $shared, &$renderPartial): string {
return render_template(
$sourceRoot . '/templates/partials/' . normalize_site_path($name) . '.php',
array_merge($shared, $vars, ['renderPartial' => $renderPartial])
);
};
$bodyHtml = $renderPartial($page['template']);
$html = render_template($sourceRoot . '/templates/layout.php', array_merge($shared, [
'bodyHtml' => $bodyHtml,
'renderPartial' => $renderPartial,
]));
$renderedPages[] = [
'page' => $page,
'html' => $html,
];
}
foreach ($renderedPages as $renderedPage) {
$outputPath = join_site_path($outputRoot, $renderedPage['page']['output']);
$outputDir = dirname($outputPath);
if (!is_dir($outputDir) && !mkdir($outputDir, 0777, true) && !is_dir($outputDir)) {
throw new RuntimeException("Unable to create output directory: {$outputDir}");
}
if (file_put_contents($outputPath, $renderedPage['html']) === false) {
throw new RuntimeException("Unable to write output file: {$outputPath}");
}
}
$blogIndexPath = join_site_path($outputRoot, 'blog/search-index.json');
$encodedBlogIndex = json_payload(blog_search_index($site['blogPosts']), true);
if (file_put_contents($blogIndexPath, $encodedBlogIndex . "\n") === false) {
throw new RuntimeException("Unable to write blog search index: {$blogIndexPath}");
}
foreach ($renderedPages as $renderedPage) {
validate_local_references($outputRoot, $renderedPage['page']['output'], $renderedPage['html']);
}
echo 'Built ' . count($renderedPages) . " pages into public/:\n";
foreach ($renderedPages as $renderedPage) {
echo ' - ' . $renderedPage['page']['output'] . "\n";
}