-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* @package Grav.Common.Twig | ||
* | ||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved. | ||
* @license MIT License; see LICENSE file for details. | ||
*/ | ||
|
||
namespace Grav\Common\Twig\Node; | ||
|
||
class TwigNodeMarkdown extends \Twig_Node implements \Twig_NodeOutputInterface | ||
{ | ||
public function __construct(\Twig_Node $body, $lineno, $tag = 'markdown') | ||
{ | ||
parent::__construct(array('body' => $body), array(), $lineno, $tag); | ||
} | ||
/** | ||
* Compiles the node to PHP. | ||
* | ||
* @param \Twig_Compiler A Twig_Compiler instance | ||
*/ | ||
public function compile(\Twig_Compiler $compiler) | ||
{ | ||
$compiler | ||
->addDebugInfo($this) | ||
->write('ob_start();' . PHP_EOL) | ||
->subcompile($this->getNode('body')) | ||
->write('$content = ob_get_clean();' . PHP_EOL) | ||
->write('preg_match("/^\s*/", $content, $matches);' . PHP_EOL) | ||
->write('$lines = explode("\n", $content);' . PHP_EOL) | ||
->write('$content = preg_replace(\'/^\' . $matches[0]. \'/\', "", $lines);' . PHP_EOL) | ||
->write('$content = join("\n", $content);' . PHP_EOL) | ||
->write('echo $this->env->getExtension(\'Grav\Common\Twig\TwigExtension\')->markdownFunction($content);' . PHP_EOL); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
system/src/Grav/Common/Twig/TokenParser/TwigTokenParserMarkdown.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/** | ||
* @package Grav.Common.Twig | ||
* | ||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved. | ||
* @license MIT License; see LICENSE file for details. | ||
*/ | ||
|
||
namespace Grav\Common\Twig\TokenParser; | ||
|
||
use Grav\Common\Twig\Node\TwigNodeMarkdown; | ||
|
||
/** | ||
* Adds ability to inline markdown between tags. | ||
* | ||
* {% markdown %} | ||
* This is **bold** and this _underlined_ | ||
* | ||
* 1. This is a bullet list | ||
* 2. This is another item in that same list | ||
* {% endmarkdown %} | ||
*/ | ||
class TwigTokenParserMarkdown extends \Twig_TokenParser | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parse(\Twig_Token $token) | ||
{ | ||
$lineno = $token->getLine(); | ||
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); | ||
$body = $this->parser->subparse(array($this, 'decideMarkdownEnd'), true); | ||
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); | ||
return new TwigNodeMarkdown($body, $lineno, $this->getTag()); | ||
} | ||
/** | ||
* Decide if current token marks end of Markdown block. | ||
* | ||
* @param \Twig_Token $token | ||
* @return bool | ||
*/ | ||
public function decideMarkdownEnd(\Twig_Token $token) | ||
{ | ||
return $token->test('endmarkdown'); | ||
} | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTag() | ||
{ | ||
return 'markdown'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters