-
Notifications
You must be signed in to change notification settings - Fork 22
/
Builder.php
455 lines (372 loc) · 11.2 KB
/
Builder.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
namespace Gregwar\RST;
/**
* A builder can parses a whole directory to build the target architecture
* of a document
*/
class Builder
{
const NO_PARSE = 1;
const PARSE = 2;
// Tree index name
protected $indexName = 'index';
// Error manager
protected $errorManager = null;
// Verbose build ?
protected $verbose = true;
// Files to copy at the end of the build
protected $toCopy = array();
protected $toMkdir = array();
// Source and target directory
protected $directory;
protected $targetDirectory;
// Metas for documents
protected $metas;
// States (decision) of the scanned documents
protected $states = array();
// Queue of documents to be parsed
protected $parseQueue = array();
// Parsed documents waiting to be rendered
protected $documents = array();
// Kernel
protected $kernel;
// Hooks before the parsing on the environment
protected $beforeHooks = array();
// Hooks after the parsing
protected $hooks = array();
// Use relative URLs
protected $relativeUrls = true;
public function __construct($kernel = null)
{
$this->errorManager = new ErrorManager;
if ($kernel) {
$this->kernel = $kernel;
} else {
$this->kernel = new HTML\Kernel;
}
$this->kernel->initBuilder($this);
}
public function getErrorManager()
{
return $this->errorManager;
}
/**
* Adds an hook which will be called on each document after parsing
*/
public function addHook($function)
{
$this->hooks[] = $function;
return $this;
}
/**
* Adds an hook which will be called on each environment during building
*/
public function addBeforeHook($function)
{
$this->beforeHooks[] = $function;
return $this;
}
protected function display($text)
{
if ($this->verbose) {
echo $text."\n";
}
}
public function build($directory, $targetDirectory = 'output', $verbose = true)
{
$this->verbose = $verbose;
$this->directory = $directory;
$this->targetDirectory = $targetDirectory;
// Creating output directory if doesn't exists
if (!is_dir($targetDirectory)) {
mkdir($targetDirectory, 0755, true);
}
// Try to load metas, if it does not exists, create it
$this->display('* Loading metas');
$this->metas = new Metas($this->loadMetas());
// Scan all the metas and the index
$this->display('* Pre-scanning files');
$this->scan($this->getIndexName());
$this->scanMetas();
// Parses all the documents
$this->parseAll();
// Renders all the documents
$this->render();
// Saving the meta
$this->display('* Writing metas');
$this->saveMetas();
// Copy the files
$this->display('* Running the copies');
$this->doMkdir();
$this->doCopy();
}
/**
* Renders all the pending documents
*/
protected function render()
{
$this->display('* Rendering documents');
foreach ($this->documents as $file => &$document) {
$this->display(' -> Rendering '.$file.'...');
$target = $this->getTargetOf($file);
$directory = dirname($target);
if (!is_dir($directory)) {
mkdir($directory, 0755, true);
}
file_put_contents($target, $document->renderDocument());
}
}
/**
* Adding a file to the parse queue
*/
protected function addToParseQueue($file)
{
$this->states[$file] = self::PARSE;
if (!isset($this->documents[$file])) {
$this->parseQueue[$file] = $file;
}
}
/**
* Returns the next file to parse
*/
protected function getFileToParse()
{
if ($this->parseQueue) {
return array_shift($this->parseQueue);
} else {
return null;
}
}
/**
* Parses all the document that need to be parsed
*/
protected function parseAll()
{
$this->display('* Parsing files');
while ($file = $this->getFileToParse()) {
$this->display(' -> Parsing '.$file.'...');
// Process the file
$rst = $this->getRST($file);
$parser = new Parser(null, $this->kernel);
$environment = $parser->getEnvironment();
$environment->setMetas($this->metas);
$environment->setCurrentFilename($file);
$environment->setCurrentDirectory($this->directory);
$environment->setTargetDirectory($this->targetDirectory);
$environment->setErrorManager($this->errorManager);
$environment->setUseRelativeUrls($this->relativeUrls);
foreach ($this->beforeHooks as $hook) {
$hook($parser);
}
if (!file_exists($rst)) {
$this->errorManager->error('Can\'t parse the file '.$rst);
continue;
}
$document = $this->documents[$file] = $parser->parseFile($rst);
// Calling all the post-process hooks
foreach ($this->hooks as $hook) {
$hook($document);
}
// Calling the kernel document tweaking
$this->kernel->postParse($document);
$dependencies = $document->getEnvironment()->getDependencies();
if ($dependencies) {
$this->display(' -> Scanning dependencies of '.$file.'...');
// Scan the dependencies for this document
foreach ($dependencies as $dependency) {
$this->scan($dependency);
}
}
// Append the meta for this document
$this->metas->set(
$file,
$this->getUrl($document),
$document->getTitle(),
$document->getTitles(),
$document->getTocs(),
filectime($rst),
$dependencies
);
}
}
/**
* Scans a file, this will check the status of the file and tell if it
* needs to be parsed or not
*/
public function scan($file)
{
// If no decision is already made about this file
if (!isset($this->states[$file])) {
$this->display(' -> Scanning '.$file.'...');
$this->states[$file] = self::NO_PARSE;
$entry = $this->metas->get($file);
$rst = $this->getRST($file);
if (!$entry || !file_exists($rst) || $entry['ctime'] < filectime($rst)) {
// File was never seen or changed and thus need to be parsed
$this->addToParseQueue($file);
} else {
// Have a look to the file dependencies to knoww if you need to parse
// it or not
$depends = $entry['depends'];
if (isset($entry['parent'])) {
$depends[] = $entry['parent'];
}
foreach ($depends as $dependency) {
$this->scan($dependency);
// If any dependency needs to be parsed, this file needs also to be
// parsed
if ($this->states[$dependency] == self::PARSE) {
$this->addToParseQueue($file);
}
}
}
}
}
/**
* Scans all the metas
*/
public function scanMetas()
{
$entries = $this->metas->getAll();
foreach ($entries as $file => $infos) {
$this->scan($file);
}
}
/**
* Get the meta file name
*/
protected function getMetaFile()
{
return $this->getTargetFile('meta.php');
}
/**
* Try to inport the metas from the meta files
*/
protected function loadMetas()
{
$metaFile = $this->getMetaFile();
if (file_exists($metaFile)) {
return @include($metaFile);
}
return null;
}
/**
* Saving the meta files
*/
protected function saveMetas()
{
$metas = '<?php return '.var_export($this->metas->getAll(), true).';';
file_put_contents($this->getMetaFile(), $metas);
}
/**
* Gets the .rst of a source file
*/
public function getRST($file)
{
return $this->getSourceFile($file . '.rst');
}
/**
* Gets the name of a target for a file, for instance /introduction/part1 could
* be resolved into /path/to/introduction/part1.html
*/
public function getTargetOf($file)
{
$meta = $this->metas->get($file);
return $this->getTargetFile($meta['url']);
}
/**
* Gets the URL of a target file
*/
public function getUrl($document)
{
$environment = $document->getEnvironment();
return $environment->getUrl() . '.' . $this->kernel->getFileExtension();
}
/**
* Gets the name of a target file
*/
public function getTargetFile($filename)
{
return $this->targetDirectory . '/' . $filename;
}
/**
* Gets the name of a source file
*/
public function getSourceFile($filename)
{
return $this->directory . '/' . $filename;
}
/**
* Is the given path absolute ?
*/
protected function isAbsolute(string $path)
{
return $path[0] === DIRECTORY_SEPARATOR || preg_match('~\A[A-Z]:(?![^/\\\\])~i', $path) > 0;
}
/**
* Run the copy
*/
public function doCopy()
{
foreach ($this->toCopy as $copy) {
list($source, $destination) = $copy;
if (!$this->isAbsolute($source)) {
$source = $this->getSourceFile($source);
}
$destination = $this->getTargetFile($destination);
if (is_dir($source) && is_dir($destination)) {
$destination = dirname($destination);
}
shell_exec('cp -R '.$source.' '.$destination);
}
}
/**
* Add a file to copy
*/
public function copy($source, $destination = null)
{
if ($destination === null) {
$destination = basename($source);
}
$this->toCopy[] = array($source, $destination);
return $this;
}
/**
* Run the directories creation
*/
public function doMkdir()
{
foreach ($this->toMkdir as $mkdir) {
$dir = $this->getTargetFile($mkdir);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
}
}
/**
* Creates a directory in the target
*
* @param $directory the directory name to create
*/
public function mkdir($directory)
{
$this->toMkdir[] = $directory;
return $this;
}
public function setIndexName($name)
{
$this->indexName = $name;
return $this;
}
public function getIndexName()
{
return $this->indexName;
}
/**
* Use relative URLs for links
*/
public function setUseRelativeUrls($enable)
{
$this->relativeUrls = $enable;
}
}