From 48c5fb46155d95430063e0b0e8ece04c738841fa Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Thu, 1 Aug 2013 15:55:28 -0400 Subject: [PATCH] Continue upgrading Twig --- inc/lib/Twig/Extensions/Extension/I18n.php | 2 +- .../Twig/Extensions/Extension/Tinyboard.php | 46 +++++++++---------- inc/lib/Twig/Extensions/Node/Trans.php | 11 +++-- inc/lib/Twig/Extensions/TokenParser/Trans.php | 6 +-- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/inc/lib/Twig/Extensions/Extension/I18n.php b/inc/lib/Twig/Extensions/Extension/I18n.php index 1f795490..bc6a05bd 100644 --- a/inc/lib/Twig/Extensions/Extension/I18n.php +++ b/inc/lib/Twig/Extensions/Extension/I18n.php @@ -28,7 +28,7 @@ class Twig_Extensions_Extension_I18n extends Twig_Extension public function getFilters() { return array( - 'trans' => new Twig_Filter_Function('gettext'), + new Twig_SimpleFilter('trans', 'gettext'), ); } diff --git a/inc/lib/Twig/Extensions/Extension/Tinyboard.php b/inc/lib/Twig/Extensions/Extension/Tinyboard.php index 8c8d9456..727d5d2c 100644 --- a/inc/lib/Twig/Extensions/Extension/Tinyboard.php +++ b/inc/lib/Twig/Extensions/Extension/Tinyboard.php @@ -9,23 +9,23 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension */ public function getFilters() { - return Array( - 'filesize' => new Twig_Filter_Function('format_bytes'), - 'truncate' => new Twig_Filter_Function('twig_truncate_filter'), - 'truncate_body' => new Twig_Filter_Function('truncate'), - 'extension' => new Twig_Filter_Function('twig_extension_filter'), - 'sprintf' => new Twig_Filter_Function('sprintf'), - 'capcode' => new Twig_Filter_Function('capcode'), - 'hasPermission' => new Twig_Filter_Function('twig_hasPermission_filter'), - 'date' => new Twig_Filter_Function('twig_date_filter'), - 'poster_id' => new Twig_Filter_Function('poster_id'), - 'remove_whitespace' => new Twig_Filter_Function('twig_remove_whitespace_filter'), - 'count' => new Twig_Filter_Function('count'), - 'ago' => new Twig_Filter_Function('ago'), - 'until' => new Twig_Filter_Function('until'), - 'push' => new Twig_Filter_Function('twig_push_filter'), - 'bidi_cleanup' => new Twig_Filter_Function('bidi_cleanup'), - 'addslashes' => new Twig_Filter_Function('addslashes') + return array( + new Twig_SimpleFilter('filesize', 'format_bytes'), + new Twig_SimpleFilter('truncate', 'twig_truncate_filter'), + new Twig_SimpleFilter('truncate_body', 'truncate'), + new Twig_SimpleFilter('extension', 'twig_extension_filter'), + new Twig_SimpleFilter('sprintf', 'sprintf'), + new Twig_SimpleFilter('capcode', 'capcode'), + new Twig_SimpleFilter('hasPermission', 'twig_hasPermission_filter'), + new Twig_SimpleFilter('date', 'twig_date_filter'), + new Twig_SimpleFilter('poster_id', 'poster_id'), + new Twig_SimpleFilter('remove_whitespace', 'twig_remove_whitespace_filter'), + new Twig_SimpleFilter('count', 'count'), + new Twig_SimpleFilter('ago', 'ago'), + new Twig_SimpleFilter('until', 'until'), + new Twig_SimpleFilter('push', 'twig_push_filter'), + new Twig_SimpleFilter('bidi_cleanup', 'bidi_cleanup'), + new Twig_SimpleFilter('addslashes', 'addslashes') ); } @@ -36,12 +36,12 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension */ public function getFunctions() { - return Array( - 'time' => new Twig_Filter_Function('time'), - 'floor' => new Twig_Filter_Function('floor'), - 'timezone' => new Twig_Filter_Function('twig_timezone_function'), - 'hiddenInputs' => new Twig_Filter_Function('hiddenInputs'), - 'hiddenInputsHash' => new Twig_Filter_Function('hiddenInputsHash'), + return array( + new Twig_SimpleFunction('time', 'time'), + new Twig_SimpleFunction('floor', 'floor'), + new Twig_SimpleFunction('timezone', 'twig_timezone_function'), + new Twig_SimpleFunction('hiddenInputs', 'hiddenInputs'), + new Twig_SimpleFunction('hiddenInputsHash', 'hiddenInputsHash'), ); } diff --git a/inc/lib/Twig/Extensions/Node/Trans.php b/inc/lib/Twig/Extensions/Node/Trans.php index 18087e77..e7dc1ca8 100644 --- a/inc/lib/Twig/Extensions/Node/Trans.php +++ b/inc/lib/Twig/Extensions/Node/Trans.php @@ -94,22 +94,25 @@ class Twig_Extensions_Node_Trans extends Twig_Node ; } - $compiler->raw(');'); + $compiler->raw(");\n"); } } protected function compileString(Twig_NodeInterface $body) { - if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant) { + if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant || $body instanceof Twig_Node_Expression_TempName) { return array($body, array()); } $vars = array(); - if (count($body)) { $msg = ''; foreach ($body as $node) { + if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof Twig_Node_SetTemp) { + $node = $node->getNode(1); + } + if ($node instanceof Twig_Node_Print) { $n = $node->getNode('expr'); while ($n instanceof Twig_Node_Expression_Filter) { @@ -127,4 +130,4 @@ class Twig_Extensions_Node_Trans extends Twig_Node return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars); } -} +} \ No newline at end of file diff --git a/inc/lib/Twig/Extensions/TokenParser/Trans.php b/inc/lib/Twig/Extensions/TokenParser/Trans.php index 5b2d93b6..4a0fad41 100644 --- a/inc/lib/Twig/Extensions/TokenParser/Trans.php +++ b/inc/lib/Twig/Extensions/TokenParser/Trans.php @@ -43,12 +43,12 @@ class Twig_Extensions_TokenParser_Trans extends Twig_TokenParser return new Twig_Extensions_Node_Trans($body, $plural, $count, $lineno, $this->getTag()); } - public function decideForFork($token) + public function decideForFork(Twig_Token $token) { return $token->test(array('plural', 'endtrans')); } - public function decideForEnd($token) + public function decideForEnd(Twig_Token $token) { return $token->test('endtrans'); } @@ -77,4 +77,4 @@ class Twig_Extensions_TokenParser_Trans extends Twig_TokenParser throw new Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno); } } -} +} \ No newline at end of file