1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\SiteGenerator; |
5
|
|
|
|
6
|
1 |
|
require_once(__DIR__ . "/functions.php"); |
7
|
|
|
|
8
|
|
|
use cebe\markdown\GithubMarkdown, |
9
|
|
|
Nette\Utils\Finder, |
10
|
|
|
Nette\Neon\Neon, |
11
|
|
|
Nette\Utils\FileSystem, |
12
|
|
|
Symfony\Component\OptionsResolver\OptionsResolver; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generator |
16
|
|
|
* |
17
|
|
|
* @author Jakub Konečný |
18
|
|
|
* @property string $source |
19
|
|
|
* @property string $output |
20
|
|
|
*/ |
21
|
1 |
|
class Generator { |
22
|
1 |
|
use \Nette\SmartObject; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $source; |
26
|
|
|
/** @var string */ |
27
|
|
|
protected $output; |
28
|
|
|
/** @var string[] */ |
29
|
|
|
protected $assets = []; |
30
|
|
|
|
31
|
|
|
public function __construct(string $source = NULL, string $output = NULL) { |
32
|
1 |
|
if(is_null($source)) { |
33
|
1 |
|
$source = findVendorDirectory() . "/../"; |
|
|
|
|
34
|
|
|
} |
35
|
1 |
|
$this->setSource($source); |
36
|
1 |
|
if(is_null($output)) { |
37
|
1 |
|
$output = findVendorDirectory() . "/../public/"; |
|
|
|
|
38
|
|
|
} |
39
|
1 |
|
FileSystem::createDir($output); |
40
|
1 |
|
$this->setOutput($output); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
public function getSource(): string { |
|
|
|
|
44
|
1 |
|
return $this->source; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setSource(string $source) { |
48
|
1 |
|
if(is_string($source) AND is_dir($source)) { |
49
|
1 |
|
$this->source = realpath($source); |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
public function getOutput(): string { |
|
|
|
|
54
|
1 |
|
return $this->output; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setOutput(string $output) { |
58
|
1 |
|
if(is_string($output)) { |
59
|
1 |
|
$this->output = realpath($output); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
protected function getMeta(string $filename): array { |
|
|
|
|
64
|
1 |
|
$resolver = new OptionsResolver; |
65
|
1 |
|
$resolver->setDefaults([ |
66
|
1 |
|
"title" => "", |
67
|
|
|
"styles" => [], |
68
|
|
|
"scripts" => [], |
69
|
|
|
]); |
70
|
1 |
|
$isArrayOfStrings = function(array $value) { |
71
|
1 |
|
foreach($value as $item) { |
72
|
1 |
|
if(!is_string($item)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
1 |
|
return true; |
77
|
|
|
}; |
78
|
1 |
|
$resolver->setAllowedTypes("title", "string"); |
79
|
1 |
|
$resolver->setAllowedTypes("styles", "array"); |
80
|
1 |
|
$resolver->setAllowedValues("styles", $isArrayOfStrings); |
81
|
1 |
|
$resolver->setAllowedTypes("scripts", "array"); |
82
|
1 |
|
$resolver->setAllowedValues("scripts", $isArrayOfStrings); |
83
|
1 |
|
$metaFilename = str_replace(".md", ".neon", $filename); |
84
|
1 |
|
$meta = []; |
|
|
|
|
85
|
1 |
|
if(file_exists($metaFilename)) { |
86
|
1 |
|
$meta = Neon::decode(file_get_contents($metaFilename)); |
87
|
|
|
} |
88
|
1 |
|
return $resolver->resolve($meta); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function addAsset(string $asset): void { |
|
|
|
|
92
|
1 |
|
if(!in_array($asset, $this->assets)) { |
93
|
1 |
|
$this->assets[] = realpath($asset); |
94
|
|
|
} |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
protected function processAssets(array &$meta, string &$html, string $basePath): void { |
|
|
|
|
98
|
1 |
|
foreach($meta["styles"] as $index => $style) { |
99
|
1 |
|
if(!file_exists("$basePath/$style")) { |
100
|
1 |
|
unset($meta["styles"][$index]); |
101
|
1 |
|
continue; |
102
|
|
|
} |
103
|
|
|
} |
104
|
1 |
|
foreach($meta["scripts"] as $index => $script) { |
105
|
1 |
|
if(!file_exists("$basePath/$script")) { |
106
|
1 |
|
unset($meta["scripts"][$index]); |
107
|
1 |
|
continue; |
108
|
|
|
} |
109
|
|
|
} |
110
|
1 |
|
if(!count($meta["styles"])) { |
111
|
1 |
|
unset($meta["styles"]); |
112
|
1 |
|
$html = str_replace(" |
113
|
1 |
|
%%styles%%", "", $html); |
114
|
|
|
} |
115
|
1 |
|
if(!count($meta["scripts"])) { |
116
|
1 |
|
unset($meta["scripts"]); |
117
|
1 |
|
$html = str_replace(" |
118
|
1 |
|
%%scripts%%", "", $html); |
119
|
|
|
} |
120
|
1 |
|
if(!isset($meta["styles"]) AND !isset($meta["scripts"])) { |
121
|
1 |
|
return; |
122
|
|
|
} |
123
|
1 |
View Code Duplication |
if(isset($meta["styles"])) { |
|
|
|
|
124
|
1 |
|
foreach($meta["styles"] as $index => $style) { |
125
|
1 |
|
$this->addAsset("$basePath/$style"); |
126
|
1 |
|
$meta["styles"][$index] = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$style\">"; |
127
|
|
|
} |
128
|
1 |
|
$meta["styles"] = implode("\n ", $meta["styles"]); |
129
|
|
|
} |
130
|
1 |
View Code Duplication |
if(isset($meta["scripts"])) { |
|
|
|
|
131
|
1 |
|
foreach($meta["scripts"] as $index => $script) { |
|
|
|
|
132
|
1 |
|
$this->addAsset("$basePath/$script"); |
133
|
1 |
|
$meta["scripts"][$index] = "<script type=\"text/javascript\" src=\"$script\"></script>"; |
134
|
|
|
} |
135
|
1 |
|
$meta["scripts"] = implode("\n ", $meta["scripts"]); |
136
|
|
|
} |
137
|
1 |
|
} |
138
|
|
|
|
139
|
|
|
protected function createHtml(string $filename): string { |
|
|
|
|
140
|
1 |
|
$parser = new GithubMarkdown; |
|
|
|
|
141
|
1 |
|
$parser->html5 = $parser->keepListStartNumber = $parser->enableNewlines = true; |
142
|
1 |
|
$source = $parser->parse(file_get_contents($filename)); |
|
|
|
|
143
|
1 |
|
$meta = $this->getMeta($filename); |
|
|
|
|
144
|
1 |
|
$html = file_get_contents(__DIR__ . "/template.html"); |
|
|
|
|
145
|
1 |
|
if(substr($source, -1) === PHP_EOL) { |
146
|
1 |
|
$source = substr($source, 0, -1); |
147
|
|
|
} |
148
|
1 |
|
if(strlen($meta["title"]) === 0) { |
149
|
1 |
|
unset($meta["title"]); |
150
|
1 |
|
$html = str_replace(" |
151
|
1 |
|
<title>%%title%%</title>", "", $html); |
152
|
|
|
} |
153
|
1 |
|
$this->processAssets($meta, $html, dirname($filename)); |
154
|
1 |
|
$meta["source"] = $source; |
155
|
1 |
|
foreach($meta as $key => $value) { |
156
|
1 |
|
$html = str_replace("%%$key%%", $value, $html); |
157
|
|
|
} |
158
|
1 |
|
return $html; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Generate the site |
163
|
|
|
*/ |
164
|
|
|
public function generate(): void { |
|
|
|
|
165
|
1 |
|
FileSystem::delete($this->output); |
166
|
1 |
|
$files = Finder::findFiles("*.md") |
167
|
1 |
|
->exclude("README.md") |
168
|
1 |
|
->from($this->source) |
169
|
1 |
|
->exclude("vendor", ".git", "tests"); |
170
|
|
|
/** @var \SplFileInfo $file */ |
171
|
1 |
|
foreach($files as $file) { |
172
|
1 |
|
$path = dirname($file->getRealPath()); |
|
|
|
|
173
|
1 |
|
$path = str_replace($this->source, "", $path); |
|
|
|
|
174
|
1 |
|
$html = $this->createHtml($file->getRealPath()); |
|
|
|
|
175
|
1 |
|
$filename = "$this->output$path/{$file->getBasename(".md")}.html"; |
176
|
1 |
|
FileSystem::write($filename, $html); |
177
|
1 |
|
echo "Created $path/{$file->getBasename(".md")}.html\n"; |
178
|
|
|
} |
179
|
1 |
|
foreach($this->assets as $asset) { |
180
|
1 |
|
$path = str_replace($this->source, "", $asset); |
|
|
|
|
181
|
1 |
|
$target = "$this->output$path"; |
182
|
1 |
|
FileSystem::copy($asset, $target); |
183
|
1 |
|
echo "Copied $path"; |
184
|
|
|
} |
185
|
1 |
|
} |
186
|
|
|
} |
187
|
|
|
?> |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.