TwigTokenParserMarkdown.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\TwigNodeMarkdown;
  10. /**
  11. * Adds ability to inline markdown between tags.
  12. *
  13. * {% markdown %}
  14. * This is **bold** and this _underlined_
  15. *
  16. * 1. This is a bullet list
  17. * 2. This is another item in that same list
  18. * {% endmarkdown %}
  19. */
  20. class TwigTokenParserMarkdown extends \Twig_TokenParser
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function parse(\Twig_Token $token)
  26. {
  27. $lineno = $token->getLine();
  28. $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
  29. $body = $this->parser->subparse(array($this, 'decideMarkdownEnd'), true);
  30. $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
  31. return new TwigNodeMarkdown($body, $lineno, $this->getTag());
  32. }
  33. /**
  34. * Decide if current token marks end of Markdown block.
  35. *
  36. * @param \Twig_Token $token
  37. * @return bool
  38. */
  39. public function decideMarkdownEnd(\Twig_Token $token)
  40. {
  41. return $token->test('endmarkdown');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getTag()
  47. {
  48. return 'markdown';
  49. }
  50. }