@@ -9,8 +9,8 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Pug extends Jade |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $streamName = 'pug'; |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $streamName = 'pug'; |
|
| 16 | 16 | } |
@@ -11,443 +11,443 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Compiler extends MixinVisitor |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Constants and configuration in Compiler/CompilerConfig.php. |
|
| 16 | - */ |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var |
|
| 20 | - */ |
|
| 21 | - protected $xml; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var |
|
| 25 | - */ |
|
| 26 | - protected $parentIndents; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - protected $buffer = array(); |
|
| 32 | - /** |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - protected $options = array(); |
|
| 36 | - /** |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - protected $filters = array(); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var bool |
|
| 43 | - */ |
|
| 44 | - protected $phpSingleLine = false; |
|
| 45 | - /** |
|
| 46 | - * @var bool |
|
| 47 | - */ |
|
| 48 | - protected $allowMixinOverride = false; |
|
| 49 | - /** |
|
| 50 | - * @var bool |
|
| 51 | - */ |
|
| 52 | - protected $keepNullAttributes = false; |
|
| 53 | - /** |
|
| 54 | - * @var bool |
|
| 55 | - */ |
|
| 56 | - protected $filterAutoLoad = true; |
|
| 57 | - /** |
|
| 58 | - * @var bool |
|
| 59 | - */ |
|
| 60 | - protected $terse = true; |
|
| 61 | - /** |
|
| 62 | - * @var bool |
|
| 63 | - */ |
|
| 64 | - protected $restrictedScope = false; |
|
| 65 | - /** |
|
| 66 | - * @var array |
|
| 67 | - */ |
|
| 68 | - protected $customKeywords = array(); |
|
| 69 | - /** |
|
| 70 | - * @var Jade |
|
| 71 | - */ |
|
| 72 | - protected $jade = null; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @var string |
|
| 76 | - */ |
|
| 77 | - protected $quote; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @var string |
|
| 81 | - */ |
|
| 82 | - protected $filename; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param array/Jade $options |
|
| 86 | - * @param array $filters |
|
| 87 | - */ |
|
| 88 | - public function __construct($options = array(), array $filters = array(), $filename = null) |
|
| 89 | - { |
|
| 90 | - $this->options = $this->setOptions($options); |
|
| 91 | - $this->filters = $filters; |
|
| 92 | - $this->filename = $filename; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Get a jade engine reference or an options array and return needed options. |
|
| 97 | - * |
|
| 98 | - * @param array/Jade $options |
|
| 99 | - * |
|
| 100 | - * @return array |
|
| 101 | - */ |
|
| 102 | - protected function setOptions($options) |
|
| 103 | - { |
|
| 104 | - $optionTypes = array( |
|
| 105 | - 'prettyprint' => 'boolean', |
|
| 106 | - 'phpSingleLine' => 'boolean', |
|
| 107 | - 'allowMixinOverride' => 'boolean', |
|
| 108 | - 'keepNullAttributes' => 'boolean', |
|
| 109 | - 'filterAutoLoad' => 'boolean', |
|
| 110 | - 'restrictedScope' => 'boolean', |
|
| 111 | - 'indentSize' => 'integer', |
|
| 112 | - 'indentChar' => 'string', |
|
| 113 | - 'customKeywords' => 'array', |
|
| 114 | - ); |
|
| 115 | - |
|
| 116 | - if ($options instanceof Jade) { |
|
| 117 | - $this->jade = $options; |
|
| 118 | - $options = array(); |
|
| 119 | - |
|
| 120 | - foreach ($optionTypes as $option => $type) { |
|
| 121 | - $this->$option = $this->jade->getOption($option); |
|
| 122 | - $options[$option] = $this->$option; |
|
| 123 | - settype($this->$option, $type); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $this->quote = $this->jade->getOption('singleQuote') ? '\'' : '"'; |
|
| 127 | - |
|
| 128 | - return $options; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - foreach (array_intersect_key($optionTypes, $options) as $option => $type) { |
|
| 132 | - $this->$option = $options[$option]; |
|
| 133 | - settype($this->$option, $type); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $this->quote = isset($options['singleQuote']) && $options['singleQuote'] ? '\'' : '"'; |
|
| 137 | - |
|
| 138 | - return $options; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Get an option from the jade engine if set or from the options array else. |
|
| 143 | - * |
|
| 144 | - * @param string $option |
|
| 145 | - * |
|
| 146 | - * @throws \InvalidArgumentException |
|
| 147 | - * |
|
| 148 | - * @return mixed |
|
| 149 | - */ |
|
| 150 | - public function getOption($option) |
|
| 151 | - { |
|
| 152 | - if (is_null($this->jade)) { |
|
| 153 | - if (!isset($this->options[$option])) { |
|
| 154 | - throw new \InvalidArgumentException("$option is not a valid option name.", 28); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $this->options[$option]; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $this->jade->getOption($option); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Get a compiler with the same settings. |
|
| 165 | - * |
|
| 166 | - * @return Compiler |
|
| 167 | - */ |
|
| 168 | - public function subCompiler() |
|
| 169 | - { |
|
| 170 | - return new static($this->options, $this->filters); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * php closing tag depanding on the pretty print setting. |
|
| 175 | - * |
|
| 176 | - * @return string |
|
| 177 | - */ |
|
| 178 | - protected function closingTag() |
|
| 179 | - { |
|
| 180 | - return '?>' . ($this->prettyprint ? ' ' : ''); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @param $node |
|
| 185 | - * |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 188 | - public function compile($node) |
|
| 189 | - { |
|
| 190 | - $this->visit($node); |
|
| 191 | - |
|
| 192 | - $code = ltrim(implode('', $this->buffer)); |
|
| 193 | - |
|
| 194 | - // Separate in several lines to get a useable line number in case of an error occurs |
|
| 195 | - if ($this->phpSingleLine) { |
|
| 196 | - $code = str_replace(array('<?php', '?>'), array("<?php\n", "\n" . $this->closingTag()), $code); |
|
| 197 | - } |
|
| 198 | - // Remove the $ wich are not needed |
|
| 199 | - return $code; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @param $method |
|
| 204 | - * @param $arguments |
|
| 205 | - * |
|
| 206 | - * @throws \BadMethodCallException If the 'apply' rely on non existing method |
|
| 207 | - * |
|
| 208 | - * @return mixed |
|
| 209 | - */ |
|
| 210 | - protected function apply($method, $arguments) |
|
| 211 | - { |
|
| 212 | - if (!method_exists($this, $method)) { |
|
| 213 | - throw new \BadMethodCallException(sprintf('Method %s do not exists', $method), 7); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - return call_user_func_array(array($this, $method), $arguments); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @param $line |
|
| 221 | - * @param null $indent |
|
| 222 | - */ |
|
| 223 | - protected function buffer($line, $indent = null) |
|
| 224 | - { |
|
| 225 | - if ($indent === true || ($indent === null && $this->prettyprint)) { |
|
| 226 | - $line = $this->indent() . $line . $this->newline(); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - $this->buffer[] = $line; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @param string $str |
|
| 234 | - * |
|
| 235 | - * @return bool|int |
|
| 236 | - */ |
|
| 237 | - protected function isConstant($str) |
|
| 238 | - { |
|
| 239 | - return preg_match('/^' . static::CONSTANT_VALUE . '$/', trim($str)); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @param $input |
|
| 244 | - * @param string $name |
|
| 245 | - * |
|
| 246 | - * @throws \ErrorException |
|
| 247 | - * |
|
| 248 | - * @return array |
|
| 249 | - */ |
|
| 250 | - public function handleCode($input, $name = '') |
|
| 251 | - { |
|
| 252 | - $handler = new CodeHandler($input, $name); |
|
| 253 | - |
|
| 254 | - return $handler->parse(); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @param $input |
|
| 259 | - * |
|
| 260 | - * @throws \ErrorException |
|
| 261 | - * |
|
| 262 | - * @return array |
|
| 263 | - */ |
|
| 264 | - public function handleString($input) |
|
| 265 | - { |
|
| 266 | - $result = array(); |
|
| 267 | - $resultsString = array(); |
|
| 268 | - |
|
| 269 | - $separators = preg_split( |
|
| 270 | - '/[+](?!\\()/', // concatenation operator - only js |
|
| 271 | - $input, |
|
| 272 | - -1, |
|
| 273 | - PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE |
|
| 274 | - ); |
|
| 275 | - |
|
| 276 | - foreach ($separators as $part) { |
|
| 277 | - // $sep[0] - the separator string due to PREG_SPLIT_OFFSET_CAPTURE flag |
|
| 278 | - // $sep[1] - the offset due to PREG_SPLIT_OFFSET_CAPTURE |
|
| 279 | - // @todo: = find original usage of this |
|
| 280 | - //$sep = substr( |
|
| 281 | - // $input, |
|
| 282 | - // strlen($part[0]) + $part[1] + 1, |
|
| 283 | - // isset($separators[$i+1]) ? $separators[$i+1][1] : strlen($input) |
|
| 284 | - //); |
|
| 285 | - |
|
| 286 | - // @todo: handleCode() in concat |
|
| 287 | - $part[0] = trim($part[0]); |
|
| 288 | - |
|
| 289 | - if (preg_match('/^("(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\')(.*)$/', $part[0], $match)) { |
|
| 290 | - $quote = substr($match[1], 0, 1); |
|
| 291 | - |
|
| 292 | - if (strlen(trim($match[2]))) { |
|
| 293 | - throw new \ErrorException('Unexpected value: ' . $match[2], 8); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - array_push($resultsString, $match[1]); |
|
| 297 | - |
|
| 298 | - continue; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - $code = $this->handleCode($part[0]); |
|
| 302 | - |
|
| 303 | - $result = array_merge($result, array_slice($code, 0, -1)); |
|
| 304 | - array_push($resultsString, array_pop($code)); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - array_push($result, implode(' . ', $resultsString)); |
|
| 308 | - |
|
| 309 | - return $result; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @param string $text |
|
| 314 | - * |
|
| 315 | - * @return mixed |
|
| 316 | - */ |
|
| 317 | - public function interpolate($text) |
|
| 318 | - { |
|
| 319 | - return preg_replace_callback('/(\\\\)?([#!]){(.*?)}/', array($this, 'interpolateFromCapture'), $text); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * @param array $match |
|
| 324 | - * |
|
| 325 | - * @return string |
|
| 326 | - */ |
|
| 327 | - protected function interpolateFromCapture($match) |
|
| 328 | - { |
|
| 329 | - if ($match[1] === '') { |
|
| 330 | - return $this->escapeIfNeeded($match[2] === '!', $match[3]); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - return substr($match[0], 1); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * @throws \InvalidArgumentException |
|
| 338 | - * |
|
| 339 | - * @return array |
|
| 340 | - */ |
|
| 341 | - protected function createStatements() |
|
| 342 | - { |
|
| 343 | - if (func_num_args() === 0) { |
|
| 344 | - throw new \InvalidArgumentException('No Arguments provided', 9); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - $arguments = func_get_args(); |
|
| 348 | - $statements = array(); |
|
| 349 | - $variables = array(); |
|
| 350 | - |
|
| 351 | - foreach ($arguments as $arg) { |
|
| 352 | - $arg = static::convertVarPath($arg); |
|
| 353 | - |
|
| 354 | - // add dollar if missing |
|
| 355 | - if (preg_match('/^' . static::VARNAME . '(\s*,.+)?$/', $arg)) { |
|
| 356 | - $arg = static::addDollarIfNeeded($arg); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - // shortcut for constants |
|
| 360 | - if ($this->isConstant($arg)) { |
|
| 361 | - array_push($variables, $arg); |
|
| 362 | - continue; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - // if we have a php variable assume that the string is good php |
|
| 366 | - if (strpos('{[', substr($arg, 0, 1)) === false && preg_match('/&?\${1,2}' . static::VARNAME . '|[A-Za-z0-9_\\\\]+::/', $arg)) { |
|
| 367 | - array_push($variables, $arg); |
|
| 368 | - continue; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - $code = $this->handleArgumentValue($arg); |
|
| 372 | - |
|
| 373 | - $statements = array_merge($statements, array_slice($code, 0, -1)); |
|
| 374 | - array_push($variables, array_pop($code)); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - array_push($statements, $variables); |
|
| 378 | - |
|
| 379 | - return $statements; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - protected function handleArgumentValue($arg) |
|
| 383 | - { |
|
| 384 | - if (preg_match('/^"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'/', $arg)) { |
|
| 385 | - return $this->handleString(trim($arg)); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - try { |
|
| 389 | - return $this->handleCode($arg); |
|
| 390 | - } catch (\Exception $e) { |
|
| 391 | - // if a bug occur, try to remove comments |
|
| 392 | - try { |
|
| 393 | - return $this->handleCode(preg_replace('#/\*(.*)\*/#', '', $arg)); |
|
| 394 | - } catch (\Exception $e) { |
|
| 395 | - throw new ParserException('Pug.php did not understand ' . $arg, 10, $e); |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * @param $code |
|
| 402 | - * @param null $statements |
|
| 403 | - * |
|
| 404 | - * @return string |
|
| 405 | - */ |
|
| 406 | - protected function createPhpBlock($code, $statements = null) |
|
| 407 | - { |
|
| 408 | - if ($statements === null) { |
|
| 409 | - return '<?php ' . $code . ' ' . $this->closingTag(); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - $codeFormat = array_pop($statements); |
|
| 413 | - array_unshift($codeFormat, $code); |
|
| 414 | - |
|
| 415 | - if (count($statements) === 0) { |
|
| 416 | - $phpString = call_user_func_array('sprintf', $codeFormat); |
|
| 417 | - |
|
| 418 | - return '<?php ' . $phpString . ' ' . $this->closingTag(); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - $stmtString = ''; |
|
| 422 | - foreach ($statements as $stmt) { |
|
| 423 | - $stmtString .= $this->newline() . $this->indent() . $stmt . ';'; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - $stmtString .= $this->newline() . $this->indent(); |
|
| 427 | - $stmtString .= call_user_func_array('sprintf', $codeFormat); |
|
| 428 | - |
|
| 429 | - $phpString = '<?php '; |
|
| 430 | - $phpString .= $stmtString; |
|
| 431 | - $phpString .= $this->newline() . $this->indent() . ' ' . $this->closingTag(); |
|
| 432 | - |
|
| 433 | - return $phpString; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * @param $code |
|
| 438 | - * |
|
| 439 | - * @return string |
|
| 440 | - */ |
|
| 441 | - protected function createCode($code) |
|
| 442 | - { |
|
| 443 | - if (func_num_args() > 1) { |
|
| 444 | - $arguments = func_get_args(); |
|
| 445 | - array_shift($arguments); // remove $code |
|
| 446 | - $statements = $this->apply('createStatements', $arguments); |
|
| 447 | - |
|
| 448 | - return $this->createPhpBlock($code, $statements); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - return $this->createPhpBlock($code); |
|
| 452 | - } |
|
| 14 | + /** |
|
| 15 | + * Constants and configuration in Compiler/CompilerConfig.php. |
|
| 16 | + */ |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var |
|
| 20 | + */ |
|
| 21 | + protected $xml; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var |
|
| 25 | + */ |
|
| 26 | + protected $parentIndents; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + protected $buffer = array(); |
|
| 32 | + /** |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + protected $options = array(); |
|
| 36 | + /** |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + protected $filters = array(); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var bool |
|
| 43 | + */ |
|
| 44 | + protected $phpSingleLine = false; |
|
| 45 | + /** |
|
| 46 | + * @var bool |
|
| 47 | + */ |
|
| 48 | + protected $allowMixinOverride = false; |
|
| 49 | + /** |
|
| 50 | + * @var bool |
|
| 51 | + */ |
|
| 52 | + protected $keepNullAttributes = false; |
|
| 53 | + /** |
|
| 54 | + * @var bool |
|
| 55 | + */ |
|
| 56 | + protected $filterAutoLoad = true; |
|
| 57 | + /** |
|
| 58 | + * @var bool |
|
| 59 | + */ |
|
| 60 | + protected $terse = true; |
|
| 61 | + /** |
|
| 62 | + * @var bool |
|
| 63 | + */ |
|
| 64 | + protected $restrictedScope = false; |
|
| 65 | + /** |
|
| 66 | + * @var array |
|
| 67 | + */ |
|
| 68 | + protected $customKeywords = array(); |
|
| 69 | + /** |
|
| 70 | + * @var Jade |
|
| 71 | + */ |
|
| 72 | + protected $jade = null; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @var string |
|
| 76 | + */ |
|
| 77 | + protected $quote; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @var string |
|
| 81 | + */ |
|
| 82 | + protected $filename; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param array/Jade $options |
|
| 86 | + * @param array $filters |
|
| 87 | + */ |
|
| 88 | + public function __construct($options = array(), array $filters = array(), $filename = null) |
|
| 89 | + { |
|
| 90 | + $this->options = $this->setOptions($options); |
|
| 91 | + $this->filters = $filters; |
|
| 92 | + $this->filename = $filename; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Get a jade engine reference or an options array and return needed options. |
|
| 97 | + * |
|
| 98 | + * @param array/Jade $options |
|
| 99 | + * |
|
| 100 | + * @return array |
|
| 101 | + */ |
|
| 102 | + protected function setOptions($options) |
|
| 103 | + { |
|
| 104 | + $optionTypes = array( |
|
| 105 | + 'prettyprint' => 'boolean', |
|
| 106 | + 'phpSingleLine' => 'boolean', |
|
| 107 | + 'allowMixinOverride' => 'boolean', |
|
| 108 | + 'keepNullAttributes' => 'boolean', |
|
| 109 | + 'filterAutoLoad' => 'boolean', |
|
| 110 | + 'restrictedScope' => 'boolean', |
|
| 111 | + 'indentSize' => 'integer', |
|
| 112 | + 'indentChar' => 'string', |
|
| 113 | + 'customKeywords' => 'array', |
|
| 114 | + ); |
|
| 115 | + |
|
| 116 | + if ($options instanceof Jade) { |
|
| 117 | + $this->jade = $options; |
|
| 118 | + $options = array(); |
|
| 119 | + |
|
| 120 | + foreach ($optionTypes as $option => $type) { |
|
| 121 | + $this->$option = $this->jade->getOption($option); |
|
| 122 | + $options[$option] = $this->$option; |
|
| 123 | + settype($this->$option, $type); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $this->quote = $this->jade->getOption('singleQuote') ? '\'' : '"'; |
|
| 127 | + |
|
| 128 | + return $options; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + foreach (array_intersect_key($optionTypes, $options) as $option => $type) { |
|
| 132 | + $this->$option = $options[$option]; |
|
| 133 | + settype($this->$option, $type); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $this->quote = isset($options['singleQuote']) && $options['singleQuote'] ? '\'' : '"'; |
|
| 137 | + |
|
| 138 | + return $options; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Get an option from the jade engine if set or from the options array else. |
|
| 143 | + * |
|
| 144 | + * @param string $option |
|
| 145 | + * |
|
| 146 | + * @throws \InvalidArgumentException |
|
| 147 | + * |
|
| 148 | + * @return mixed |
|
| 149 | + */ |
|
| 150 | + public function getOption($option) |
|
| 151 | + { |
|
| 152 | + if (is_null($this->jade)) { |
|
| 153 | + if (!isset($this->options[$option])) { |
|
| 154 | + throw new \InvalidArgumentException("$option is not a valid option name.", 28); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $this->options[$option]; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $this->jade->getOption($option); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Get a compiler with the same settings. |
|
| 165 | + * |
|
| 166 | + * @return Compiler |
|
| 167 | + */ |
|
| 168 | + public function subCompiler() |
|
| 169 | + { |
|
| 170 | + return new static($this->options, $this->filters); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * php closing tag depanding on the pretty print setting. |
|
| 175 | + * |
|
| 176 | + * @return string |
|
| 177 | + */ |
|
| 178 | + protected function closingTag() |
|
| 179 | + { |
|
| 180 | + return '?>' . ($this->prettyprint ? ' ' : ''); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @param $node |
|
| 185 | + * |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | + public function compile($node) |
|
| 189 | + { |
|
| 190 | + $this->visit($node); |
|
| 191 | + |
|
| 192 | + $code = ltrim(implode('', $this->buffer)); |
|
| 193 | + |
|
| 194 | + // Separate in several lines to get a useable line number in case of an error occurs |
|
| 195 | + if ($this->phpSingleLine) { |
|
| 196 | + $code = str_replace(array('<?php', '?>'), array("<?php\n", "\n" . $this->closingTag()), $code); |
|
| 197 | + } |
|
| 198 | + // Remove the $ wich are not needed |
|
| 199 | + return $code; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @param $method |
|
| 204 | + * @param $arguments |
|
| 205 | + * |
|
| 206 | + * @throws \BadMethodCallException If the 'apply' rely on non existing method |
|
| 207 | + * |
|
| 208 | + * @return mixed |
|
| 209 | + */ |
|
| 210 | + protected function apply($method, $arguments) |
|
| 211 | + { |
|
| 212 | + if (!method_exists($this, $method)) { |
|
| 213 | + throw new \BadMethodCallException(sprintf('Method %s do not exists', $method), 7); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + return call_user_func_array(array($this, $method), $arguments); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @param $line |
|
| 221 | + * @param null $indent |
|
| 222 | + */ |
|
| 223 | + protected function buffer($line, $indent = null) |
|
| 224 | + { |
|
| 225 | + if ($indent === true || ($indent === null && $this->prettyprint)) { |
|
| 226 | + $line = $this->indent() . $line . $this->newline(); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + $this->buffer[] = $line; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @param string $str |
|
| 234 | + * |
|
| 235 | + * @return bool|int |
|
| 236 | + */ |
|
| 237 | + protected function isConstant($str) |
|
| 238 | + { |
|
| 239 | + return preg_match('/^' . static::CONSTANT_VALUE . '$/', trim($str)); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @param $input |
|
| 244 | + * @param string $name |
|
| 245 | + * |
|
| 246 | + * @throws \ErrorException |
|
| 247 | + * |
|
| 248 | + * @return array |
|
| 249 | + */ |
|
| 250 | + public function handleCode($input, $name = '') |
|
| 251 | + { |
|
| 252 | + $handler = new CodeHandler($input, $name); |
|
| 253 | + |
|
| 254 | + return $handler->parse(); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @param $input |
|
| 259 | + * |
|
| 260 | + * @throws \ErrorException |
|
| 261 | + * |
|
| 262 | + * @return array |
|
| 263 | + */ |
|
| 264 | + public function handleString($input) |
|
| 265 | + { |
|
| 266 | + $result = array(); |
|
| 267 | + $resultsString = array(); |
|
| 268 | + |
|
| 269 | + $separators = preg_split( |
|
| 270 | + '/[+](?!\\()/', // concatenation operator - only js |
|
| 271 | + $input, |
|
| 272 | + -1, |
|
| 273 | + PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE |
|
| 274 | + ); |
|
| 275 | + |
|
| 276 | + foreach ($separators as $part) { |
|
| 277 | + // $sep[0] - the separator string due to PREG_SPLIT_OFFSET_CAPTURE flag |
|
| 278 | + // $sep[1] - the offset due to PREG_SPLIT_OFFSET_CAPTURE |
|
| 279 | + // @todo: = find original usage of this |
|
| 280 | + //$sep = substr( |
|
| 281 | + // $input, |
|
| 282 | + // strlen($part[0]) + $part[1] + 1, |
|
| 283 | + // isset($separators[$i+1]) ? $separators[$i+1][1] : strlen($input) |
|
| 284 | + //); |
|
| 285 | + |
|
| 286 | + // @todo: handleCode() in concat |
|
| 287 | + $part[0] = trim($part[0]); |
|
| 288 | + |
|
| 289 | + if (preg_match('/^("(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\')(.*)$/', $part[0], $match)) { |
|
| 290 | + $quote = substr($match[1], 0, 1); |
|
| 291 | + |
|
| 292 | + if (strlen(trim($match[2]))) { |
|
| 293 | + throw new \ErrorException('Unexpected value: ' . $match[2], 8); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + array_push($resultsString, $match[1]); |
|
| 297 | + |
|
| 298 | + continue; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + $code = $this->handleCode($part[0]); |
|
| 302 | + |
|
| 303 | + $result = array_merge($result, array_slice($code, 0, -1)); |
|
| 304 | + array_push($resultsString, array_pop($code)); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + array_push($result, implode(' . ', $resultsString)); |
|
| 308 | + |
|
| 309 | + return $result; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @param string $text |
|
| 314 | + * |
|
| 315 | + * @return mixed |
|
| 316 | + */ |
|
| 317 | + public function interpolate($text) |
|
| 318 | + { |
|
| 319 | + return preg_replace_callback('/(\\\\)?([#!]){(.*?)}/', array($this, 'interpolateFromCapture'), $text); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @param array $match |
|
| 324 | + * |
|
| 325 | + * @return string |
|
| 326 | + */ |
|
| 327 | + protected function interpolateFromCapture($match) |
|
| 328 | + { |
|
| 329 | + if ($match[1] === '') { |
|
| 330 | + return $this->escapeIfNeeded($match[2] === '!', $match[3]); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + return substr($match[0], 1); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * @throws \InvalidArgumentException |
|
| 338 | + * |
|
| 339 | + * @return array |
|
| 340 | + */ |
|
| 341 | + protected function createStatements() |
|
| 342 | + { |
|
| 343 | + if (func_num_args() === 0) { |
|
| 344 | + throw new \InvalidArgumentException('No Arguments provided', 9); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + $arguments = func_get_args(); |
|
| 348 | + $statements = array(); |
|
| 349 | + $variables = array(); |
|
| 350 | + |
|
| 351 | + foreach ($arguments as $arg) { |
|
| 352 | + $arg = static::convertVarPath($arg); |
|
| 353 | + |
|
| 354 | + // add dollar if missing |
|
| 355 | + if (preg_match('/^' . static::VARNAME . '(\s*,.+)?$/', $arg)) { |
|
| 356 | + $arg = static::addDollarIfNeeded($arg); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + // shortcut for constants |
|
| 360 | + if ($this->isConstant($arg)) { |
|
| 361 | + array_push($variables, $arg); |
|
| 362 | + continue; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + // if we have a php variable assume that the string is good php |
|
| 366 | + if (strpos('{[', substr($arg, 0, 1)) === false && preg_match('/&?\${1,2}' . static::VARNAME . '|[A-Za-z0-9_\\\\]+::/', $arg)) { |
|
| 367 | + array_push($variables, $arg); |
|
| 368 | + continue; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + $code = $this->handleArgumentValue($arg); |
|
| 372 | + |
|
| 373 | + $statements = array_merge($statements, array_slice($code, 0, -1)); |
|
| 374 | + array_push($variables, array_pop($code)); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + array_push($statements, $variables); |
|
| 378 | + |
|
| 379 | + return $statements; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + protected function handleArgumentValue($arg) |
|
| 383 | + { |
|
| 384 | + if (preg_match('/^"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'/', $arg)) { |
|
| 385 | + return $this->handleString(trim($arg)); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + try { |
|
| 389 | + return $this->handleCode($arg); |
|
| 390 | + } catch (\Exception $e) { |
|
| 391 | + // if a bug occur, try to remove comments |
|
| 392 | + try { |
|
| 393 | + return $this->handleCode(preg_replace('#/\*(.*)\*/#', '', $arg)); |
|
| 394 | + } catch (\Exception $e) { |
|
| 395 | + throw new ParserException('Pug.php did not understand ' . $arg, 10, $e); |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * @param $code |
|
| 402 | + * @param null $statements |
|
| 403 | + * |
|
| 404 | + * @return string |
|
| 405 | + */ |
|
| 406 | + protected function createPhpBlock($code, $statements = null) |
|
| 407 | + { |
|
| 408 | + if ($statements === null) { |
|
| 409 | + return '<?php ' . $code . ' ' . $this->closingTag(); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + $codeFormat = array_pop($statements); |
|
| 413 | + array_unshift($codeFormat, $code); |
|
| 414 | + |
|
| 415 | + if (count($statements) === 0) { |
|
| 416 | + $phpString = call_user_func_array('sprintf', $codeFormat); |
|
| 417 | + |
|
| 418 | + return '<?php ' . $phpString . ' ' . $this->closingTag(); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + $stmtString = ''; |
|
| 422 | + foreach ($statements as $stmt) { |
|
| 423 | + $stmtString .= $this->newline() . $this->indent() . $stmt . ';'; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + $stmtString .= $this->newline() . $this->indent(); |
|
| 427 | + $stmtString .= call_user_func_array('sprintf', $codeFormat); |
|
| 428 | + |
|
| 429 | + $phpString = '<?php '; |
|
| 430 | + $phpString .= $stmtString; |
|
| 431 | + $phpString .= $this->newline() . $this->indent() . ' ' . $this->closingTag(); |
|
| 432 | + |
|
| 433 | + return $phpString; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * @param $code |
|
| 438 | + * |
|
| 439 | + * @return string |
|
| 440 | + */ |
|
| 441 | + protected function createCode($code) |
|
| 442 | + { |
|
| 443 | + if (func_num_args() > 1) { |
|
| 444 | + $arguments = func_get_args(); |
|
| 445 | + array_shift($arguments); // remove $code |
|
| 446 | + $statements = $this->apply('createStatements', $arguments); |
|
| 447 | + |
|
| 448 | + return $this->createPhpBlock($code, $statements); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + return $this->createPhpBlock($code); |
|
| 452 | + } |
|
| 453 | 453 | } |
@@ -9,183 +9,183 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Lexer extends Scanner |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var int |
|
| 14 | - */ |
|
| 15 | - public $lineno = 1; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var bool |
|
| 19 | - */ |
|
| 20 | - public $pipeless; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - public $allowMixedIndent; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - public $lastTagIndent = -2; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - protected $customKeywords = array(); |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param $input |
|
| 39 | - */ |
|
| 40 | - public function __construct($input, array $options = array()) |
|
| 41 | - { |
|
| 42 | - $this->allowMixedIndent = isset($options['allowMixedIndent']) && $options['allowMixedIndent']; |
|
| 43 | - $this->customKeywords = isset($options['customKeywords']) ? $options['customKeywords'] : array(); |
|
| 44 | - $this->setInput($input); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Construct token with specified parameters. |
|
| 49 | - * |
|
| 50 | - * @param string $type token type |
|
| 51 | - * @param string $value token value |
|
| 52 | - * |
|
| 53 | - * @return object new token object |
|
| 54 | - */ |
|
| 55 | - public function token($type, $value = null) |
|
| 56 | - { |
|
| 57 | - return (object) array( |
|
| 58 | - 'type' => $type, |
|
| 59 | - 'line' => $this->lineno, |
|
| 60 | - 'value' => $value, |
|
| 61 | - ); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Consume input. |
|
| 66 | - * |
|
| 67 | - * @param string $bytes utf8 string of input to consume |
|
| 68 | - */ |
|
| 69 | - protected function consume($bytes) |
|
| 70 | - { |
|
| 71 | - $this->input = substr($this->input, strlen($bytes)); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Defer token. |
|
| 76 | - * |
|
| 77 | - * @param \stdClass $token token to defer |
|
| 78 | - */ |
|
| 79 | - public function defer(\stdClass $token) |
|
| 80 | - { |
|
| 81 | - $this->deferred[] = $token; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Lookahead token 'n'. |
|
| 86 | - * |
|
| 87 | - * @param int $number number of tokens to predict |
|
| 88 | - * |
|
| 89 | - * @return object predicted token |
|
| 90 | - */ |
|
| 91 | - public function lookahead($number = 1) |
|
| 92 | - { |
|
| 93 | - $fetch = $number - count($this->stash); |
|
| 94 | - |
|
| 95 | - while ($fetch-- > 0) { |
|
| 96 | - $this->stash[] = $this->next(); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return $this->stash[--$number]; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Return stashed token. |
|
| 104 | - * |
|
| 105 | - * @return object|bool token if has stashed, false otherways |
|
| 106 | - */ |
|
| 107 | - protected function getStashed() |
|
| 108 | - { |
|
| 109 | - return count($this->stash) ? array_shift($this->stash) : null; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Return deferred token. |
|
| 114 | - * |
|
| 115 | - * @return object|bool token if has deferred, false otherways |
|
| 116 | - */ |
|
| 117 | - protected function deferred() |
|
| 118 | - { |
|
| 119 | - return count($this->deferred) ? array_shift($this->deferred) : null; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Return next token or previously stashed one. |
|
| 124 | - * |
|
| 125 | - * @return object |
|
| 126 | - */ |
|
| 127 | - public function advance() |
|
| 128 | - { |
|
| 129 | - $token = $this->getStashed() |
|
| 130 | - or $token = $this->next(); |
|
| 131 | - |
|
| 132 | - return $token; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Return next token. |
|
| 137 | - * |
|
| 138 | - * @return object |
|
| 139 | - */ |
|
| 140 | - protected function next() |
|
| 141 | - { |
|
| 142 | - return $this->nextToken(); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return bool|mixed|null|object|void |
|
| 147 | - */ |
|
| 148 | - public function nextToken() |
|
| 149 | - { |
|
| 150 | - if ($token = $this->deferred()) { |
|
| 151 | - return $token; |
|
| 152 | - } |
|
| 153 | - foreach (array( |
|
| 154 | - 'Blank', |
|
| 155 | - 'EOS', |
|
| 156 | - 'PipelessText', |
|
| 157 | - 'Yield', |
|
| 158 | - 'Doctype', |
|
| 159 | - 'Interpolation', |
|
| 160 | - 'Case', |
|
| 161 | - 'When', |
|
| 162 | - 'Default', |
|
| 163 | - 'Extends', |
|
| 164 | - 'Append', |
|
| 165 | - 'Prepend', |
|
| 166 | - 'Block', |
|
| 167 | - 'Include', |
|
| 168 | - 'Mixin', |
|
| 169 | - 'Call', |
|
| 170 | - 'Conditional', |
|
| 171 | - 'Each', |
|
| 172 | - 'Assignment', |
|
| 173 | - 'CustomKeyword', |
|
| 174 | - 'Tag', |
|
| 175 | - 'Filter', |
|
| 176 | - 'Code', |
|
| 177 | - 'Id', |
|
| 178 | - 'ClassName', |
|
| 179 | - 'Attributes', |
|
| 180 | - 'Indent', |
|
| 181 | - 'Comment', |
|
| 182 | - 'Colon', |
|
| 183 | - 'AndAttributes', |
|
| 184 | - 'Text', |
|
| 185 | - ) as $tokenType) { |
|
| 186 | - if ($token = $this->{'scan' . $tokenType}()) { |
|
| 187 | - return $token; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - } |
|
| 12 | + /** |
|
| 13 | + * @var int |
|
| 14 | + */ |
|
| 15 | + public $lineno = 1; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var bool |
|
| 19 | + */ |
|
| 20 | + public $pipeless; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + public $allowMixedIndent; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + public $lastTagIndent = -2; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + protected $customKeywords = array(); |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param $input |
|
| 39 | + */ |
|
| 40 | + public function __construct($input, array $options = array()) |
|
| 41 | + { |
|
| 42 | + $this->allowMixedIndent = isset($options['allowMixedIndent']) && $options['allowMixedIndent']; |
|
| 43 | + $this->customKeywords = isset($options['customKeywords']) ? $options['customKeywords'] : array(); |
|
| 44 | + $this->setInput($input); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Construct token with specified parameters. |
|
| 49 | + * |
|
| 50 | + * @param string $type token type |
|
| 51 | + * @param string $value token value |
|
| 52 | + * |
|
| 53 | + * @return object new token object |
|
| 54 | + */ |
|
| 55 | + public function token($type, $value = null) |
|
| 56 | + { |
|
| 57 | + return (object) array( |
|
| 58 | + 'type' => $type, |
|
| 59 | + 'line' => $this->lineno, |
|
| 60 | + 'value' => $value, |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Consume input. |
|
| 66 | + * |
|
| 67 | + * @param string $bytes utf8 string of input to consume |
|
| 68 | + */ |
|
| 69 | + protected function consume($bytes) |
|
| 70 | + { |
|
| 71 | + $this->input = substr($this->input, strlen($bytes)); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Defer token. |
|
| 76 | + * |
|
| 77 | + * @param \stdClass $token token to defer |
|
| 78 | + */ |
|
| 79 | + public function defer(\stdClass $token) |
|
| 80 | + { |
|
| 81 | + $this->deferred[] = $token; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Lookahead token 'n'. |
|
| 86 | + * |
|
| 87 | + * @param int $number number of tokens to predict |
|
| 88 | + * |
|
| 89 | + * @return object predicted token |
|
| 90 | + */ |
|
| 91 | + public function lookahead($number = 1) |
|
| 92 | + { |
|
| 93 | + $fetch = $number - count($this->stash); |
|
| 94 | + |
|
| 95 | + while ($fetch-- > 0) { |
|
| 96 | + $this->stash[] = $this->next(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return $this->stash[--$number]; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Return stashed token. |
|
| 104 | + * |
|
| 105 | + * @return object|bool token if has stashed, false otherways |
|
| 106 | + */ |
|
| 107 | + protected function getStashed() |
|
| 108 | + { |
|
| 109 | + return count($this->stash) ? array_shift($this->stash) : null; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Return deferred token. |
|
| 114 | + * |
|
| 115 | + * @return object|bool token if has deferred, false otherways |
|
| 116 | + */ |
|
| 117 | + protected function deferred() |
|
| 118 | + { |
|
| 119 | + return count($this->deferred) ? array_shift($this->deferred) : null; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Return next token or previously stashed one. |
|
| 124 | + * |
|
| 125 | + * @return object |
|
| 126 | + */ |
|
| 127 | + public function advance() |
|
| 128 | + { |
|
| 129 | + $token = $this->getStashed() |
|
| 130 | + or $token = $this->next(); |
|
| 131 | + |
|
| 132 | + return $token; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Return next token. |
|
| 137 | + * |
|
| 138 | + * @return object |
|
| 139 | + */ |
|
| 140 | + protected function next() |
|
| 141 | + { |
|
| 142 | + return $this->nextToken(); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return bool|mixed|null|object|void |
|
| 147 | + */ |
|
| 148 | + public function nextToken() |
|
| 149 | + { |
|
| 150 | + if ($token = $this->deferred()) { |
|
| 151 | + return $token; |
|
| 152 | + } |
|
| 153 | + foreach (array( |
|
| 154 | + 'Blank', |
|
| 155 | + 'EOS', |
|
| 156 | + 'PipelessText', |
|
| 157 | + 'Yield', |
|
| 158 | + 'Doctype', |
|
| 159 | + 'Interpolation', |
|
| 160 | + 'Case', |
|
| 161 | + 'When', |
|
| 162 | + 'Default', |
|
| 163 | + 'Extends', |
|
| 164 | + 'Append', |
|
| 165 | + 'Prepend', |
|
| 166 | + 'Block', |
|
| 167 | + 'Include', |
|
| 168 | + 'Mixin', |
|
| 169 | + 'Call', |
|
| 170 | + 'Conditional', |
|
| 171 | + 'Each', |
|
| 172 | + 'Assignment', |
|
| 173 | + 'CustomKeyword', |
|
| 174 | + 'Tag', |
|
| 175 | + 'Filter', |
|
| 176 | + 'Code', |
|
| 177 | + 'Id', |
|
| 178 | + 'ClassName', |
|
| 179 | + 'Attributes', |
|
| 180 | + 'Indent', |
|
| 181 | + 'Comment', |
|
| 182 | + 'Colon', |
|
| 183 | + 'AndAttributes', |
|
| 184 | + 'Text', |
|
| 185 | + ) as $tokenType) { |
|
| 186 | + if ($token = $this->{'scan' . $tokenType}()) { |
|
| 187 | + return $token; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -4,48 +4,48 @@ |
||
| 4 | 4 | |
| 5 | 5 | class ExtensionsHelper |
| 6 | 6 | { |
| 7 | - protected $extensions; |
|
| 8 | - |
|
| 9 | - public function __construct($extensions) |
|
| 10 | - { |
|
| 11 | - $this->extensions = is_string($extensions) |
|
| 12 | - ? array($extensions) |
|
| 13 | - : array_unique($extensions); |
|
| 14 | - } |
|
| 15 | - |
|
| 16 | - public function getExtensions() |
|
| 17 | - { |
|
| 18 | - return $this->extensions; |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public function getFirst($defaultValue = '') |
|
| 22 | - { |
|
| 23 | - return isset($this->extensions[0]) |
|
| 24 | - ? $this->extensions[0] |
|
| 25 | - : $defaultValue; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public function hasValidTemplateExtension($path) |
|
| 29 | - { |
|
| 30 | - foreach ($this->getExtensions() as $extension) { |
|
| 31 | - if (substr($path, -strlen($extension)) === $extension) { |
|
| 32 | - return true; |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - public function findValidTemplatePath($path) |
|
| 40 | - { |
|
| 41 | - $extensions = $this->getExtensions(); |
|
| 42 | - foreach (array_slice(func_get_args(), 1) as $extension) { |
|
| 43 | - $extensions[] = $extension; |
|
| 44 | - } |
|
| 45 | - foreach ($extensions as $extension) { |
|
| 46 | - if (file_exists($path . $extension)) { |
|
| 47 | - return realpath($path . $extension); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - } |
|
| 7 | + protected $extensions; |
|
| 8 | + |
|
| 9 | + public function __construct($extensions) |
|
| 10 | + { |
|
| 11 | + $this->extensions = is_string($extensions) |
|
| 12 | + ? array($extensions) |
|
| 13 | + : array_unique($extensions); |
|
| 14 | + } |
|
| 15 | + |
|
| 16 | + public function getExtensions() |
|
| 17 | + { |
|
| 18 | + return $this->extensions; |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public function getFirst($defaultValue = '') |
|
| 22 | + { |
|
| 23 | + return isset($this->extensions[0]) |
|
| 24 | + ? $this->extensions[0] |
|
| 25 | + : $defaultValue; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function hasValidTemplateExtension($path) |
|
| 29 | + { |
|
| 30 | + foreach ($this->getExtensions() as $extension) { |
|
| 31 | + if (substr($path, -strlen($extension)) === $extension) { |
|
| 32 | + return true; |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + public function findValidTemplatePath($path) |
|
| 40 | + { |
|
| 41 | + $extensions = $this->getExtensions(); |
|
| 42 | + foreach (array_slice(func_get_args(), 1) as $extension) { |
|
| 43 | + $extensions[] = $extension; |
|
| 44 | + } |
|
| 45 | + foreach ($extensions as $extension) { |
|
| 46 | + if (file_exists($path . $extension)) { |
|
| 47 | + return realpath($path . $extension); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -7,757 +7,757 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Parser |
| 9 | 9 | { |
| 10 | - public static $includeNotFound = ".alert.alert-danger.\n\tPage not found."; |
|
| 11 | - |
|
| 12 | - protected $allowMixedIndent; |
|
| 13 | - protected $basedir; |
|
| 14 | - protected $extending; |
|
| 15 | - protected $extension; |
|
| 16 | - protected $filename; |
|
| 17 | - protected $input; |
|
| 18 | - protected $lexer; |
|
| 19 | - protected $notFound; |
|
| 20 | - protected $options = array(); |
|
| 21 | - protected $preRender; |
|
| 22 | - |
|
| 23 | - protected $blocks = array(); |
|
| 24 | - protected $mixins = array(); |
|
| 25 | - protected $contexts = array(); |
|
| 26 | - |
|
| 27 | - public function __construct($input, $filename = null, array $options = array()) |
|
| 28 | - { |
|
| 29 | - $defaultOptions = array( |
|
| 30 | - 'allowMixedIndent' => true, |
|
| 31 | - 'basedir' => null, |
|
| 32 | - 'customKeywords' => array(), |
|
| 33 | - 'extension' => array('.pug', '.jade'), |
|
| 34 | - 'notFound' => null, |
|
| 35 | - 'preRender' => null, |
|
| 36 | - ); |
|
| 37 | - foreach ($defaultOptions as $key => $default) { |
|
| 38 | - $this->$key = isset($options[$key]) ? $options[$key] : $default; |
|
| 39 | - $this->options[$key] = $this->$key; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $this->setInput($filename, $input); |
|
| 43 | - |
|
| 44 | - if ($this->input && $this->input[0] === "\xef" && $this->input[1] === "\xbb" && $this->input[2] === "\xbf") { |
|
| 45 | - $this->input = substr($this->input, 3); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $this->lexer = new Lexer($this->input, $this->options); |
|
| 49 | - array_push($this->contexts, $this); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - protected function getExtensions() |
|
| 53 | - { |
|
| 54 | - $extensions = new ExtensionsHelper($this->extension); |
|
| 55 | - |
|
| 56 | - return $extensions->getExtensions(); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - protected function hasValidTemplateExtension($path) |
|
| 60 | - { |
|
| 61 | - $extensions = new ExtensionsHelper($this->extension); |
|
| 62 | - |
|
| 63 | - return $extensions->hasValidTemplateExtension($path); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - protected function getTemplatePath($path) |
|
| 67 | - { |
|
| 68 | - $isAbsolutePath = substr($path, 0, 1) === '/'; |
|
| 69 | - if ($isAbsolutePath && !isset($this->options['basedir'])) { |
|
| 70 | - throw new \ErrorException("The 'basedir' option need to be set to use absolute path like $path", 29); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $path = ($isAbsolutePath |
|
| 74 | - ? rtrim($this->options['basedir'], '/\\') |
|
| 75 | - : dirname($this->filename) |
|
| 76 | - ) . DIRECTORY_SEPARATOR . $path; |
|
| 77 | - $extensions = new ExtensionsHelper($this->extension); |
|
| 78 | - |
|
| 79 | - return $extensions->findValidTemplatePath($path, ''); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - protected function getTemplateContents($path, $value = null) |
|
| 83 | - { |
|
| 84 | - if ($path !== null) { |
|
| 85 | - $contents = file_get_contents($path); |
|
| 86 | - if (is_callable($this->preRender)) { |
|
| 87 | - $contents = call_user_func($this->preRender, $contents); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $contents; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $notFound = isset($this->options['notFound']) |
|
| 94 | - ? $this->options['notFound'] |
|
| 95 | - : static::$includeNotFound; |
|
| 96 | - |
|
| 97 | - if ($notFound !== false) { |
|
| 98 | - return $notFound; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $value = $value ?: $path; |
|
| 102 | - throw new \InvalidArgumentException("The included file '$value' does not exists.", 22); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - protected function setInput($filename, $input) |
|
| 106 | - { |
|
| 107 | - if ($filename === null && file_exists($input)) { |
|
| 108 | - $filename = $input; |
|
| 109 | - $input = file_get_contents($input); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $this->input = preg_replace('`\r\n|\r`', "\n", $input); |
|
| 113 | - if (is_callable($this->preRender)) { |
|
| 114 | - $this->input = call_user_func($this->preRender, $this->input); |
|
| 115 | - } |
|
| 116 | - $this->filename = $filename; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - public function getFilename() |
|
| 120 | - { |
|
| 121 | - return $this->filename; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get a parser with the same settings. |
|
| 126 | - * |
|
| 127 | - * @return Parser |
|
| 128 | - */ |
|
| 129 | - public function subParser($input) |
|
| 130 | - { |
|
| 131 | - return new static($input, $this->filename, $this->options); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - public function context($parser = null) |
|
| 135 | - { |
|
| 136 | - if ($parser === null) { |
|
| 137 | - return array_pop($this->contexts); |
|
| 138 | - } |
|
| 139 | - array_push($this->contexts, $parser); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public function advance() |
|
| 143 | - { |
|
| 144 | - return $this->lexer->advance(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - public function skip($n) |
|
| 148 | - { |
|
| 149 | - while ($n--) { |
|
| 150 | - $this->advance(); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function peek() |
|
| 155 | - { |
|
| 156 | - return $this->lookahead(1); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - public function line() |
|
| 160 | - { |
|
| 161 | - return $this->lexer->lineno; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - public function lookahead($n = 1) |
|
| 165 | - { |
|
| 166 | - return $this->lexer->lookahead($n); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - public function parse() |
|
| 170 | - { |
|
| 171 | - $block = new Nodes\Block(); |
|
| 172 | - $block->line = $this->line(); |
|
| 173 | - |
|
| 174 | - while ($this->peekType() !== 'eos') { |
|
| 175 | - if ($this->peekType() === 'newline') { |
|
| 176 | - $this->advance(); |
|
| 177 | - continue; |
|
| 178 | - } |
|
| 179 | - $block->push($this->parseExpression()); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($parser = $this->extending) { |
|
| 183 | - $this->context($parser); |
|
| 184 | - // $parser->blocks = $this->blocks; |
|
| 185 | - try { |
|
| 186 | - $ast = $parser->parse(); |
|
| 187 | - } catch (\Exception $e) { |
|
| 188 | - throw new ParserException($parser->getFilename() . ' (' . $block->line . ') : ' . $e->getMessage(), 23, $e); |
|
| 189 | - } |
|
| 190 | - $this->context(); |
|
| 191 | - |
|
| 192 | - foreach ($this->mixins as $name => $v) { |
|
| 193 | - $ast->unshift($this->mixins[$name]); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $ast; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $block; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - protected function expect($type) |
|
| 203 | - { |
|
| 204 | - if ($this->peekType() === $type) { |
|
| 205 | - return $this->lexer->advance(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - $lineNumber = $this->line(); |
|
| 209 | - $lines = explode("\n", $this->input); |
|
| 210 | - $lineString = isset($lines[$lineNumber]) ? $lines[$lineNumber] : ''; |
|
| 211 | - throw new \ErrorException("\n" . sprintf('Expected %s, but got %s in %dth line : %s', $type, $this->peekType(), $lineNumber, $lineString) . "\n", 24); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - protected function accept($type) |
|
| 215 | - { |
|
| 216 | - if ($this->peekType() === $type) { |
|
| 217 | - return $this->advance(); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - protected function parseExpression() |
|
| 222 | - { |
|
| 223 | - $_types = array('tag', 'mixin', 'block', 'case', 'when', 'default', 'extends', 'include', 'doctype', 'filter', 'comment', 'text', 'each', 'customKeyword', 'code', 'call', 'interpolation'); |
|
| 224 | - |
|
| 225 | - if (in_array($this->peekType(), $_types)) { |
|
| 226 | - $_method = 'parse' . ucfirst($this->peekType()); |
|
| 227 | - |
|
| 228 | - return $this->$_method(); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - switch ($this->peekType()) { |
|
| 232 | - case 'yield': |
|
| 233 | - $this->advance(); |
|
| 234 | - $block = new Nodes\Block(); |
|
| 235 | - $block->yield = true; |
|
| 236 | - |
|
| 237 | - return $block; |
|
| 238 | - |
|
| 239 | - case 'id': |
|
| 240 | - case 'class': |
|
| 241 | - $token = $this->advance(); |
|
| 242 | - $this->lexer->defer($this->lexer->token('tag', 'div')); |
|
| 243 | - $this->lexer->defer($token); |
|
| 244 | - |
|
| 245 | - return $this->parseExpression(); |
|
| 246 | - |
|
| 247 | - default: |
|
| 248 | - throw new \ErrorException($this->filename . ' (' . $this->line() . ') : Unexpected token "' . $this->peekType() . '"', 25); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - protected function parseText() |
|
| 253 | - { |
|
| 254 | - $token = $this->expect('text'); |
|
| 255 | - if (preg_match('/^(.*?)#\[([^\]\n]+)\]/', $token->value)) { |
|
| 256 | - $block = new Nodes\Block(); |
|
| 257 | - $this->parseInlineTags($block, $token->value); |
|
| 258 | - |
|
| 259 | - return $block; |
|
| 260 | - } |
|
| 261 | - $node = new Nodes\Text($token->value); |
|
| 262 | - $node->line = $this->line(); |
|
| 263 | - |
|
| 264 | - return $node; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - protected function parseBlockExpansion() |
|
| 268 | - { |
|
| 269 | - if (':' === $this->peekType()) { |
|
| 270 | - $this->advance(); |
|
| 271 | - |
|
| 272 | - return new Nodes\Block($this->parseExpression()); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - return $this->block(); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - protected function parseCase() |
|
| 279 | - { |
|
| 280 | - $value = $this->expect('case')->value; |
|
| 281 | - $node = new Nodes\CaseNode($value); |
|
| 282 | - $node->line = $this->line(); |
|
| 283 | - $node->block = $this->block(); |
|
| 284 | - |
|
| 285 | - return $node; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - protected function parseWhen() |
|
| 289 | - { |
|
| 290 | - $value = $this->expect('when')->value; |
|
| 291 | - |
|
| 292 | - return new Nodes\When($value, $this->parseBlockExpansion()); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - protected function parseDefault() |
|
| 296 | - { |
|
| 297 | - $this->expect('default'); |
|
| 298 | - |
|
| 299 | - return new Nodes\When('default', $this->parseBlockExpansion()); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - protected function parseCode() |
|
| 303 | - { |
|
| 304 | - $token = $this->expect('code'); |
|
| 305 | - $buffer = isset($token->buffer) ? $token->buffer : false; |
|
| 306 | - $escape = isset($token->escape) ? $token->escape : true; |
|
| 307 | - $node = new Nodes\Code($token->value, $buffer, $escape); |
|
| 308 | - $node->line = $this->line(); |
|
| 309 | - |
|
| 310 | - $i = 1; |
|
| 311 | - while ($this->lookahead($i)->type === 'newline') { |
|
| 312 | - $i++; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - if ($this->lookahead($i)->type === 'indent') { |
|
| 316 | - $this->skip($i - 1); |
|
| 317 | - $node->block = $this->block(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - return $node; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - protected function parseComment() |
|
| 324 | - { |
|
| 325 | - $token = $this->expect('comment'); |
|
| 326 | - $node = new Nodes\Comment($token->value, $token->buffer); |
|
| 327 | - $node->line = $this->line(); |
|
| 328 | - |
|
| 329 | - return $node; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - protected function parseDoctype() |
|
| 333 | - { |
|
| 334 | - $token = $this->expect('doctype'); |
|
| 335 | - $node = new Nodes\Doctype($token->value); |
|
| 336 | - $node->line = $this->line(); |
|
| 337 | - |
|
| 338 | - return $node; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - protected function parseFilter() |
|
| 342 | - { |
|
| 343 | - $token = $this->expect('filter'); |
|
| 344 | - $attributes = $this->accept('attributes'); |
|
| 345 | - |
|
| 346 | - $this->lexer->pipeless = true; |
|
| 347 | - $block = $this->parseTextBlock(); |
|
| 348 | - $this->lexer->pipeless = false; |
|
| 349 | - |
|
| 350 | - $node = new Nodes\Filter($token->value, $block, $attributes); |
|
| 351 | - $node->line = $this->line(); |
|
| 352 | - |
|
| 353 | - return $node; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - protected function parseEach() |
|
| 357 | - { |
|
| 358 | - $token = $this->expect('each'); |
|
| 359 | - $node = new Nodes\Each($token->code, $token->value, $token->key); |
|
| 360 | - $node->line = $this->line(); |
|
| 361 | - $node->block = $this->block(); |
|
| 362 | - if ($this->peekType() === 'code' && $this->peek()->value === 'else') { |
|
| 363 | - $this->advance(); |
|
| 364 | - $node->alternative = $this->block(); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - return $node; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - protected function parseCustomKeyword() |
|
| 371 | - { |
|
| 372 | - $token = $this->expect('customKeyword'); |
|
| 373 | - $node = new Nodes\CustomKeyword($token->value, $token->args); |
|
| 374 | - $node->line = $this->line(); |
|
| 375 | - if ('indent' === $this->peekType()) { |
|
| 376 | - $node->block = $this->block(); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return $node; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - protected function parseExtends() |
|
| 383 | - { |
|
| 384 | - $extendValue = $this->expect('extends')->value; |
|
| 385 | - $path = $this->getTemplatePath($extendValue); |
|
| 386 | - |
|
| 387 | - $string = $this->getTemplateContents($path, $extendValue); |
|
| 388 | - $parser = new static($string, $path, $this->options); |
|
| 389 | - // need to be a reference, or be seted after the parse loop |
|
| 390 | - $parser->blocks = &$this->blocks; |
|
| 391 | - $parser->contexts = $this->contexts; |
|
| 392 | - $this->extending = $parser; |
|
| 393 | - |
|
| 394 | - return new Nodes\Literal(''); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - protected function parseBlock() |
|
| 398 | - { |
|
| 399 | - $block = $this->expect('block'); |
|
| 400 | - $mode = $block->mode; |
|
| 401 | - $name = trim($block->value); |
|
| 402 | - |
|
| 403 | - $block = 'indent' === $this->peekType() |
|
| 404 | - ? $this->block() |
|
| 405 | - : new Nodes\Block(empty($name) |
|
| 406 | - ? new Nodes\MixinBlock() |
|
| 407 | - : new Nodes\Literal('') |
|
| 408 | - ); |
|
| 409 | - |
|
| 410 | - if (isset($this->blocks[$name])) { |
|
| 411 | - $prev = &$this->blocks[$name]; |
|
| 412 | - |
|
| 413 | - switch ($prev->mode) { |
|
| 414 | - case 'append': |
|
| 415 | - $block->nodes = array_merge($block->nodes, $prev->nodes); |
|
| 416 | - $prev = $block; |
|
| 417 | - break; |
|
| 418 | - |
|
| 419 | - case 'prepend': |
|
| 420 | - $block->nodes = array_merge($prev->nodes, $block->nodes); |
|
| 421 | - $prev = $block; |
|
| 422 | - break; |
|
| 423 | - |
|
| 424 | - case 'replace': |
|
| 425 | - default: |
|
| 426 | - break; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - return $this->blocks[$name]; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - $block->mode = $mode; |
|
| 433 | - $this->blocks[$name] = $block; |
|
| 434 | - |
|
| 435 | - return $block; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - protected function parseInclude() |
|
| 439 | - { |
|
| 440 | - $token = $this->expect('include'); |
|
| 441 | - $includeValue = trim($token->value); |
|
| 442 | - $path = $this->getTemplatePath($includeValue); |
|
| 443 | - |
|
| 444 | - if ($path && !$this->hasValidTemplateExtension($path)) { |
|
| 445 | - return new Nodes\Text(file_get_contents($path)); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - $string = $this->getTemplateContents($path, $includeValue); |
|
| 449 | - |
|
| 450 | - $parser = new static($string, $path, $this->options); |
|
| 451 | - $parser->blocks = $this->blocks; |
|
| 452 | - $parser->mixins = $this->mixins; |
|
| 453 | - |
|
| 454 | - $this->context($parser); |
|
| 455 | - try { |
|
| 456 | - $ast = $parser->parse(); |
|
| 457 | - } catch (\Exception $e) { |
|
| 458 | - throw new \ErrorException($path . ' (' . $parser->lexer->lineno . ') : ' . $e->getMessage(), 27); |
|
| 459 | - } |
|
| 460 | - $this->context(); |
|
| 461 | - $ast->filename = $path; |
|
| 462 | - |
|
| 463 | - if ('indent' === $this->peekType() && method_exists($ast, 'includeBlock')) { |
|
| 464 | - $block = $ast->includeBlock(); |
|
| 465 | - if (is_object($block)) { |
|
| 466 | - $handler = count($block->nodes) === 1 && isset($block->nodes[0]->block) |
|
| 467 | - ? $block->nodes[0]->block |
|
| 468 | - : $block; |
|
| 469 | - $handler->push($this->block()); |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - return $ast; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - protected function parseCall() |
|
| 477 | - { |
|
| 478 | - $token = $this->expect('call'); |
|
| 479 | - $name = $token->value; |
|
| 480 | - |
|
| 481 | - $arguments = isset($token->arguments) |
|
| 482 | - ? $token->arguments |
|
| 483 | - : null; |
|
| 484 | - |
|
| 485 | - $mixin = new Nodes\Mixin($name, $arguments, new Nodes\Block(), true); |
|
| 486 | - |
|
| 487 | - $this->tag($mixin); |
|
| 488 | - |
|
| 489 | - if ($mixin->block->isEmpty()) { |
|
| 490 | - $mixin->block = null; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - return $mixin; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - protected function parseMixin() |
|
| 497 | - { |
|
| 498 | - $token = $this->expect('mixin'); |
|
| 499 | - $name = $token->value; |
|
| 500 | - $arguments = $token->arguments; |
|
| 501 | - |
|
| 502 | - // definition |
|
| 503 | - if ('indent' === $this->peekType()) { |
|
| 504 | - $mixin = new Nodes\Mixin($name, $arguments, $this->block(), false); |
|
| 505 | - $this->mixins[$name] = $mixin; |
|
| 506 | - |
|
| 507 | - return $mixin; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - // call |
|
| 511 | - return new Nodes\Mixin($name, $arguments, null, true); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - protected function parseTextBlock() |
|
| 515 | - { |
|
| 516 | - $block = new Nodes\Block(); |
|
| 517 | - $block->line = $this->line(); |
|
| 518 | - $spaces = $this->expect('indent')->value; |
|
| 519 | - |
|
| 520 | - if (!isset($this->_spaces)) { |
|
| 521 | - $this->_spaces = $spaces; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - $indent = str_repeat(' ', $spaces - $this->_spaces + 1); |
|
| 525 | - |
|
| 526 | - while ($this->peekType() != 'outdent') { |
|
| 527 | - switch ($this->peekType()) { |
|
| 528 | - case 'newline': |
|
| 529 | - $this->lexer->advance(); |
|
| 530 | - break; |
|
| 531 | - |
|
| 532 | - case 'indent': |
|
| 533 | - foreach ($this->parseTextBlock()->nodes as $n) { |
|
| 534 | - $block->push($n); |
|
| 535 | - } |
|
| 536 | - break; |
|
| 537 | - |
|
| 538 | - default: |
|
| 539 | - $this->parseInlineTags($block, $indent . $this->advance()->value); |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - if (isset($this->_spaces) && $spaces === $this->_spaces) { |
|
| 544 | - unset($this->_spaces); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - $this->expect('outdent'); |
|
| 548 | - |
|
| 549 | - return $block; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - protected function block() |
|
| 553 | - { |
|
| 554 | - $block = new Nodes\Block(); |
|
| 555 | - $block->line = $this->line(); |
|
| 556 | - $this->expect('indent'); |
|
| 557 | - |
|
| 558 | - while ($this->peekType() !== 'outdent') { |
|
| 559 | - if ($this->peekType() === 'newline') { |
|
| 560 | - $this->lexer->advance(); |
|
| 561 | - continue; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - $block->push($this->parseExpression()); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - $this->expect('outdent'); |
|
| 568 | - |
|
| 569 | - return $block; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - protected function parseInterpolation() |
|
| 573 | - { |
|
| 574 | - $token = $this->advance(); |
|
| 575 | - $tag = new Nodes\Tag($token->value); |
|
| 576 | - $tag->buffer = true; |
|
| 577 | - |
|
| 578 | - return $this->tag($tag); |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - protected function parseASTFilter() |
|
| 582 | - { |
|
| 583 | - $token = $this->expect('tag'); |
|
| 584 | - $attributes = $this->accept('attributes'); |
|
| 585 | - $this->expect(':'); |
|
| 586 | - $block = $this->block(); |
|
| 587 | - $node = new Nodes\Filter($token->value, $block, $attributes); |
|
| 588 | - $node->line = $this->line(); |
|
| 589 | - |
|
| 590 | - return $node; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - protected function parseTag() |
|
| 594 | - { |
|
| 595 | - $i = 2; |
|
| 596 | - |
|
| 597 | - if ('attributes' === $this->lookahead($i)->type) { |
|
| 598 | - $i++; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - if (':' === $this->lookahead($i)->type) { |
|
| 602 | - $i++; |
|
| 603 | - |
|
| 604 | - if ('indent' === $this->lookahead($i)->type) { |
|
| 605 | - return $this->parseASTFilter(); |
|
| 606 | - } |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $token = $this->advance(); |
|
| 610 | - $tag = new Nodes\Tag($token->value); |
|
| 611 | - |
|
| 612 | - $tag->selfClosing = isset($token->selfClosing) |
|
| 613 | - ? $token->selfClosing |
|
| 614 | - : false; |
|
| 615 | - |
|
| 616 | - return $this->tag($tag); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - public function parseInlineTags($block, $str) |
|
| 620 | - { |
|
| 621 | - while (preg_match('/^(.*?)#\[([^\]\n]+)\]/', $str, $matches)) { |
|
| 622 | - if (!empty($matches[1])) { |
|
| 623 | - $text = new Nodes\Text($matches[1]); |
|
| 624 | - $text->line = $this->line(); |
|
| 625 | - $block->push($text); |
|
| 626 | - } |
|
| 627 | - $parser = $this->subParser($matches[2]); |
|
| 628 | - $tag = $parser->parse(); |
|
| 629 | - $tag->line = $this->line(); |
|
| 630 | - $block->push($tag); |
|
| 631 | - $str = substr($str, strlen($matches[0])); |
|
| 632 | - } |
|
| 633 | - if (substr($str, 0, 1) === ' ') { |
|
| 634 | - $str = substr($str, 1); |
|
| 635 | - } |
|
| 636 | - $text = new Nodes\Text($str); |
|
| 637 | - $text->line = $this->line(); |
|
| 638 | - $block->push($text); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - protected function peekType() |
|
| 642 | - { |
|
| 643 | - return ($peek = $this->peek()) |
|
| 644 | - ? $peek->type |
|
| 645 | - : null; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - protected function tag($tag) |
|
| 649 | - { |
|
| 650 | - $tag->line = $this->line(); |
|
| 651 | - |
|
| 652 | - while (true) { |
|
| 653 | - switch ($type = $this->peekType()) { |
|
| 654 | - case 'id': |
|
| 655 | - $token = $this->advance(); |
|
| 656 | - $peek = $this->peek(); |
|
| 657 | - $escaped = isset($peek->escaped, $peek->escaped[$type]) && $peek->escaped[$type]; |
|
| 658 | - $value = $escaped || !isset($peek->attributes, $peek->attributes[$type]) |
|
| 659 | - ? "'" . $token->value . "'" |
|
| 660 | - : $peek->attributes[$type]; |
|
| 661 | - $tag->setAttribute($token->type, $value, $escaped); |
|
| 662 | - unset($peek->attributes[$type]); |
|
| 663 | - continue; |
|
| 664 | - |
|
| 665 | - case 'class': |
|
| 666 | - $token = $this->advance(); |
|
| 667 | - $tag->setAttribute($token->type, "'" . $token->value . "'"); |
|
| 668 | - continue; |
|
| 669 | - |
|
| 670 | - case 'attributes': |
|
| 671 | - $token = $this->advance(); |
|
| 672 | - $obj = $token->attributes; |
|
| 673 | - $escaped = $token->escaped; |
|
| 674 | - $nameList = array_keys($obj); |
|
| 675 | - |
|
| 676 | - if ($token->selfClosing) { |
|
| 677 | - $tag->selfClosing = true; |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - foreach ($nameList as $name) { |
|
| 681 | - $value = $obj[$name]; |
|
| 682 | - $normalizedValue = strtolower($value); |
|
| 683 | - if ($normalizedValue === 'true' || $normalizedValue === 'false') { |
|
| 684 | - $value = $normalizedValue === 'true'; |
|
| 685 | - } |
|
| 686 | - $tag->setAttribute($name, $value, $escaped[$name]); |
|
| 687 | - } |
|
| 688 | - continue; |
|
| 689 | - |
|
| 690 | - case '&attributes': |
|
| 691 | - $token = $this->advance(); |
|
| 692 | - $tag->setAttribute('&attributes', $token->value); |
|
| 693 | - continue; |
|
| 694 | - |
|
| 695 | - default: |
|
| 696 | - break 2; |
|
| 697 | - } |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - $dot = false; |
|
| 701 | - $tag->textOnly = false; |
|
| 702 | - if ('.' === $this->peek()->value) { |
|
| 703 | - $dot = $tag->textOnly = true; |
|
| 704 | - $this->advance(); |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - switch ($this->peekType()) { |
|
| 708 | - case 'text': |
|
| 709 | - $this->parseInlineTags($tag->block, $this->expect('text')->value); |
|
| 710 | - break; |
|
| 711 | - |
|
| 712 | - case 'code': |
|
| 713 | - $tag->code = $this->parseCode(); |
|
| 714 | - break; |
|
| 715 | - |
|
| 716 | - case ':': |
|
| 717 | - $this->advance(); |
|
| 718 | - $tag->block = new Nodes\Block(); |
|
| 719 | - $tag->block->push($this->parseExpression()); |
|
| 720 | - break; |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - while ('newline' === $this->peekType()) { |
|
| 724 | - $this->advance(); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - if ('script' === $tag->name) { |
|
| 728 | - $type = $tag->getAttribute('type'); |
|
| 729 | - |
|
| 730 | - if ($type !== null) { |
|
| 731 | - $type = preg_replace('/^[\'\"]|[\'\"]$/', '', $type); |
|
| 732 | - |
|
| 733 | - if (!$dot && 'text/javascript' != $type['value']) { |
|
| 734 | - $tag->textOnly = false; |
|
| 735 | - } |
|
| 736 | - } |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - if ('indent' === $this->peekType()) { |
|
| 740 | - if ($tag->textOnly) { |
|
| 741 | - $this->lexer->pipeless = true; |
|
| 742 | - $tag->block = $this->parseTextBlock(); |
|
| 743 | - $this->lexer->pipeless = false; |
|
| 744 | - |
|
| 745 | - return $tag; |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - $block = $this->block(); |
|
| 749 | - |
|
| 750 | - if ($tag->block && !$tag->block->isEmpty()) { |
|
| 751 | - foreach ($block->nodes as $n) { |
|
| 752 | - $tag->block->push($n); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - return $tag; |
|
| 756 | - } |
|
| 757 | - |
|
| 758 | - $tag->block = $block; |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - return $tag; |
|
| 762 | - } |
|
| 10 | + public static $includeNotFound = ".alert.alert-danger.\n\tPage not found."; |
|
| 11 | + |
|
| 12 | + protected $allowMixedIndent; |
|
| 13 | + protected $basedir; |
|
| 14 | + protected $extending; |
|
| 15 | + protected $extension; |
|
| 16 | + protected $filename; |
|
| 17 | + protected $input; |
|
| 18 | + protected $lexer; |
|
| 19 | + protected $notFound; |
|
| 20 | + protected $options = array(); |
|
| 21 | + protected $preRender; |
|
| 22 | + |
|
| 23 | + protected $blocks = array(); |
|
| 24 | + protected $mixins = array(); |
|
| 25 | + protected $contexts = array(); |
|
| 26 | + |
|
| 27 | + public function __construct($input, $filename = null, array $options = array()) |
|
| 28 | + { |
|
| 29 | + $defaultOptions = array( |
|
| 30 | + 'allowMixedIndent' => true, |
|
| 31 | + 'basedir' => null, |
|
| 32 | + 'customKeywords' => array(), |
|
| 33 | + 'extension' => array('.pug', '.jade'), |
|
| 34 | + 'notFound' => null, |
|
| 35 | + 'preRender' => null, |
|
| 36 | + ); |
|
| 37 | + foreach ($defaultOptions as $key => $default) { |
|
| 38 | + $this->$key = isset($options[$key]) ? $options[$key] : $default; |
|
| 39 | + $this->options[$key] = $this->$key; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $this->setInput($filename, $input); |
|
| 43 | + |
|
| 44 | + if ($this->input && $this->input[0] === "\xef" && $this->input[1] === "\xbb" && $this->input[2] === "\xbf") { |
|
| 45 | + $this->input = substr($this->input, 3); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $this->lexer = new Lexer($this->input, $this->options); |
|
| 49 | + array_push($this->contexts, $this); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + protected function getExtensions() |
|
| 53 | + { |
|
| 54 | + $extensions = new ExtensionsHelper($this->extension); |
|
| 55 | + |
|
| 56 | + return $extensions->getExtensions(); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + protected function hasValidTemplateExtension($path) |
|
| 60 | + { |
|
| 61 | + $extensions = new ExtensionsHelper($this->extension); |
|
| 62 | + |
|
| 63 | + return $extensions->hasValidTemplateExtension($path); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + protected function getTemplatePath($path) |
|
| 67 | + { |
|
| 68 | + $isAbsolutePath = substr($path, 0, 1) === '/'; |
|
| 69 | + if ($isAbsolutePath && !isset($this->options['basedir'])) { |
|
| 70 | + throw new \ErrorException("The 'basedir' option need to be set to use absolute path like $path", 29); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $path = ($isAbsolutePath |
|
| 74 | + ? rtrim($this->options['basedir'], '/\\') |
|
| 75 | + : dirname($this->filename) |
|
| 76 | + ) . DIRECTORY_SEPARATOR . $path; |
|
| 77 | + $extensions = new ExtensionsHelper($this->extension); |
|
| 78 | + |
|
| 79 | + return $extensions->findValidTemplatePath($path, ''); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + protected function getTemplateContents($path, $value = null) |
|
| 83 | + { |
|
| 84 | + if ($path !== null) { |
|
| 85 | + $contents = file_get_contents($path); |
|
| 86 | + if (is_callable($this->preRender)) { |
|
| 87 | + $contents = call_user_func($this->preRender, $contents); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $contents; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $notFound = isset($this->options['notFound']) |
|
| 94 | + ? $this->options['notFound'] |
|
| 95 | + : static::$includeNotFound; |
|
| 96 | + |
|
| 97 | + if ($notFound !== false) { |
|
| 98 | + return $notFound; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $value = $value ?: $path; |
|
| 102 | + throw new \InvalidArgumentException("The included file '$value' does not exists.", 22); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + protected function setInput($filename, $input) |
|
| 106 | + { |
|
| 107 | + if ($filename === null && file_exists($input)) { |
|
| 108 | + $filename = $input; |
|
| 109 | + $input = file_get_contents($input); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $this->input = preg_replace('`\r\n|\r`', "\n", $input); |
|
| 113 | + if (is_callable($this->preRender)) { |
|
| 114 | + $this->input = call_user_func($this->preRender, $this->input); |
|
| 115 | + } |
|
| 116 | + $this->filename = $filename; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + public function getFilename() |
|
| 120 | + { |
|
| 121 | + return $this->filename; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get a parser with the same settings. |
|
| 126 | + * |
|
| 127 | + * @return Parser |
|
| 128 | + */ |
|
| 129 | + public function subParser($input) |
|
| 130 | + { |
|
| 131 | + return new static($input, $this->filename, $this->options); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + public function context($parser = null) |
|
| 135 | + { |
|
| 136 | + if ($parser === null) { |
|
| 137 | + return array_pop($this->contexts); |
|
| 138 | + } |
|
| 139 | + array_push($this->contexts, $parser); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public function advance() |
|
| 143 | + { |
|
| 144 | + return $this->lexer->advance(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + public function skip($n) |
|
| 148 | + { |
|
| 149 | + while ($n--) { |
|
| 150 | + $this->advance(); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function peek() |
|
| 155 | + { |
|
| 156 | + return $this->lookahead(1); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function line() |
|
| 160 | + { |
|
| 161 | + return $this->lexer->lineno; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + public function lookahead($n = 1) |
|
| 165 | + { |
|
| 166 | + return $this->lexer->lookahead($n); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + public function parse() |
|
| 170 | + { |
|
| 171 | + $block = new Nodes\Block(); |
|
| 172 | + $block->line = $this->line(); |
|
| 173 | + |
|
| 174 | + while ($this->peekType() !== 'eos') { |
|
| 175 | + if ($this->peekType() === 'newline') { |
|
| 176 | + $this->advance(); |
|
| 177 | + continue; |
|
| 178 | + } |
|
| 179 | + $block->push($this->parseExpression()); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($parser = $this->extending) { |
|
| 183 | + $this->context($parser); |
|
| 184 | + // $parser->blocks = $this->blocks; |
|
| 185 | + try { |
|
| 186 | + $ast = $parser->parse(); |
|
| 187 | + } catch (\Exception $e) { |
|
| 188 | + throw new ParserException($parser->getFilename() . ' (' . $block->line . ') : ' . $e->getMessage(), 23, $e); |
|
| 189 | + } |
|
| 190 | + $this->context(); |
|
| 191 | + |
|
| 192 | + foreach ($this->mixins as $name => $v) { |
|
| 193 | + $ast->unshift($this->mixins[$name]); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $ast; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $block; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + protected function expect($type) |
|
| 203 | + { |
|
| 204 | + if ($this->peekType() === $type) { |
|
| 205 | + return $this->lexer->advance(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + $lineNumber = $this->line(); |
|
| 209 | + $lines = explode("\n", $this->input); |
|
| 210 | + $lineString = isset($lines[$lineNumber]) ? $lines[$lineNumber] : ''; |
|
| 211 | + throw new \ErrorException("\n" . sprintf('Expected %s, but got %s in %dth line : %s', $type, $this->peekType(), $lineNumber, $lineString) . "\n", 24); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + protected function accept($type) |
|
| 215 | + { |
|
| 216 | + if ($this->peekType() === $type) { |
|
| 217 | + return $this->advance(); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + protected function parseExpression() |
|
| 222 | + { |
|
| 223 | + $_types = array('tag', 'mixin', 'block', 'case', 'when', 'default', 'extends', 'include', 'doctype', 'filter', 'comment', 'text', 'each', 'customKeyword', 'code', 'call', 'interpolation'); |
|
| 224 | + |
|
| 225 | + if (in_array($this->peekType(), $_types)) { |
|
| 226 | + $_method = 'parse' . ucfirst($this->peekType()); |
|
| 227 | + |
|
| 228 | + return $this->$_method(); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + switch ($this->peekType()) { |
|
| 232 | + case 'yield': |
|
| 233 | + $this->advance(); |
|
| 234 | + $block = new Nodes\Block(); |
|
| 235 | + $block->yield = true; |
|
| 236 | + |
|
| 237 | + return $block; |
|
| 238 | + |
|
| 239 | + case 'id': |
|
| 240 | + case 'class': |
|
| 241 | + $token = $this->advance(); |
|
| 242 | + $this->lexer->defer($this->lexer->token('tag', 'div')); |
|
| 243 | + $this->lexer->defer($token); |
|
| 244 | + |
|
| 245 | + return $this->parseExpression(); |
|
| 246 | + |
|
| 247 | + default: |
|
| 248 | + throw new \ErrorException($this->filename . ' (' . $this->line() . ') : Unexpected token "' . $this->peekType() . '"', 25); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + protected function parseText() |
|
| 253 | + { |
|
| 254 | + $token = $this->expect('text'); |
|
| 255 | + if (preg_match('/^(.*?)#\[([^\]\n]+)\]/', $token->value)) { |
|
| 256 | + $block = new Nodes\Block(); |
|
| 257 | + $this->parseInlineTags($block, $token->value); |
|
| 258 | + |
|
| 259 | + return $block; |
|
| 260 | + } |
|
| 261 | + $node = new Nodes\Text($token->value); |
|
| 262 | + $node->line = $this->line(); |
|
| 263 | + |
|
| 264 | + return $node; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + protected function parseBlockExpansion() |
|
| 268 | + { |
|
| 269 | + if (':' === $this->peekType()) { |
|
| 270 | + $this->advance(); |
|
| 271 | + |
|
| 272 | + return new Nodes\Block($this->parseExpression()); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + return $this->block(); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + protected function parseCase() |
|
| 279 | + { |
|
| 280 | + $value = $this->expect('case')->value; |
|
| 281 | + $node = new Nodes\CaseNode($value); |
|
| 282 | + $node->line = $this->line(); |
|
| 283 | + $node->block = $this->block(); |
|
| 284 | + |
|
| 285 | + return $node; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + protected function parseWhen() |
|
| 289 | + { |
|
| 290 | + $value = $this->expect('when')->value; |
|
| 291 | + |
|
| 292 | + return new Nodes\When($value, $this->parseBlockExpansion()); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + protected function parseDefault() |
|
| 296 | + { |
|
| 297 | + $this->expect('default'); |
|
| 298 | + |
|
| 299 | + return new Nodes\When('default', $this->parseBlockExpansion()); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + protected function parseCode() |
|
| 303 | + { |
|
| 304 | + $token = $this->expect('code'); |
|
| 305 | + $buffer = isset($token->buffer) ? $token->buffer : false; |
|
| 306 | + $escape = isset($token->escape) ? $token->escape : true; |
|
| 307 | + $node = new Nodes\Code($token->value, $buffer, $escape); |
|
| 308 | + $node->line = $this->line(); |
|
| 309 | + |
|
| 310 | + $i = 1; |
|
| 311 | + while ($this->lookahead($i)->type === 'newline') { |
|
| 312 | + $i++; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + if ($this->lookahead($i)->type === 'indent') { |
|
| 316 | + $this->skip($i - 1); |
|
| 317 | + $node->block = $this->block(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + return $node; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + protected function parseComment() |
|
| 324 | + { |
|
| 325 | + $token = $this->expect('comment'); |
|
| 326 | + $node = new Nodes\Comment($token->value, $token->buffer); |
|
| 327 | + $node->line = $this->line(); |
|
| 328 | + |
|
| 329 | + return $node; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + protected function parseDoctype() |
|
| 333 | + { |
|
| 334 | + $token = $this->expect('doctype'); |
|
| 335 | + $node = new Nodes\Doctype($token->value); |
|
| 336 | + $node->line = $this->line(); |
|
| 337 | + |
|
| 338 | + return $node; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + protected function parseFilter() |
|
| 342 | + { |
|
| 343 | + $token = $this->expect('filter'); |
|
| 344 | + $attributes = $this->accept('attributes'); |
|
| 345 | + |
|
| 346 | + $this->lexer->pipeless = true; |
|
| 347 | + $block = $this->parseTextBlock(); |
|
| 348 | + $this->lexer->pipeless = false; |
|
| 349 | + |
|
| 350 | + $node = new Nodes\Filter($token->value, $block, $attributes); |
|
| 351 | + $node->line = $this->line(); |
|
| 352 | + |
|
| 353 | + return $node; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + protected function parseEach() |
|
| 357 | + { |
|
| 358 | + $token = $this->expect('each'); |
|
| 359 | + $node = new Nodes\Each($token->code, $token->value, $token->key); |
|
| 360 | + $node->line = $this->line(); |
|
| 361 | + $node->block = $this->block(); |
|
| 362 | + if ($this->peekType() === 'code' && $this->peek()->value === 'else') { |
|
| 363 | + $this->advance(); |
|
| 364 | + $node->alternative = $this->block(); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + return $node; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + protected function parseCustomKeyword() |
|
| 371 | + { |
|
| 372 | + $token = $this->expect('customKeyword'); |
|
| 373 | + $node = new Nodes\CustomKeyword($token->value, $token->args); |
|
| 374 | + $node->line = $this->line(); |
|
| 375 | + if ('indent' === $this->peekType()) { |
|
| 376 | + $node->block = $this->block(); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return $node; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + protected function parseExtends() |
|
| 383 | + { |
|
| 384 | + $extendValue = $this->expect('extends')->value; |
|
| 385 | + $path = $this->getTemplatePath($extendValue); |
|
| 386 | + |
|
| 387 | + $string = $this->getTemplateContents($path, $extendValue); |
|
| 388 | + $parser = new static($string, $path, $this->options); |
|
| 389 | + // need to be a reference, or be seted after the parse loop |
|
| 390 | + $parser->blocks = &$this->blocks; |
|
| 391 | + $parser->contexts = $this->contexts; |
|
| 392 | + $this->extending = $parser; |
|
| 393 | + |
|
| 394 | + return new Nodes\Literal(''); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + protected function parseBlock() |
|
| 398 | + { |
|
| 399 | + $block = $this->expect('block'); |
|
| 400 | + $mode = $block->mode; |
|
| 401 | + $name = trim($block->value); |
|
| 402 | + |
|
| 403 | + $block = 'indent' === $this->peekType() |
|
| 404 | + ? $this->block() |
|
| 405 | + : new Nodes\Block(empty($name) |
|
| 406 | + ? new Nodes\MixinBlock() |
|
| 407 | + : new Nodes\Literal('') |
|
| 408 | + ); |
|
| 409 | + |
|
| 410 | + if (isset($this->blocks[$name])) { |
|
| 411 | + $prev = &$this->blocks[$name]; |
|
| 412 | + |
|
| 413 | + switch ($prev->mode) { |
|
| 414 | + case 'append': |
|
| 415 | + $block->nodes = array_merge($block->nodes, $prev->nodes); |
|
| 416 | + $prev = $block; |
|
| 417 | + break; |
|
| 418 | + |
|
| 419 | + case 'prepend': |
|
| 420 | + $block->nodes = array_merge($prev->nodes, $block->nodes); |
|
| 421 | + $prev = $block; |
|
| 422 | + break; |
|
| 423 | + |
|
| 424 | + case 'replace': |
|
| 425 | + default: |
|
| 426 | + break; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + return $this->blocks[$name]; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + $block->mode = $mode; |
|
| 433 | + $this->blocks[$name] = $block; |
|
| 434 | + |
|
| 435 | + return $block; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + protected function parseInclude() |
|
| 439 | + { |
|
| 440 | + $token = $this->expect('include'); |
|
| 441 | + $includeValue = trim($token->value); |
|
| 442 | + $path = $this->getTemplatePath($includeValue); |
|
| 443 | + |
|
| 444 | + if ($path && !$this->hasValidTemplateExtension($path)) { |
|
| 445 | + return new Nodes\Text(file_get_contents($path)); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + $string = $this->getTemplateContents($path, $includeValue); |
|
| 449 | + |
|
| 450 | + $parser = new static($string, $path, $this->options); |
|
| 451 | + $parser->blocks = $this->blocks; |
|
| 452 | + $parser->mixins = $this->mixins; |
|
| 453 | + |
|
| 454 | + $this->context($parser); |
|
| 455 | + try { |
|
| 456 | + $ast = $parser->parse(); |
|
| 457 | + } catch (\Exception $e) { |
|
| 458 | + throw new \ErrorException($path . ' (' . $parser->lexer->lineno . ') : ' . $e->getMessage(), 27); |
|
| 459 | + } |
|
| 460 | + $this->context(); |
|
| 461 | + $ast->filename = $path; |
|
| 462 | + |
|
| 463 | + if ('indent' === $this->peekType() && method_exists($ast, 'includeBlock')) { |
|
| 464 | + $block = $ast->includeBlock(); |
|
| 465 | + if (is_object($block)) { |
|
| 466 | + $handler = count($block->nodes) === 1 && isset($block->nodes[0]->block) |
|
| 467 | + ? $block->nodes[0]->block |
|
| 468 | + : $block; |
|
| 469 | + $handler->push($this->block()); |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + return $ast; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + protected function parseCall() |
|
| 477 | + { |
|
| 478 | + $token = $this->expect('call'); |
|
| 479 | + $name = $token->value; |
|
| 480 | + |
|
| 481 | + $arguments = isset($token->arguments) |
|
| 482 | + ? $token->arguments |
|
| 483 | + : null; |
|
| 484 | + |
|
| 485 | + $mixin = new Nodes\Mixin($name, $arguments, new Nodes\Block(), true); |
|
| 486 | + |
|
| 487 | + $this->tag($mixin); |
|
| 488 | + |
|
| 489 | + if ($mixin->block->isEmpty()) { |
|
| 490 | + $mixin->block = null; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + return $mixin; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + protected function parseMixin() |
|
| 497 | + { |
|
| 498 | + $token = $this->expect('mixin'); |
|
| 499 | + $name = $token->value; |
|
| 500 | + $arguments = $token->arguments; |
|
| 501 | + |
|
| 502 | + // definition |
|
| 503 | + if ('indent' === $this->peekType()) { |
|
| 504 | + $mixin = new Nodes\Mixin($name, $arguments, $this->block(), false); |
|
| 505 | + $this->mixins[$name] = $mixin; |
|
| 506 | + |
|
| 507 | + return $mixin; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + // call |
|
| 511 | + return new Nodes\Mixin($name, $arguments, null, true); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + protected function parseTextBlock() |
|
| 515 | + { |
|
| 516 | + $block = new Nodes\Block(); |
|
| 517 | + $block->line = $this->line(); |
|
| 518 | + $spaces = $this->expect('indent')->value; |
|
| 519 | + |
|
| 520 | + if (!isset($this->_spaces)) { |
|
| 521 | + $this->_spaces = $spaces; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + $indent = str_repeat(' ', $spaces - $this->_spaces + 1); |
|
| 525 | + |
|
| 526 | + while ($this->peekType() != 'outdent') { |
|
| 527 | + switch ($this->peekType()) { |
|
| 528 | + case 'newline': |
|
| 529 | + $this->lexer->advance(); |
|
| 530 | + break; |
|
| 531 | + |
|
| 532 | + case 'indent': |
|
| 533 | + foreach ($this->parseTextBlock()->nodes as $n) { |
|
| 534 | + $block->push($n); |
|
| 535 | + } |
|
| 536 | + break; |
|
| 537 | + |
|
| 538 | + default: |
|
| 539 | + $this->parseInlineTags($block, $indent . $this->advance()->value); |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + if (isset($this->_spaces) && $spaces === $this->_spaces) { |
|
| 544 | + unset($this->_spaces); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + $this->expect('outdent'); |
|
| 548 | + |
|
| 549 | + return $block; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + protected function block() |
|
| 553 | + { |
|
| 554 | + $block = new Nodes\Block(); |
|
| 555 | + $block->line = $this->line(); |
|
| 556 | + $this->expect('indent'); |
|
| 557 | + |
|
| 558 | + while ($this->peekType() !== 'outdent') { |
|
| 559 | + if ($this->peekType() === 'newline') { |
|
| 560 | + $this->lexer->advance(); |
|
| 561 | + continue; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + $block->push($this->parseExpression()); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + $this->expect('outdent'); |
|
| 568 | + |
|
| 569 | + return $block; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + protected function parseInterpolation() |
|
| 573 | + { |
|
| 574 | + $token = $this->advance(); |
|
| 575 | + $tag = new Nodes\Tag($token->value); |
|
| 576 | + $tag->buffer = true; |
|
| 577 | + |
|
| 578 | + return $this->tag($tag); |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + protected function parseASTFilter() |
|
| 582 | + { |
|
| 583 | + $token = $this->expect('tag'); |
|
| 584 | + $attributes = $this->accept('attributes'); |
|
| 585 | + $this->expect(':'); |
|
| 586 | + $block = $this->block(); |
|
| 587 | + $node = new Nodes\Filter($token->value, $block, $attributes); |
|
| 588 | + $node->line = $this->line(); |
|
| 589 | + |
|
| 590 | + return $node; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + protected function parseTag() |
|
| 594 | + { |
|
| 595 | + $i = 2; |
|
| 596 | + |
|
| 597 | + if ('attributes' === $this->lookahead($i)->type) { |
|
| 598 | + $i++; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + if (':' === $this->lookahead($i)->type) { |
|
| 602 | + $i++; |
|
| 603 | + |
|
| 604 | + if ('indent' === $this->lookahead($i)->type) { |
|
| 605 | + return $this->parseASTFilter(); |
|
| 606 | + } |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $token = $this->advance(); |
|
| 610 | + $tag = new Nodes\Tag($token->value); |
|
| 611 | + |
|
| 612 | + $tag->selfClosing = isset($token->selfClosing) |
|
| 613 | + ? $token->selfClosing |
|
| 614 | + : false; |
|
| 615 | + |
|
| 616 | + return $this->tag($tag); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + public function parseInlineTags($block, $str) |
|
| 620 | + { |
|
| 621 | + while (preg_match('/^(.*?)#\[([^\]\n]+)\]/', $str, $matches)) { |
|
| 622 | + if (!empty($matches[1])) { |
|
| 623 | + $text = new Nodes\Text($matches[1]); |
|
| 624 | + $text->line = $this->line(); |
|
| 625 | + $block->push($text); |
|
| 626 | + } |
|
| 627 | + $parser = $this->subParser($matches[2]); |
|
| 628 | + $tag = $parser->parse(); |
|
| 629 | + $tag->line = $this->line(); |
|
| 630 | + $block->push($tag); |
|
| 631 | + $str = substr($str, strlen($matches[0])); |
|
| 632 | + } |
|
| 633 | + if (substr($str, 0, 1) === ' ') { |
|
| 634 | + $str = substr($str, 1); |
|
| 635 | + } |
|
| 636 | + $text = new Nodes\Text($str); |
|
| 637 | + $text->line = $this->line(); |
|
| 638 | + $block->push($text); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + protected function peekType() |
|
| 642 | + { |
|
| 643 | + return ($peek = $this->peek()) |
|
| 644 | + ? $peek->type |
|
| 645 | + : null; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + protected function tag($tag) |
|
| 649 | + { |
|
| 650 | + $tag->line = $this->line(); |
|
| 651 | + |
|
| 652 | + while (true) { |
|
| 653 | + switch ($type = $this->peekType()) { |
|
| 654 | + case 'id': |
|
| 655 | + $token = $this->advance(); |
|
| 656 | + $peek = $this->peek(); |
|
| 657 | + $escaped = isset($peek->escaped, $peek->escaped[$type]) && $peek->escaped[$type]; |
|
| 658 | + $value = $escaped || !isset($peek->attributes, $peek->attributes[$type]) |
|
| 659 | + ? "'" . $token->value . "'" |
|
| 660 | + : $peek->attributes[$type]; |
|
| 661 | + $tag->setAttribute($token->type, $value, $escaped); |
|
| 662 | + unset($peek->attributes[$type]); |
|
| 663 | + continue; |
|
| 664 | + |
|
| 665 | + case 'class': |
|
| 666 | + $token = $this->advance(); |
|
| 667 | + $tag->setAttribute($token->type, "'" . $token->value . "'"); |
|
| 668 | + continue; |
|
| 669 | + |
|
| 670 | + case 'attributes': |
|
| 671 | + $token = $this->advance(); |
|
| 672 | + $obj = $token->attributes; |
|
| 673 | + $escaped = $token->escaped; |
|
| 674 | + $nameList = array_keys($obj); |
|
| 675 | + |
|
| 676 | + if ($token->selfClosing) { |
|
| 677 | + $tag->selfClosing = true; |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + foreach ($nameList as $name) { |
|
| 681 | + $value = $obj[$name]; |
|
| 682 | + $normalizedValue = strtolower($value); |
|
| 683 | + if ($normalizedValue === 'true' || $normalizedValue === 'false') { |
|
| 684 | + $value = $normalizedValue === 'true'; |
|
| 685 | + } |
|
| 686 | + $tag->setAttribute($name, $value, $escaped[$name]); |
|
| 687 | + } |
|
| 688 | + continue; |
|
| 689 | + |
|
| 690 | + case '&attributes': |
|
| 691 | + $token = $this->advance(); |
|
| 692 | + $tag->setAttribute('&attributes', $token->value); |
|
| 693 | + continue; |
|
| 694 | + |
|
| 695 | + default: |
|
| 696 | + break 2; |
|
| 697 | + } |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + $dot = false; |
|
| 701 | + $tag->textOnly = false; |
|
| 702 | + if ('.' === $this->peek()->value) { |
|
| 703 | + $dot = $tag->textOnly = true; |
|
| 704 | + $this->advance(); |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + switch ($this->peekType()) { |
|
| 708 | + case 'text': |
|
| 709 | + $this->parseInlineTags($tag->block, $this->expect('text')->value); |
|
| 710 | + break; |
|
| 711 | + |
|
| 712 | + case 'code': |
|
| 713 | + $tag->code = $this->parseCode(); |
|
| 714 | + break; |
|
| 715 | + |
|
| 716 | + case ':': |
|
| 717 | + $this->advance(); |
|
| 718 | + $tag->block = new Nodes\Block(); |
|
| 719 | + $tag->block->push($this->parseExpression()); |
|
| 720 | + break; |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + while ('newline' === $this->peekType()) { |
|
| 724 | + $this->advance(); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + if ('script' === $tag->name) { |
|
| 728 | + $type = $tag->getAttribute('type'); |
|
| 729 | + |
|
| 730 | + if ($type !== null) { |
|
| 731 | + $type = preg_replace('/^[\'\"]|[\'\"]$/', '', $type); |
|
| 732 | + |
|
| 733 | + if (!$dot && 'text/javascript' != $type['value']) { |
|
| 734 | + $tag->textOnly = false; |
|
| 735 | + } |
|
| 736 | + } |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + if ('indent' === $this->peekType()) { |
|
| 740 | + if ($tag->textOnly) { |
|
| 741 | + $this->lexer->pipeless = true; |
|
| 742 | + $tag->block = $this->parseTextBlock(); |
|
| 743 | + $this->lexer->pipeless = false; |
|
| 744 | + |
|
| 745 | + return $tag; |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + $block = $this->block(); |
|
| 749 | + |
|
| 750 | + if ($tag->block && !$tag->block->isEmpty()) { |
|
| 751 | + foreach ($block->nodes as $n) { |
|
| 752 | + $tag->block->push($n); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + return $tag; |
|
| 756 | + } |
|
| 757 | + |
|
| 758 | + $tag->block = $block; |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + return $tag; |
|
| 762 | + } |
|
| 763 | 763 | } |
@@ -7,77 +7,77 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | abstract class Keywords extends Cache |
| 9 | 9 | { |
| 10 | - protected function hasKeyword($keyword) |
|
| 11 | - { |
|
| 12 | - return $this->hasValidCustomKeywordsOption() && isset($this->options['customKeywords'][$keyword]); |
|
| 13 | - } |
|
| 10 | + protected function hasKeyword($keyword) |
|
| 11 | + { |
|
| 12 | + return $this->hasValidCustomKeywordsOption() && isset($this->options['customKeywords'][$keyword]); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - protected function hasValidCustomKeywordsOption() |
|
| 16 | - { |
|
| 17 | - return isset($this->options['customKeywords']) && ( |
|
| 18 | - is_array($this->options['customKeywords']) || |
|
| 19 | - $this->options['customKeywords'] instanceof \ArrayAccess |
|
| 20 | - ); |
|
| 21 | - } |
|
| 15 | + protected function hasValidCustomKeywordsOption() |
|
| 16 | + { |
|
| 17 | + return isset($this->options['customKeywords']) && ( |
|
| 18 | + is_array($this->options['customKeywords']) || |
|
| 19 | + $this->options['customKeywords'] instanceof \ArrayAccess |
|
| 20 | + ); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Set custom keyword. |
|
| 25 | - * |
|
| 26 | - * @param string $keyword the keyword to be found. |
|
| 27 | - * @param callable $action action to be executed when the keyword is found. |
|
| 28 | - */ |
|
| 29 | - public function setKeyword($keyword, $action) |
|
| 30 | - { |
|
| 31 | - if (!is_callable($action)) { |
|
| 32 | - throw new \InvalidArgumentException("Please add a callable action for your keyword $keyword", 30); |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * Set custom keyword. |
|
| 25 | + * |
|
| 26 | + * @param string $keyword the keyword to be found. |
|
| 27 | + * @param callable $action action to be executed when the keyword is found. |
|
| 28 | + */ |
|
| 29 | + public function setKeyword($keyword, $action) |
|
| 30 | + { |
|
| 31 | + if (!is_callable($action)) { |
|
| 32 | + throw new \InvalidArgumentException("Please add a callable action for your keyword $keyword", 30); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - if (!$this->hasValidCustomKeywordsOption()) { |
|
| 36 | - $this->options['customKeywords'] = array(); |
|
| 37 | - } |
|
| 35 | + if (!$this->hasValidCustomKeywordsOption()) { |
|
| 36 | + $this->options['customKeywords'] = array(); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - $this->options['customKeywords'][$keyword] = $action; |
|
| 40 | - } |
|
| 39 | + $this->options['customKeywords'][$keyword] = $action; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Add custom keyword. |
|
| 44 | - * |
|
| 45 | - * @param string $keyword the keyword to be found. |
|
| 46 | - * @param callable $action action to be executed when the keyword is found. |
|
| 47 | - */ |
|
| 48 | - public function addKeyword($keyword, $action) |
|
| 49 | - { |
|
| 50 | - if ($this->hasKeyword($keyword)) { |
|
| 51 | - throw new \InvalidArgumentException("The keyword $keyword is already set.", 31); |
|
| 52 | - } |
|
| 42 | + /** |
|
| 43 | + * Add custom keyword. |
|
| 44 | + * |
|
| 45 | + * @param string $keyword the keyword to be found. |
|
| 46 | + * @param callable $action action to be executed when the keyword is found. |
|
| 47 | + */ |
|
| 48 | + public function addKeyword($keyword, $action) |
|
| 49 | + { |
|
| 50 | + if ($this->hasKeyword($keyword)) { |
|
| 51 | + throw new \InvalidArgumentException("The keyword $keyword is already set.", 31); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - $this->setKeyword($keyword, $action); |
|
| 55 | - } |
|
| 54 | + $this->setKeyword($keyword, $action); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Replace custom keyword. |
|
| 59 | - * |
|
| 60 | - * @param string $keyword the keyword to be found. |
|
| 61 | - * @param callable $action action to be executed when the keyword is found. |
|
| 62 | - */ |
|
| 63 | - public function replaceKeyword($keyword, $action) |
|
| 64 | - { |
|
| 65 | - if (!$this->hasKeyword($keyword)) { |
|
| 66 | - throw new \InvalidArgumentException("The keyword $keyword is not set.", 32); |
|
| 67 | - } |
|
| 57 | + /** |
|
| 58 | + * Replace custom keyword. |
|
| 59 | + * |
|
| 60 | + * @param string $keyword the keyword to be found. |
|
| 61 | + * @param callable $action action to be executed when the keyword is found. |
|
| 62 | + */ |
|
| 63 | + public function replaceKeyword($keyword, $action) |
|
| 64 | + { |
|
| 65 | + if (!$this->hasKeyword($keyword)) { |
|
| 66 | + throw new \InvalidArgumentException("The keyword $keyword is not set.", 32); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - $this->setKeyword($keyword, $action); |
|
| 70 | - } |
|
| 69 | + $this->setKeyword($keyword, $action); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Remove custom keyword. |
|
| 74 | - * |
|
| 75 | - * @param string $keyword the keyword to be removed. |
|
| 76 | - */ |
|
| 77 | - public function removeKeyword($keyword) |
|
| 78 | - { |
|
| 79 | - if ($this->hasKeyword($keyword)) { |
|
| 80 | - unset($this->options['customKeywords'][$keyword]); |
|
| 81 | - } |
|
| 82 | - } |
|
| 72 | + /** |
|
| 73 | + * Remove custom keyword. |
|
| 74 | + * |
|
| 75 | + * @param string $keyword the keyword to be removed. |
|
| 76 | + */ |
|
| 77 | + public function removeKeyword($keyword) |
|
| 78 | + { |
|
| 79 | + if ($this->hasKeyword($keyword)) { |
|
| 80 | + unset($this->options['customKeywords'][$keyword]); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -9,27 +9,27 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class Extensions |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Get main template file extension. |
|
| 14 | - * |
|
| 15 | - * @return string |
|
| 16 | - */ |
|
| 17 | - public function getExtension() |
|
| 18 | - { |
|
| 19 | - $extensions = new ExtensionsHelper($this->getOption('extension')); |
|
| 12 | + /** |
|
| 13 | + * Get main template file extension. |
|
| 14 | + * |
|
| 15 | + * @return string |
|
| 16 | + */ |
|
| 17 | + public function getExtension() |
|
| 18 | + { |
|
| 19 | + $extensions = new ExtensionsHelper($this->getOption('extension')); |
|
| 20 | 20 | |
| 21 | - return $extensions->getFirst(); |
|
| 22 | - } |
|
| 21 | + return $extensions->getFirst(); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get list of supported extensions. |
|
| 26 | - * |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - public function getExtensions() |
|
| 30 | - { |
|
| 31 | - $extensions = new ExtensionsHelper($this->getOption('extension')); |
|
| 24 | + /** |
|
| 25 | + * Get list of supported extensions. |
|
| 26 | + * |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + public function getExtensions() |
|
| 30 | + { |
|
| 31 | + $extensions = new ExtensionsHelper($this->getOption('extension')); |
|
| 32 | 32 | |
| 33 | - return $extensions->getExtensions(); |
|
| 34 | - } |
|
| 33 | + return $extensions->getExtensions(); |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -9,46 +9,46 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class Filters extends Extensions |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Register / override new filter. |
|
| 14 | - * |
|
| 15 | - * @param string name |
|
| 16 | - * @param callable filter |
|
| 17 | - * |
|
| 18 | - * @return $this |
|
| 19 | - */ |
|
| 20 | - public function filter($name, $filter) |
|
| 21 | - { |
|
| 22 | - $this->filters[$name] = $filter; |
|
| 23 | - |
|
| 24 | - return $this; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Check if a filter is registered. |
|
| 29 | - * |
|
| 30 | - * @param string name |
|
| 31 | - * |
|
| 32 | - * @return bool |
|
| 33 | - */ |
|
| 34 | - public function hasFilter($name) |
|
| 35 | - { |
|
| 36 | - $helper = new FilterHelper($this->filters, $this->options['filterAutoLoad']); |
|
| 37 | - |
|
| 38 | - return $helper->hasFilter($name); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Get a registered filter by name. |
|
| 43 | - * |
|
| 44 | - * @param string name |
|
| 45 | - * |
|
| 46 | - * @return callable |
|
| 47 | - */ |
|
| 48 | - public function getFilter($name) |
|
| 49 | - { |
|
| 50 | - $helper = new FilterHelper($this->filters, $this->options['filterAutoLoad']); |
|
| 51 | - |
|
| 52 | - return $helper->getFilter($name); |
|
| 53 | - } |
|
| 12 | + /** |
|
| 13 | + * Register / override new filter. |
|
| 14 | + * |
|
| 15 | + * @param string name |
|
| 16 | + * @param callable filter |
|
| 17 | + * |
|
| 18 | + * @return $this |
|
| 19 | + */ |
|
| 20 | + public function filter($name, $filter) |
|
| 21 | + { |
|
| 22 | + $this->filters[$name] = $filter; |
|
| 23 | + |
|
| 24 | + return $this; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Check if a filter is registered. |
|
| 29 | + * |
|
| 30 | + * @param string name |
|
| 31 | + * |
|
| 32 | + * @return bool |
|
| 33 | + */ |
|
| 34 | + public function hasFilter($name) |
|
| 35 | + { |
|
| 36 | + $helper = new FilterHelper($this->filters, $this->options['filterAutoLoad']); |
|
| 37 | + |
|
| 38 | + return $helper->hasFilter($name); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Get a registered filter by name. |
|
| 43 | + * |
|
| 44 | + * @param string name |
|
| 45 | + * |
|
| 46 | + * @return callable |
|
| 47 | + */ |
|
| 48 | + public function getFilter($name) |
|
| 49 | + { |
|
| 50 | + $helper = new FilterHelper($this->filters, $this->options['filterAutoLoad']); |
|
| 51 | + |
|
| 52 | + return $helper->getFilter($name); |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -9,35 +9,35 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class Cache extends Filters |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Get cached input/file a matching cache file exists. |
|
| 14 | - * Else, render the input, cache it in a file and return it. |
|
| 15 | - * |
|
| 16 | - * @param string input |
|
| 17 | - * |
|
| 18 | - * @throws \InvalidArgumentException |
|
| 19 | - * @throws \Exception |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function cache($input) |
|
| 24 | - { |
|
| 25 | - $cache = new CacheHelper($this); |
|
| 12 | + /** |
|
| 13 | + * Get cached input/file a matching cache file exists. |
|
| 14 | + * Else, render the input, cache it in a file and return it. |
|
| 15 | + * |
|
| 16 | + * @param string input |
|
| 17 | + * |
|
| 18 | + * @throws \InvalidArgumentException |
|
| 19 | + * @throws \Exception |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function cache($input) |
|
| 24 | + { |
|
| 25 | + $cache = new CacheHelper($this); |
|
| 26 | 26 | |
| 27 | - return $cache->cache($input); |
|
| 28 | - } |
|
| 27 | + return $cache->cache($input); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Scan a directory recursively, compile them and save them into the cache directory. |
|
| 32 | - * |
|
| 33 | - * @param string $directory the directory to search in pug templates |
|
| 34 | - * |
|
| 35 | - * @return array count of cached files and error count |
|
| 36 | - */ |
|
| 37 | - public function cacheDirectory($directory) |
|
| 38 | - { |
|
| 39 | - $cache = new CacheHelper($this); |
|
| 30 | + /** |
|
| 31 | + * Scan a directory recursively, compile them and save them into the cache directory. |
|
| 32 | + * |
|
| 33 | + * @param string $directory the directory to search in pug templates |
|
| 34 | + * |
|
| 35 | + * @return array count of cached files and error count |
|
| 36 | + */ |
|
| 37 | + public function cacheDirectory($directory) |
|
| 38 | + { |
|
| 39 | + $cache = new CacheHelper($this); |
|
| 40 | 40 | |
| 41 | - return $cache->cacheDirectory($directory); |
|
| 42 | - } |
|
| 41 | + return $cache->cacheDirectory($directory); |
|
| 42 | + } |
|
| 43 | 43 | } |