TwigTokenParserStyle.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @package Grav.Common.Twig
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Twig\TokenParser;
  9. use Grav\Common\Twig\Node\TwigNodeStyle;
  10. /**
  11. * Adds a style to the document.
  12. *
  13. * {% style 'theme://css/foo.css' priority: 20 %}
  14. * {% style priority: 20 with { media: 'screen' } %}
  15. * a { color: red; }
  16. * {% endstyle %}
  17. */
  18. class TwigTokenParserStyle extends \Twig_TokenParser
  19. {
  20. /**
  21. * Parses a token and returns a node.
  22. *
  23. * @param \Twig_Token $token A Twig_Token instance
  24. *
  25. * @return \Twig_Node A Twig_Node instance
  26. */
  27. public function parse(\Twig_Token $token)
  28. {
  29. $lineno = $token->getLine();
  30. $stream = $this->parser->getStream();
  31. list ($file, $group, $priority, $attributes) = $this->parseArguments($token);
  32. $content = null;
  33. if (!$file) {
  34. $content = $this->parser->subparse([$this, 'decideBlockEnd'], true);
  35. $stream->expect(\Twig_Token::BLOCK_END_TYPE);
  36. }
  37. return new TwigNodeStyle($content, $file, $group, $priority, $attributes, $lineno, $this->getTag());
  38. }
  39. /**
  40. * @param \Twig_Token $token
  41. * @return array
  42. */
  43. protected function parseArguments(\Twig_Token $token)
  44. {
  45. $stream = $this->parser->getStream();
  46. $file = null;
  47. if (!$stream->test(\Twig_Token::NAME_TYPE) && !$stream->test(\Twig_Token::OPERATOR_TYPE) && !$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
  48. $file = $this->parser->getExpressionParser()->parseExpression();
  49. }
  50. $group = null;
  51. if ($stream->nextIf(\Twig_Token::OPERATOR_TYPE, 'in')) {
  52. $group = $this->parser->getExpressionParser()->parseExpression();
  53. }
  54. $priority = null;
  55. if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'priority')) {
  56. $stream->expect(\Twig_Token::PUNCTUATION_TYPE, ':');
  57. $priority = $this->parser->getExpressionParser()->parseExpression();
  58. }
  59. $attributes = null;
  60. if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'with')) {
  61. $attributes = $this->parser->getExpressionParser()->parseExpression();
  62. }
  63. $stream->expect(\Twig_Token::BLOCK_END_TYPE);
  64. return [$file, $group, $priority, $attributes];
  65. }
  66. /**
  67. * @param \Twig_Token $token
  68. * @return bool
  69. */
  70. public function decideBlockEnd(\Twig_Token $token)
  71. {
  72. return $token->test('endstyle');
  73. }
  74. /**
  75. * Gets the tag name associated with this token parser.
  76. *
  77. * @return string The tag name
  78. */
  79. public function getTag()
  80. {
  81. return 'style';
  82. }
  83. }