|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nexendrie\SiteGenerator; |
|
5
|
|
|
|
|
6
|
|
|
use cebe\markdown\GithubMarkdown, |
|
7
|
|
|
Nette\Utils\Finder, |
|
8
|
|
|
Nette\Neon\Neon, |
|
9
|
|
|
Nette\Utils\FileSystem, |
|
10
|
|
|
Symfony\Component\OptionsResolver\OptionsResolver, |
|
11
|
|
|
Nette\Utils\Validators; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Generator |
|
15
|
|
|
* |
|
16
|
|
|
* @author Jakub Konečný |
|
17
|
|
|
* @property string $source |
|
18
|
|
|
* @property string $output |
|
19
|
|
|
* @method void onBeforeGenerate() |
|
20
|
|
|
* @method void onCreatePage(string $html, Generator $generator, string $filename) |
|
21
|
|
|
* @method void onAfterGenerate() |
|
22
|
|
|
*/ |
|
23
|
1 |
|
class Generator { |
|
24
|
1 |
|
use \Nette\SmartObject; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $templateFile = __DIR__ . "/template.html"; |
|
28
|
|
|
/** @var string[] */ |
|
29
|
|
|
protected $ignoredFiles = [ |
|
30
|
|
|
"README.md", |
|
31
|
|
|
]; |
|
32
|
|
|
/** @var string[] */ |
|
33
|
|
|
protected $ignoredFolders = [ |
|
34
|
|
|
"vendor", ".git", "tests", |
|
35
|
|
|
]; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
protected $source; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
protected $output; |
|
40
|
|
|
/** @var string[] */ |
|
41
|
|
|
protected $assets = []; |
|
42
|
|
|
/** @var callable[] */ |
|
43
|
|
|
protected $metaNormalizers = []; |
|
44
|
|
|
/** @var callable[] */ |
|
45
|
|
|
public $onBeforeGenerate = []; |
|
46
|
|
|
/** @var callable[] */ |
|
47
|
|
|
public $onCreatePage = []; |
|
48
|
|
|
/** @var callable[] */ |
|
49
|
|
|
public $onAfterGenerate = []; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(string $source, string $output) { |
|
52
|
1 |
|
$this->setSource($source); |
|
53
|
1 |
|
FileSystem::createDir($output); |
|
54
|
1 |
|
$this->setOutput($output); |
|
55
|
1 |
|
$this->onBeforeGenerate[] = [$this, "clearOutputFolder"]; |
|
56
|
1 |
|
$this->onCreatePage[] = [$this, "processImages"]; |
|
57
|
1 |
|
$this->onAfterGenerate[] = [$this, "copyAssets"]; |
|
58
|
1 |
|
$this->addMetaNormalizer([$this, "normalizeTitle"]); |
|
59
|
1 |
|
$this->addMetaNormalizer([$this, "normalizeStyles"]); |
|
60
|
1 |
|
$this->addMetaNormalizer([$this, "normalizeScripts"]); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function addMetaNormalizer(callable $callback): void { |
|
64
|
1 |
|
$this->metaNormalizers[] = $callback; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getSource(): string { |
|
68
|
1 |
|
return $this->source; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setSource(string $source) { |
|
72
|
1 |
|
if(is_dir($source)) { |
|
73
|
1 |
|
$this->source = realpath($source); |
|
74
|
|
|
} |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getOutput(): string { |
|
78
|
1 |
|
return $this->output; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setOutput(string $output) { |
|
82
|
1 |
|
$this->output = realpath($output); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function createMetaResolver(): OptionsResolver { |
|
86
|
1 |
|
$resolver = new OptionsResolver(); |
|
87
|
1 |
|
$resolver->setDefaults([ |
|
88
|
1 |
|
"title" => "", |
|
89
|
|
|
"styles" => [], |
|
90
|
|
|
"scripts" => [], |
|
91
|
|
|
]); |
|
92
|
1 |
|
$isArrayOfStrings = function(array $value) { |
|
93
|
1 |
|
return Validators::everyIs($value, "string"); |
|
94
|
|
|
}; |
|
95
|
1 |
|
$resolver->setAllowedTypes("title", "string"); |
|
96
|
1 |
|
$resolver->setAllowedTypes("styles", "array"); |
|
97
|
1 |
|
$resolver->setAllowedValues("styles", $isArrayOfStrings); |
|
98
|
1 |
|
$resolver->setAllowedTypes("scripts", "array"); |
|
99
|
1 |
|
$resolver->setAllowedValues("scripts", $isArrayOfStrings); |
|
100
|
1 |
|
return $resolver; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function getMeta(string $filename, string &$html): array { |
|
104
|
1 |
|
$resolver = $this->createMetaResolver(); |
|
105
|
1 |
|
$metaFilename = str_replace(".md", ".neon", $filename); |
|
106
|
1 |
|
$meta = []; |
|
107
|
1 |
|
if(file_exists($metaFilename)) { |
|
108
|
1 |
|
$meta = Neon::decode(file_get_contents($metaFilename)); |
|
109
|
|
|
} |
|
110
|
1 |
|
$result = $resolver->resolve($meta); |
|
111
|
1 |
|
foreach($this->metaNormalizers as $normalizer) { |
|
112
|
1 |
|
$normalizer($result, $html, $filename); |
|
113
|
|
|
} |
|
114
|
1 |
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function addAsset(string $asset): void { |
|
118
|
1 |
|
$asset = realpath($asset); |
|
|
|
|
|
|
119
|
1 |
|
if(!in_array($asset, $this->assets)) { |
|
120
|
1 |
|
$this->assets[] = $asset; |
|
121
|
|
|
} |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
protected function normalizeTitle(array &$meta, string &$html, string $filename): void { |
|
|
|
|
|
|
125
|
1 |
|
if(strlen($meta["title"]) === 0) { |
|
126
|
1 |
|
unset($meta["title"]); |
|
127
|
1 |
|
$html = str_replace(" |
|
128
|
1 |
|
<title>%%title%%</title>", "", $html); |
|
129
|
|
|
} |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
protected function normalizeStyles(array &$meta, string &$html, string $filename): void { |
|
|
|
|
|
|
133
|
1 |
|
$basePath = dirname($filename); |
|
134
|
1 |
|
$meta["styles"] = array_filter($meta["styles"], function($value) use($basePath) { |
|
135
|
1 |
|
return file_exists("$basePath/$value"); |
|
136
|
1 |
|
}); |
|
137
|
1 |
|
if(!count($meta["styles"])) { |
|
138
|
1 |
|
unset($meta["styles"]); |
|
139
|
1 |
|
$html = str_replace(" |
|
140
|
1 |
|
%%styles%%", "", $html); |
|
141
|
1 |
|
return; |
|
142
|
|
|
} |
|
143
|
1 |
|
array_walk($meta["styles"], function(&$value) use($basePath) { |
|
144
|
1 |
|
$this->addAsset("$basePath/$value"); |
|
145
|
1 |
|
$value = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$value\">"; |
|
146
|
1 |
|
}); |
|
147
|
1 |
|
$meta["styles"] = implode("\n ", $meta["styles"]); |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
|
View Code Duplication |
protected function normalizeScripts(array &$meta, string &$html, string $filename): void { |
|
|
|
|
|
|
151
|
1 |
|
$basePath = dirname($filename); |
|
152
|
1 |
|
$meta["scripts"] = array_filter($meta["scripts"], function($value) use($basePath) { |
|
153
|
1 |
|
return file_exists("$basePath/$value"); |
|
154
|
1 |
|
}); |
|
155
|
1 |
|
if(!count($meta["scripts"])) { |
|
156
|
1 |
|
unset($meta["scripts"]); |
|
157
|
1 |
|
$html = str_replace(" |
|
158
|
1 |
|
%%scripts%%", "", $html); |
|
159
|
1 |
|
return; |
|
160
|
|
|
} |
|
161
|
1 |
|
array_walk($meta["scripts"], function(&$value) use($basePath) { |
|
162
|
1 |
|
$this->addAsset("$basePath/$value"); |
|
163
|
1 |
|
$value = "<script type=\"text/javascript\" src=\"$value\"></script>"; |
|
164
|
1 |
|
}); |
|
165
|
1 |
|
$meta["scripts"] = implode("\n ", $meta["scripts"]); |
|
166
|
1 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
protected function createMarkdownParser(): \cebe\markdown\Markdown { |
|
169
|
1 |
|
$parser = new class extends GithubMarkdown { |
|
|
|
|
|
|
170
|
|
|
public function parse($text): string { |
|
171
|
1 |
|
$markup = parent::parse($text); |
|
172
|
1 |
|
if(substr($markup, -1) === PHP_EOL) { |
|
173
|
1 |
|
$markup = substr($markup, 0, -1); |
|
174
|
|
|
} |
|
175
|
1 |
|
return $markup; |
|
176
|
|
|
} |
|
177
|
|
|
}; |
|
178
|
1 |
|
$parser->html5 = $parser->keepListStartNumber = $parser->enableNewlines = true; |
|
179
|
1 |
|
return $parser; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
protected function createHtml(string $filename): string { |
|
183
|
1 |
|
$parser = $this->createMarkdownParser(); |
|
184
|
1 |
|
$source = $parser->parse(file_get_contents($filename)); |
|
185
|
1 |
|
$html = file_get_contents($this->templateFile); |
|
186
|
1 |
|
$html = str_replace("%%source%%", $source, $html); |
|
187
|
1 |
|
return $html; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @internal |
|
192
|
|
|
*/ |
|
193
|
|
|
public function clearOutputFolder(): void { |
|
194
|
1 |
|
FileSystem::delete($this->output); |
|
195
|
1 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @internal |
|
199
|
|
|
*/ |
|
200
|
|
|
public function copyAssets(): void { |
|
201
|
1 |
|
foreach($this->assets as $asset) { |
|
202
|
1 |
|
$path = str_replace($this->source, "", $asset); |
|
203
|
1 |
|
$target = "$this->output$path"; |
|
204
|
1 |
|
FileSystem::copy($asset, $target); |
|
205
|
1 |
|
echo "Copied $path"; |
|
206
|
|
|
} |
|
207
|
1 |
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @internal |
|
211
|
|
|
*/ |
|
212
|
|
|
public function processImages(string $html, self $generator, string $filename): void { |
|
213
|
1 |
|
$dom = new \DOMDocument(); |
|
214
|
1 |
|
$dom->loadHTML($html); |
|
215
|
1 |
|
$images = $dom->getElementsByTagName("img"); |
|
216
|
|
|
/** @var \DOMElement $image */ |
|
217
|
1 |
|
foreach($images as $image) { |
|
218
|
1 |
|
$path = dirname($filename) . "/" . $image->getAttribute("src"); |
|
219
|
1 |
|
if(file_exists($path)) { |
|
220
|
1 |
|
$generator->addAsset($path); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
1 |
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Generate the site |
|
227
|
|
|
*/ |
|
228
|
|
|
public function generate(): void { |
|
229
|
1 |
|
$this->onBeforeGenerate(); |
|
230
|
1 |
|
$files = Finder::findFiles("*.md") |
|
231
|
1 |
|
->exclude($this->ignoredFiles) |
|
232
|
1 |
|
->from($this->source) |
|
233
|
1 |
|
->exclude($this->ignoredFolders); |
|
234
|
|
|
/** @var \SplFileInfo $file */ |
|
235
|
1 |
|
foreach($files as $file) { |
|
236
|
1 |
|
$path = str_replace($this->source, "", dirname($file->getRealPath())); |
|
237
|
1 |
|
$html = $this->createHtml($file->getRealPath()); |
|
238
|
1 |
|
$meta = $this->getMeta($file->getRealPath(), $html); |
|
239
|
1 |
|
foreach($meta as $key => $value) { |
|
240
|
1 |
|
$html = str_replace("%%$key%%", $value, $html); |
|
241
|
|
|
} |
|
242
|
1 |
|
$basename = $file->getBasename(".md") . ".html"; |
|
243
|
1 |
|
$filename = "$this->output$path/$basename"; |
|
244
|
1 |
|
FileSystem::write($filename, $html); |
|
245
|
1 |
|
echo "Created $path/$basename\n"; |
|
246
|
1 |
|
$this->onCreatePage($html, $this, $file->getRealPath()); |
|
247
|
|
|
} |
|
248
|
1 |
|
$this->onAfterGenerate(); |
|
249
|
1 |
|
} |
|
250
|
|
|
} |
|
251
|
|
|
?> |