Last active
September 22, 2016 10:16
-
-
Save liayn/6ebd781fbc5618e927d64e430f8d3784 to your computer and use it in GitHub Desktop.
LaTex Fluid Parser
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
<?php | |
/** | |
* (c) by Reelworx GmbH, Markus Klein <markus.klein@reelworx.at> | |
* | |
* License GPL 3 | |
*/ | |
namespace Reelworx\LatexFluid; | |
use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode; | |
use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\TextNode; | |
use TYPO3\CMS\Fluid\Core\Parser\TemplateParser; | |
/** | |
* Dedicated parser for LaTex files | |
* | |
* This parser replaces the { and } characters for inline syntax by the delimiters defined in $inlineDelimiters | |
*/ | |
class LaTexParser extends TemplateParser | |
{ | |
/** | |
* @var array | |
*/ | |
protected static $inlineDelimiters = ['|','§']; | |
/** | |
* Modify regular expressions of parent | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
// quote the delimiters for regex | |
$escapedDelimiters = array_map(function($v) { return preg_quote($v); }, self::$inlineDelimiters); | |
static::$SPLIT_PATTERN_SHORTHANDSYNTAX = str_replace(['{','}'], $escapedDelimiters, static::$SPLIT_PATTERN_SHORTHANDSYNTAX); | |
static::$SCAN_PATTERN_SHORTHANDSYNTAX_OBJECTACCESSORS = str_replace(['{','}'], $escapedDelimiters, static::$SCAN_PATTERN_SHORTHANDSYNTAX_OBJECTACCESSORS); | |
static::$SPLIT_PATTERN_SHORTHANDSYNTAX_VIEWHELPER = str_replace(['{','}'], $escapedDelimiters, static::$SPLIT_PATTERN_SHORTHANDSYNTAX_VIEWHELPER); | |
static::$SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS = str_replace(['{','}'], $escapedDelimiters, static::$SCAN_PATTERN_SHORTHANDSYNTAX_ARRAYS); | |
static::$SPLIT_PATTERN_SHORTHANDSYNTAX_ARRAY_PARTS = str_replace(['{','}'], $escapedDelimiters, static::$SPLIT_PATTERN_SHORTHANDSYNTAX_ARRAY_PARTS); | |
} | |
/** | |
* Build up an argument object tree for the string in $argumentString. | |
* This builds up the tree for a single argument value. | |
* | |
* This method also does some performance optimizations, so in case | |
* no { or < is found, then we just return a TextNode. | |
* | |
* @param string $argumentString | |
* @return AbstractNode the corresponding argument object tree. | |
*/ | |
protected function buildArgumentObjectTree($argumentString) | |
{ | |
if (strpos($argumentString, static::$inlineDelimiters[0]) === false && strpos($argumentString, '<') === false) { | |
return $this->objectManager->get(TextNode::class, $argumentString); | |
} | |
$splitArgument = $this->splitTemplateAtDynamicTags($argumentString); | |
$rootNode = $this->buildObjectTree($splitArgument, self::CONTEXT_INSIDE_VIEWHELPER_ARGUMENTS)->getRootNode(); | |
return $rootNode; | |
} | |
} |
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
<?php | |
/** | |
* (c) by Reelworx GmbH, Markus Klein <markus.klein@reelworx.at> | |
* | |
* License GPL 3 | |
*/ | |
namespace Reelworx\LatexFluid; | |
use TYPO3\CMS\Fluid\View\StandaloneView; | |
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; | |
/** | |
* Special view to render LaTex files | |
* | |
* This view explicitly uses the LaTexParser to parse templates. | |
*/ | |
class LaTexView extends StandaloneView | |
{ | |
/** | |
* @param ContentObjectRenderer $contentObject | |
*/ | |
public function __construct(ContentObjectRenderer $contentObject) | |
{ | |
parent::__construct($contentObject); | |
$this->templateParser = $this->objectManager->get(LaTexParser::class); | |
$this->setFormat('tex'); | |
} | |
} |
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
<?php | |
/* partial code only */ | |
class MyController { | |
public function someAction() | |
{ | |
$view = GeneralUtility::makeInstance(LaTexView::class, $GLOBALS['TSFE']->cObj); | |
$view->setTemplateSource(' | |
\textbf{|title§} | |
<f:for each="|names§" as="n">|n§ | |
</f:for> | |
|f:if(condition:ahoi,then:\'-->> yes <<--\')§ | |
'); | |
$view->assign('title', 'Words'); | |
$view->assign('ahoi', true); | |
$view->assign('names', ['I', 'you', 'he']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment