1 | <?php |
||
12 | class FrontMatterParser |
||
13 | { |
||
14 | /** |
||
15 | * The RegEx used to identify Front Matter variables |
||
16 | */ |
||
17 | const VARIABLE_DEF = '/(?<!\\\\)%([a-zA-Z]+)/'; |
||
18 | |||
19 | /** |
||
20 | * A list of special fields in the Front Matter that will support expansion |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | private static $expandableFields = array('permalink'); |
||
25 | |||
26 | /** |
||
27 | * Whether or not an field was expanded into several values |
||
28 | * |
||
29 | * Only fields specified in $expandableFields will cause this value to be set to true |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $expansionUsed; |
||
34 | |||
35 | /** |
||
36 | * The current depth of the recursion for evaluating nested arrays in the Front Matter |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | private $nestingLevel; |
||
41 | |||
42 | /** |
||
43 | * The current hierarchy of the keys that are being evaluated |
||
44 | * |
||
45 | * Since arrays can be nested, we'll keep track of the keys up until the current depth. This information is used for |
||
46 | * error reporting |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $yamlKeys; |
||
51 | |||
52 | /** |
||
53 | * The entire Front Matter block; evaluation will happen in place |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $frontMatter; |
||
58 | |||
59 | /** |
||
60 | * FrontMatterParser constructor |
||
61 | * |
||
62 | * @param array $rawFrontMatter |
||
63 | */ |
||
64 | 21 | public function __construct(&$rawFrontMatter) |
|
75 | |||
76 | /** |
||
77 | * True if any fields were expanded in the Front Matter block |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | 6 | public function hasExpansion () |
|
85 | |||
86 | // |
||
87 | // Special FrontMatter fields |
||
88 | // |
||
89 | |||
90 | /** |
||
91 | * Special treatment for some FrontMatter variables |
||
92 | */ |
||
93 | 21 | private function handleSpecialFrontMatter () |
|
97 | |||
98 | /** |
||
99 | * Special treatment for the `date` field in FrontMatter that creates three new variables: year, month, day |
||
100 | */ |
||
101 | 21 | private function handleDateField () |
|
102 | { |
||
103 | 21 | if (!isset($this->frontMatter['date'])) { return; } |
|
104 | |||
105 | try |
||
106 | { |
||
107 | // Coming from a string variable |
||
108 | 5 | $itemDate = new \DateTime($this->frontMatter['date']); |
|
109 | } |
||
110 | 5 | catch (\Exception $e) |
|
111 | { |
||
112 | // YAML has parsed them to Epoch time |
||
113 | 1 | $itemDate = \DateTime::createFromFormat('U', $this->frontMatter['date']); |
|
114 | |||
115 | // Localize dates in FrontMatter based on the timezone set in the PHP configuration |
||
116 | 1 | $timezone = new \DateTimeZone(date_default_timezone_get()); |
|
117 | 1 | $itemDate = new \DateTime($itemDate->format('Y-m-d h:i:s'), $timezone); |
|
118 | } |
||
119 | |||
120 | 5 | if (!$itemDate === false) |
|
121 | 5 | { |
|
122 | 5 | $this->frontMatter['date'] = $itemDate->format('U'); |
|
123 | 5 | $this->frontMatter['year'] = $itemDate->format('Y'); |
|
124 | 5 | $this->frontMatter['month'] = $itemDate->format('m'); |
|
125 | 5 | $this->frontMatter['day'] = $itemDate->format('d'); |
|
126 | 5 | } |
|
127 | 5 | } |
|
128 | |||
129 | // |
||
130 | // Evaluation |
||
131 | // |
||
132 | |||
133 | /** |
||
134 | * Evaluate an array as Front Matter |
||
135 | * |
||
136 | * @param array $yaml |
||
137 | */ |
||
138 | 21 | private function evaluateBlock (&$yaml) |
|
164 | |||
165 | /** |
||
166 | * Evaluate an expandable field |
||
167 | * |
||
168 | * @param string $key |
||
169 | * @param string $fmStatement |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | 6 | private function evaluateExpandableField ($key, $fmStatement) |
|
199 | |||
200 | /** |
||
201 | * Convert a string or an array into an array of ExpandedValue objects created through "value expansion" |
||
202 | * |
||
203 | * @param string $frontMatterKey The current hierarchy of the Front Matter keys being used |
||
204 | * @param string $expandableValue The Front Matter value that will be expanded |
||
205 | * @param array $arrayVariableNames The Front Matter variable names that reference arrays |
||
206 | * |
||
207 | * @return array |
||
208 | * |
||
209 | * @throws YamlUnsupportedVariableException If a multidimensional array is given for value expansion |
||
210 | */ |
||
211 | 4 | private function evaluateArrayType ($frontMatterKey, $expandableValue, $arrayVariableNames) |
|
246 | |||
247 | /** |
||
248 | * Evaluate an string for FrontMatter variables and replace them with the corresponding values |
||
249 | * |
||
250 | * @param string $key The key of the Front Matter value |
||
251 | * @param string $string The string that will be evaluated |
||
252 | * @param bool $ignoreArrays When set to true, an exception won't be thrown when an array is found with the |
||
253 | * interpolation |
||
254 | * |
||
255 | * @return string The final string with variables evaluated |
||
256 | * |
||
257 | * @throws YamlUnsupportedVariableException A FrontMatter variable is not an int, float, or string |
||
258 | */ |
||
259 | 21 | private function evaluateBasicType ($key, $string, $ignoreArrays = false) |
|
279 | |||
280 | // |
||
281 | // Variable management |
||
282 | // |
||
283 | |||
284 | /** |
||
285 | * Get an array of FrontMatter variables in the specified string that need to be interpolated |
||
286 | * |
||
287 | * @param string $string |
||
288 | * |
||
289 | * @return string[] |
||
290 | */ |
||
291 | 21 | private function getFrontMatterVariables ($string) |
|
301 | |||
302 | /** |
||
303 | * Get the value of a FM variable or throw an exception |
||
304 | * |
||
305 | * @param string $key |
||
306 | * @param string $varName |
||
307 | * |
||
308 | * @return mixed |
||
309 | * @throws YamlVariableUndefinedException |
||
310 | */ |
||
311 | 15 | private function getVariableValue ($key, $varName) |
|
320 | } |