|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\FrontMatter; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\Document\JailedDocumentInterface; |
|
11
|
|
|
use allejo\stakx\Document\PermalinkDocument; |
|
12
|
|
|
use allejo\stakx\Exception\FileAwareException; |
|
13
|
|
|
use allejo\stakx\Exception\InvalidSyntaxException; |
|
14
|
|
|
use allejo\stakx\FrontMatter\Exception\YamlVariableUndefinedException; |
|
15
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
|
16
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
17
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
|
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
19
|
|
|
|
|
20
|
|
|
abstract class FrontMatterDocument extends PermalinkDocument implements |
|
21
|
|
|
\ArrayAccess, |
|
22
|
|
|
JailedDocumentInterface, |
|
23
|
|
|
WritableDocumentInterface |
|
24
|
|
|
{ |
|
25
|
|
|
const TEMPLATE = "---\n%s\n---\n\n%s"; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The names of FrontMatter keys that are specially defined for all Documents |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
public static $specialFrontMatterKeys = array( |
|
|
|
|
|
|
33
|
|
|
'filename', 'basename' |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
protected static $whiteListFunctions = array( |
|
37
|
|
|
'getPermalink', 'getRedirects', 'getTargetFile', 'getName', 'getFilePath', 'getRelativeFilePath', 'getContent', |
|
38
|
|
|
'getExtension', 'getFrontMatter' |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* An array to keep track of collection or data dependencies used inside of a Twig template. |
|
43
|
|
|
* |
|
44
|
|
|
* $dataDependencies['collections'] = array() |
|
45
|
|
|
* $dataDependencies['data'] = array() |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $dataDependencies; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* FrontMatter values that can be injected or set after the file has been parsed. Values in this array will take |
|
53
|
|
|
* precedence over values in $frontMatter. |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $writableFrontMatter; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* A list of Front Matter values that should not be returned directly from the $frontMatter array. Values listed |
|
61
|
|
|
* here have dedicated functions that handle those Front Matter values and the respective functions should be called |
|
62
|
|
|
* instead. |
|
63
|
|
|
* |
|
64
|
|
|
* @var string[] |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $frontMatterBlacklist; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set to true if the front matter has already been evaluated with variable interpolation. |
|
70
|
|
|
* |
|
71
|
|
|
* @var bool |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $frontMatterEvaluated; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var Parser |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $frontMatterParser; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* An array containing the Yaml of the file. |
|
82
|
|
|
* |
|
83
|
|
|
* @var array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $frontMatter; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set to true if the body has already been parsed as markdown or any other format. |
|
89
|
|
|
* |
|
90
|
|
|
* @var bool |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $bodyContentEvaluated; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Only the body of the file, i.e. the content. |
|
96
|
|
|
* |
|
97
|
|
|
* @var string |
|
98
|
|
|
*/ |
|
99
|
|
|
protected $bodyContent; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* The number of lines that Twig template errors should offset. |
|
103
|
|
|
* |
|
104
|
|
|
* @var int |
|
105
|
|
|
*/ |
|
106
|
|
|
private $lineOffset; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* ContentItem constructor. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $filePath The path to the file that will be parsed into a ContentItem |
|
112
|
|
|
* |
|
113
|
|
|
* @throws FileNotFoundException The given file path does not exist |
|
114
|
|
|
* @throws IOException The file was not a valid ContentItem. This would meam there was no front matter or |
|
115
|
|
|
* no body |
|
116
|
|
|
*/ |
|
117
|
132 |
|
public function __construct($filePath) |
|
118
|
|
|
{ |
|
119
|
132 |
|
$this->frontMatterBlacklist = array('permalink', 'redirects'); |
|
120
|
132 |
|
$this->writableFrontMatter = array(); |
|
121
|
|
|
|
|
122
|
132 |
|
parent::__construct($filePath); |
|
123
|
120 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Return the body of the Content Item. |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
abstract public function getContent(); |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* The number of lines that are taken up by FrontMatter and white space. |
|
134
|
|
|
* |
|
135
|
|
|
* @return int |
|
136
|
|
|
*/ |
|
137
|
|
|
final public function getLineOffset() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->lineOffset; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the name of the item, which is just the filename without the extension. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
62 |
|
final public function getName() |
|
148
|
|
|
{ |
|
149
|
62 |
|
return $this->getBaseName(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Check whether this object has a reference to a collection or data item. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $namespace 'collections' or 'data' |
|
156
|
|
|
* @param string $needle |
|
157
|
|
|
* |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
12 |
|
final public function hasTwigDependency($namespace, $needle) |
|
161
|
|
|
{ |
|
162
|
|
|
return |
|
163
|
12 |
|
in_array($needle, $this->dataDependencies[$namespace]) || |
|
164
|
12 |
|
(is_null($needle) && !empty($this->dataDependencies[$namespace])); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Read the file, and parse its contents. |
|
169
|
|
|
*/ |
|
170
|
132 |
|
final public function refreshFileContent() |
|
171
|
|
|
{ |
|
172
|
|
|
// This function can be called after the initial object was created and the file may have been deleted since the |
|
173
|
|
|
// creation of the object. |
|
174
|
132 |
View Code Duplication |
if (!$this->fs->exists($this->filePath)) |
|
|
|
|
|
|
175
|
132 |
|
{ |
|
176
|
2 |
|
throw new FileNotFoundException(null, 0, null, $this->filePath); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// $fileStructure[1] is the YAML |
|
|
|
|
|
|
180
|
|
|
// $fileStructure[2] is the amount of new lines after the closing `---` and the beginning of content |
|
181
|
|
|
// $fileStructure[3] is the body of the document |
|
182
|
131 |
|
$fileStructure = array(); |
|
183
|
|
|
|
|
184
|
131 |
|
$rawFileContents = file_get_contents($this->filePath); |
|
185
|
131 |
|
preg_match('/---\R(.*?\R)?---(\s+)(.*)/s', $rawFileContents, $fileStructure); |
|
186
|
|
|
|
|
187
|
131 |
|
if (count($fileStructure) != 4) |
|
188
|
131 |
|
{ |
|
189
|
9 |
|
throw new InvalidSyntaxException('Invalid FrontMatter file', 0, null, $this->getRelativeFilePath()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
122 |
|
if (empty(trim($fileStructure[3]))) |
|
193
|
122 |
|
{ |
|
194
|
1 |
|
throw new InvalidSyntaxException('FrontMatter files must have a body to render', 0, null, $this->getRelativeFilePath()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// The hard coded 1 is the offset used to count the new line used after the first `---` that is not caught in the regex |
|
198
|
121 |
|
$this->lineOffset = substr_count($fileStructure[1], "\n") + substr_count($fileStructure[2], "\n") + 1; |
|
199
|
121 |
|
$this->bodyContent = $fileStructure[3]; |
|
200
|
|
|
|
|
201
|
121 |
|
if (!empty(trim($fileStructure[1]))) |
|
202
|
121 |
|
{ |
|
203
|
92 |
|
$this->frontMatter = Yaml::parse($fileStructure[1], Yaml::PARSE_DATETIME); |
|
|
|
|
|
|
204
|
|
|
|
|
205
|
92 |
|
if (!empty($this->frontMatter) && !is_array($this->frontMatter)) |
|
206
|
92 |
|
{ |
|
207
|
1 |
|
throw new ParseException('The evaluated FrontMatter should be an array'); |
|
208
|
|
|
} |
|
209
|
91 |
|
} |
|
210
|
|
|
else |
|
211
|
|
|
{ |
|
212
|
32 |
|
$this->frontMatter = array(); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
120 |
|
$this->frontMatterEvaluated = false; |
|
216
|
120 |
|
$this->bodyContentEvaluated = false; |
|
217
|
120 |
|
$this->permalink = null; |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
120 |
|
$this->findTwigDataDependencies('collections'); |
|
220
|
120 |
|
$this->findTwigDataDependencies('data'); |
|
221
|
120 |
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get all of the references to either DataItems or ContentItems inside of given string. |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $filter 'collections' or 'data' |
|
227
|
|
|
*/ |
|
228
|
120 |
|
private function findTwigDataDependencies($filter) |
|
229
|
|
|
{ |
|
230
|
120 |
|
$regex = '/{[{%](?:.+)?(?:' . $filter . ')(?:\.|\[\')?([^_\W]+)?(?:\'\])?\s+[%}]}/'; |
|
231
|
120 |
|
$results = array(); |
|
232
|
|
|
|
|
233
|
120 |
|
preg_match_all($regex, $this->bodyContent, $results); |
|
234
|
|
|
|
|
235
|
120 |
|
$this->dataDependencies[$filter] = array_unique($results[1]); |
|
236
|
120 |
|
} |
|
237
|
|
|
|
|
238
|
|
|
// |
|
239
|
|
|
// Permalink and redirect functionality |
|
240
|
|
|
// |
|
241
|
|
|
|
|
242
|
39 |
|
final protected function buildPermalink() |
|
243
|
|
|
{ |
|
244
|
39 |
|
if (!is_null($this->permalink)) |
|
245
|
39 |
|
{ |
|
246
|
8 |
|
return; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
37 |
|
if (!is_null($this->frontMatterParser) && $this->frontMatterParser->hasExpansion()) |
|
250
|
37 |
|
{ |
|
251
|
|
|
throw new \Exception('The permalink for this item has not been set'); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
37 |
|
$permalink = (is_array($this->frontMatter) && isset($this->frontMatter['permalink'])) ? |
|
255
|
37 |
|
$this->frontMatter['permalink'] : $this->getPathPermalink(); |
|
256
|
|
|
|
|
257
|
37 |
View Code Duplication |
if (is_array($permalink)) |
|
|
|
|
|
|
258
|
37 |
|
{ |
|
259
|
19 |
|
$this->permalink = $permalink[0]; |
|
260
|
19 |
|
array_shift($permalink); |
|
261
|
19 |
|
$this->redirects = $permalink; |
|
262
|
19 |
|
} |
|
263
|
|
|
else |
|
264
|
|
|
{ |
|
265
|
24 |
|
$this->permalink = $permalink; |
|
266
|
24 |
|
$this->redirects = array(); |
|
267
|
|
|
} |
|
268
|
37 |
|
} |
|
269
|
|
|
|
|
270
|
|
|
// |
|
271
|
|
|
// WritableFrontMatter Implementation |
|
272
|
|
|
// |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* {@inheritdoc} |
|
276
|
|
|
*/ |
|
277
|
7 |
|
final public function evaluateFrontMatter($variables = null) |
|
278
|
|
|
{ |
|
279
|
7 |
|
if (!is_null($variables)) |
|
280
|
7 |
|
{ |
|
281
|
7 |
|
$this->frontMatter = array_merge($this->frontMatter, $variables); |
|
282
|
7 |
|
$this->evaluateYaml($this->frontMatter); |
|
283
|
7 |
|
} |
|
284
|
7 |
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* {@inheritdoc} |
|
288
|
|
|
*/ |
|
289
|
29 |
|
final public function getFrontMatter($evaluateYaml = true) |
|
290
|
|
|
{ |
|
291
|
29 |
|
if (is_null($this->frontMatter)) |
|
292
|
29 |
|
{ |
|
293
|
|
|
$this->frontMatter = array(); |
|
294
|
|
|
} |
|
295
|
29 |
|
elseif (!$this->frontMatterEvaluated && $evaluateYaml) |
|
296
|
|
|
{ |
|
297
|
23 |
|
$this->evaluateYaml($this->frontMatter); |
|
298
|
22 |
|
} |
|
299
|
|
|
|
|
300
|
28 |
|
return $this->frontMatter; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* {@inheritdoc} |
|
305
|
|
|
*/ |
|
306
|
2 |
|
final public function hasExpandedFrontMatter() |
|
307
|
|
|
{ |
|
308
|
2 |
|
return !is_null($this->frontMatterParser) && $this->frontMatterParser->hasExpansion(); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* {@inheritdoc. |
|
313
|
|
|
*/ |
|
314
|
|
|
final public function appendFrontMatter(array $frontMatter) |
|
315
|
|
|
{ |
|
316
|
|
|
foreach ($frontMatter as $key => $value) |
|
317
|
|
|
{ |
|
318
|
|
|
$this->writableFrontMatter[$key] = $value; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* {@inheritdoc. |
|
324
|
|
|
*/ |
|
325
|
|
|
final public function deleteFrontMatter($key) |
|
326
|
|
|
{ |
|
327
|
|
|
if (!isset($this->writableFrontMatter[$key])) |
|
328
|
|
|
{ |
|
329
|
|
|
return; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
unset($this->writableFrontMatter[$key]); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* {@inheritdoc. |
|
337
|
|
|
*/ |
|
338
|
2 |
|
final public function setFrontMatter(array $frontMatter) |
|
339
|
|
|
{ |
|
340
|
2 |
|
if (!is_array($frontMatter)) |
|
341
|
2 |
|
{ |
|
342
|
|
|
throw new \InvalidArgumentException('An array is required for setting the writable FrontMatter'); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
2 |
|
$this->writableFrontMatter = $frontMatter; |
|
346
|
2 |
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Evaluate an array of data for FrontMatter variables. This function will modify the array in place. |
|
350
|
|
|
* |
|
351
|
|
|
* @param array $yaml An array of data containing FrontMatter variables |
|
352
|
|
|
* |
|
353
|
|
|
* @throws YamlVariableUndefinedException A FrontMatter variable used does not exist |
|
354
|
|
|
*/ |
|
355
|
30 |
|
private function evaluateYaml(&$yaml) |
|
356
|
|
|
{ |
|
357
|
|
|
try |
|
358
|
|
|
{ |
|
359
|
30 |
|
$this->frontMatterParser = new Parser($yaml, array( |
|
360
|
30 |
|
'filename' => $this->getFileName(), |
|
361
|
30 |
|
'basename' => $this->getName(), |
|
362
|
30 |
|
)); |
|
363
|
29 |
|
$this->frontMatterEvaluated = true; |
|
364
|
|
|
} |
|
365
|
30 |
|
catch (\Exception $e) |
|
366
|
|
|
{ |
|
367
|
1 |
|
throw FileAwareException::castException($e, $this->getRelativeFilePath()); |
|
368
|
|
|
} |
|
369
|
29 |
|
} |
|
370
|
|
|
|
|
371
|
|
|
// |
|
372
|
|
|
// ArrayAccess Implementation |
|
373
|
|
|
// |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* {@inheritdoc} |
|
377
|
|
|
*/ |
|
378
|
|
|
public function offsetSet($offset, $value) |
|
379
|
|
|
{ |
|
380
|
|
|
if (is_null($offset)) |
|
381
|
|
|
{ |
|
382
|
|
|
throw new \InvalidArgumentException('$offset cannot be null'); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
$this->writableFrontMatter[$offset] = $value; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* {@inheritdoc} |
|
390
|
|
|
*/ |
|
391
|
34 |
|
public function offsetExists($offset) |
|
392
|
|
|
{ |
|
393
|
34 |
|
if (isset($this->writableFrontMatter[$offset]) || isset($this->frontMatter[$offset])) |
|
394
|
34 |
|
{ |
|
395
|
33 |
|
return true; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
14 |
|
$fxnCall = 'get' . ucfirst($offset); |
|
399
|
|
|
|
|
400
|
14 |
|
return method_exists($this, $fxnCall) && in_array($fxnCall, static::$whiteListFunctions); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* {@inheritdoc} |
|
405
|
|
|
*/ |
|
406
|
|
|
public function offsetUnset($offset) |
|
407
|
|
|
{ |
|
408
|
|
|
unset($this->writableFrontMatter[$offset]); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* {@inheritdoc} |
|
413
|
|
|
*/ |
|
414
|
51 |
|
public function offsetGet($offset) |
|
415
|
|
|
{ |
|
416
|
51 |
|
$fxnCall = 'get' . ucfirst($offset); |
|
417
|
|
|
|
|
418
|
51 |
|
if (in_array($fxnCall, self::$whiteListFunctions) && method_exists($this, $fxnCall)) |
|
419
|
51 |
|
{ |
|
420
|
6 |
|
return call_user_func_array(array($this, $fxnCall), array()); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
45 |
|
if (isset($this->writableFrontMatter[$offset])) |
|
424
|
45 |
|
{ |
|
425
|
|
|
return $this->writableFrontMatter[$offset]; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
45 |
|
if (isset($this->frontMatter[$offset])) |
|
429
|
45 |
|
{ |
|
430
|
44 |
|
return $this->frontMatter[$offset]; |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
5 |
|
return null; |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.