summaryrefslogtreecommitdiff
path: root/inc/lib/Twig/Extensions
diff options
context:
space:
mode:
authorMichael Save <savetheinternet@tinyboard.org>2012-04-09 20:52:26 +1000
committerMichael Save <savetheinternet@tinyboard.org>2012-04-09 20:52:26 +1000
commit874b8cbf002e9d8bf0ff39671e1501165016320a (patch)
tree1428acec23fef6c607e10fd355d88af31c2029ed /inc/lib/Twig/Extensions
parent69c9a9dd452299e0e8f7335966a1d0b4036df583 (diff)
inc/contrib -> inc/lib
Diffstat (limited to 'inc/lib/Twig/Extensions')
-rw-r--r--inc/lib/Twig/Extensions/Extension/I18n.php45
-rw-r--r--inc/lib/Twig/Extensions/Extension/Tinyboard.php92
-rw-r--r--inc/lib/Twig/Extensions/Node/Trans.php130
-rw-r--r--inc/lib/Twig/Extensions/TokenParser/Trans.php80
4 files changed, 347 insertions, 0 deletions
diff --git a/inc/lib/Twig/Extensions/Extension/I18n.php b/inc/lib/Twig/Extensions/Extension/I18n.php
new file mode 100644
index 00000000..1f795490
--- /dev/null
+++ b/inc/lib/Twig/Extensions/Extension/I18n.php
@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Extensions_Extension_I18n extends Twig_Extension
+{
+ /**
+ * Returns the token parser instances to add to the existing list.
+ *
+ * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
+ */
+ public function getTokenParsers()
+ {
+ return array(new Twig_Extensions_TokenParser_Trans());
+ }
+
+ /**
+ * Returns a list of filters to add to the existing list.
+ *
+ * @return array An array of filters
+ */
+ public function getFilters()
+ {
+ return array(
+ 'trans' => new Twig_Filter_Function('gettext'),
+ );
+ }
+
+ /**
+ * Returns the name of the extension.
+ *
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return 'i18n';
+ }
+}
+
diff --git a/inc/lib/Twig/Extensions/Extension/Tinyboard.php b/inc/lib/Twig/Extensions/Extension/Tinyboard.php
new file mode 100644
index 00000000..0cf7705a
--- /dev/null
+++ b/inc/lib/Twig/Extensions/Extension/Tinyboard.php
@@ -0,0 +1,92 @@
+<?php
+
+class Twig_Extensions_Extension_Tinyboard extends Twig_Extension
+{
+ /**
+ * Returns a list of filters to add to the existing list.
+ *
+ * @return array An array of filters
+ */
+ public function getFilters()
+ {
+ return Array(
+ 'filesize' => new Twig_Filter_Function('format_bytes', Array('needs_environment' => false)),
+ 'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => false)),
+ 'truncate_body' => new Twig_Filter_Function('truncate', array('needs_environment' => false)),
+ 'extension' => new Twig_Filter_Function('twig_extension_filter', array('needs_environment' => false)),
+ 'sprintf' => new Twig_Filter_Function('sprintf', array('needs_environment' => false)),
+ 'capcode' => new Twig_Filter_Function('capcode', array('needs_environment' => false)),
+ 'hasPermission' => new Twig_Filter_Function('twig_hasPermission_filter', array('needs_environment' => false)),
+ 'date' => new Twig_Filter_Function('twig_date_filter', array('needs_environment' => false)),
+ 'poster_id' => new Twig_Filter_Function('poster_id', array('needs_environment' => false)),
+ 'remove_whitespace' => new Twig_Filter_Function('twig_remove_whitespace_filter', array('needs_environment' => false)),
+ 'count' => new Twig_Filter_Function('count', array('needs_environment' => false)),
+ 'until' => new Twig_Filter_Function('until', array('needs_environment' => false))
+ );
+ }
+
+ /**
+ * Returns a list of functions to add to the existing list.
+ *
+ * @return array An array of filters
+ */
+ public function getFunctions()
+ {
+ return Array(
+ 'time' => new Twig_Filter_Function('time', array('needs_environment' => false)),
+ 'timezone' => new Twig_Filter_Function('twig_timezone_function', array('needs_environment' => false)),
+ 'createHiddenInputs' => new Twig_Filter_Function('createHiddenInputs', array('needs_environment' => false))
+ );
+ }
+
+ /**
+ * Returns the name of the extension.
+ *
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return 'tinyboard';
+ }
+}
+
+function twig_timezone_function() {
+ // there's probably a much easier way of doing this
+ return sprintf("%s%02d", ($hr = (int)floor(($tz = date('Z')) / 3600)) > 0 ? '+' : '-', abs($hr)) . ':' . sprintf("%02d", (($tz / 3600) - $hr) * 60);
+}
+
+function twig_remove_whitespace_filter($data) {
+ return preg_replace('/[\t\r\n]/', '', $data);
+}
+
+function twig_date_filter($date, $format) {
+ return strftime($format, $date);
+}
+
+function twig_hasPermission_filter($mod, $permission, $board) {
+ return hasPermission($permission, $board, $mod);
+}
+
+function twig_extension_filter($value, $case_insensitive = true) {
+ $ext = substr($value, strrpos($value, '.') + 1);
+ if($case_insensitive)
+ $ext = strtolower($ext);
+ return $ext;
+}
+
+function twig_sprintf_filter( $value, $var) {
+ return sprintf($value, $var);
+}
+
+function twig_truncate_filter($value, $length = 30, $preserve = false, $separator = '&hellip;') {
+ if (mb_strlen($value) > $length) {
+ if ($preserve) {
+ if (false !== ($breakpoint = strpos($value, ' ', $length))) {
+ $length = $breakpoint;
+ }
+ }
+ return substr($value, 0, $length) . $separator;
+ }
+ return $value;
+}
+
diff --git a/inc/lib/Twig/Extensions/Node/Trans.php b/inc/lib/Twig/Extensions/Node/Trans.php
new file mode 100644
index 00000000..18087e77
--- /dev/null
+++ b/inc/lib/Twig/Extensions/Node/Trans.php
@@ -0,0 +1,130 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Represents a trans node.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class Twig_Extensions_Node_Trans extends Twig_Node
+{
+ public function __construct(Twig_NodeInterface $body, Twig_NodeInterface $plural = null, Twig_Node_Expression $count = null, $lineno, $tag = null)
+ {
+ parent::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural), array(), $lineno, $tag);
+ }
+
+ /**
+ * Compiles the node to PHP.
+ *
+ * @param Twig_Compiler A Twig_Compiler instance
+ */
+ public function compile(Twig_Compiler $compiler)
+ {
+ $compiler->addDebugInfo($this);
+
+ list($msg, $vars) = $this->compileString($this->getNode('body'));
+
+ if (null !== $this->getNode('plural')) {
+ list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
+
+ $vars = array_merge($vars, $vars1);
+ }
+
+ $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
+
+ if ($vars) {
+ $compiler
+ ->write('echo strtr('.$function.'(')
+ ->subcompile($msg)
+ ;
+
+ if (null !== $this->getNode('plural')) {
+ $compiler
+ ->raw(', ')
+ ->subcompile($msg1)
+ ->raw(', abs(')
+ ->subcompile($this->getNode('count'))
+ ->raw(')')
+ ;
+ }
+
+ $compiler->raw('), array(');
+
+ foreach ($vars as $var) {
+ if ('count' === $var->getAttribute('name')) {
+ $compiler
+ ->string('%count%')
+ ->raw(' => abs(')
+ ->subcompile($this->getNode('count'))
+ ->raw('), ')
+ ;
+ } else {
+ $compiler
+ ->string('%'.$var->getAttribute('name').'%')
+ ->raw(' => ')
+ ->subcompile($var)
+ ->raw(', ')
+ ;
+ }
+ }
+
+ $compiler->raw("));\n");
+ } else {
+ $compiler
+ ->write('echo '.$function.'(')
+ ->subcompile($msg)
+ ;
+
+ if (null !== $this->getNode('plural')) {
+ $compiler
+ ->raw(', ')
+ ->subcompile($msg1)
+ ->raw(', abs(')
+ ->subcompile($this->getNode('count'))
+ ->raw(')')
+ ;
+ }
+
+ $compiler->raw(');');
+ }
+ }
+
+ protected function compileString(Twig_NodeInterface $body)
+ {
+ if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant) {
+ return array($body, array());
+ }
+
+ $vars = array();
+
+ if (count($body)) {
+ $msg = '';
+
+ foreach ($body as $node) {
+ if ($node instanceof Twig_Node_Print) {
+ $n = $node->getNode('expr');
+ while ($n instanceof Twig_Node_Expression_Filter) {
+ $n = $n->getNode('node');
+ }
+ $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
+ $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
+ } else {
+ $msg .= $node->getAttribute('data');
+ }
+ }
+ } else {
+ $msg = $body->getAttribute('data');
+ }
+
+ return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
+ }
+}
diff --git a/inc/lib/Twig/Extensions/TokenParser/Trans.php b/inc/lib/Twig/Extensions/TokenParser/Trans.php
new file mode 100644
index 00000000..5b2d93b6
--- /dev/null
+++ b/inc/lib/Twig/Extensions/TokenParser/Trans.php
@@ -0,0 +1,80 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Extensions_TokenParser_Trans extends Twig_TokenParser
+{
+ /**
+ * Parses a token and returns a node.
+ *
+ * @param Twig_Token $token A Twig_Token instance
+ *
+ * @return Twig_NodeInterface A Twig_NodeInterface instance
+ */
+ public function parse(Twig_Token $token)
+ {
+ $lineno = $token->getLine();
+ $stream = $this->parser->getStream();
+ $count = null;
+ $plural = null;
+
+ if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
+ $body = $this->parser->getExpressionParser()->parseExpression();
+ } else {
+ $stream->expect(Twig_Token::BLOCK_END_TYPE);
+ $body = $this->parser->subparse(array($this, 'decideForFork'));
+ if ('plural' === $stream->next()->getValue()) {
+ $count = $this->parser->getExpressionParser()->parseExpression();
+ $stream->expect(Twig_Token::BLOCK_END_TYPE);
+ $plural = $this->parser->subparse(array($this, 'decideForEnd'), true);
+ }
+ }
+
+ $stream->expect(Twig_Token::BLOCK_END_TYPE);
+
+ $this->checkTransString($body, $lineno);
+
+ return new Twig_Extensions_Node_Trans($body, $plural, $count, $lineno, $this->getTag());
+ }
+
+ public function decideForFork($token)
+ {
+ return $token->test(array('plural', 'endtrans'));
+ }
+
+ public function decideForEnd($token)
+ {
+ return $token->test('endtrans');
+ }
+
+ /**
+ * Gets the tag name associated with this token parser.
+ *
+ * @param string The tag name
+ */
+ public function getTag()
+ {
+ return 'trans';
+ }
+
+ protected function checkTransString(Twig_NodeInterface $body, $lineno)
+ {
+ foreach ($body as $i => $node) {
+ if (
+ $node instanceof Twig_Node_Text
+ ||
+ ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_Name)
+ ) {
+ continue;
+ }
+
+ throw new Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
+ }
+ }
+}