aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/Twig/Extensions/Extension
diff options
context:
space:
mode:
Diffstat (limited to 'inc/Twig/Extensions/Extension')
-rw-r--r--inc/Twig/Extensions/Extension/Debug.php34
-rw-r--r--inc/Twig/Extensions/Extension/I18n.php44
-rw-r--r--inc/Twig/Extensions/Extension/Intl.php66
-rw-r--r--inc/Twig/Extensions/Extension/Text.php109
4 files changed, 253 insertions, 0 deletions
diff --git a/inc/Twig/Extensions/Extension/Debug.php b/inc/Twig/Extensions/Extension/Debug.php
new file mode 100644
index 00000000..8974ce20
--- /dev/null
+++ b/inc/Twig/Extensions/Extension/Debug.php
@@ -0,0 +1,34 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11class Twig_Extensions_Extension_Debug extends Twig_Extension
12{
13 /**
14 * Returns the token parser instance to add to the existing list.
15 *
16 * @return array An array of Twig_TokenParser instances
17 */
18 public function getTokenParsers()
19 {
20 return array(
21 new Twig_Extensions_TokenParser_Debug(),
22 );
23 }
24
25 /**
26 * Returns the name of the extension.
27 *
28 * @return string The extension name
29 */
30 public function getName()
31 {
32 return 'debug';
33 }
34}
diff --git a/inc/Twig/Extensions/Extension/I18n.php b/inc/Twig/Extensions/Extension/I18n.php
new file mode 100644
index 00000000..3702aa22
--- /dev/null
+++ b/inc/Twig/Extensions/Extension/I18n.php
@@ -0,0 +1,44 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11class Twig_Extensions_Extension_I18n extends Twig_Extension
12{
13 /**
14 * Returns the token parser instances to add to the existing list.
15 *
16 * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
17 */
18 public function getTokenParsers()
19 {
20 return array(new Twig_Extensions_TokenParser_Trans());
21 }
22
23 /**
24 * Returns a list of filters to add to the existing list.
25 *
26 * @return array An array of filters
27 */
28 public function getFilters()
29 {
30 return array(
31 'trans' => new Twig_Filter_Function('gettext'),
32 );
33 }
34
35 /**
36 * Returns the name of the extension.
37 *
38 * @return string The extension name
39 */
40 public function getName()
41 {
42 return 'i18n';
43 }
44}
diff --git a/inc/Twig/Extensions/Extension/Intl.php b/inc/Twig/Extensions/Extension/Intl.php
new file mode 100644
index 00000000..40f7fc20
--- /dev/null
+++ b/inc/Twig/Extensions/Extension/Intl.php
@@ -0,0 +1,66 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12class Twig_Extensions_Extension_Intl extends Twig_Extension
13{
14 public function __construct()
15 {
16 if (!class_exists('IntlDateFormatter')) {
17 throw new RuntimeException('The intl extension is needed to use intl-based filters.');
18 }
19 }
20
21 /**
22 * Returns a list of filters to add to the existing list.
23 *
24 * @return array An array of filters
25 */
26 public function getFilters()
27 {
28 return array(
29 'localizeddate' => new Twig_Filter_Function('twig_localized_date_filter', array('needs_environment' => true)),
30 );
31 }
32
33 /**
34 * Returns the name of the extension.
35 *
36 * @return string The extension name
37 */
38 public function getName()
39 {
40 return 'intl';
41 }
42}
43
44function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null)
45{
46 $date = twig_date_converter($env, $date, $timezone);
47
48 $formatValues = array(
49 'none' => IntlDateFormatter::NONE,
50 'short' => IntlDateFormatter::SHORT,
51 'medium' => IntlDateFormatter::MEDIUM,
52 'long' => IntlDateFormatter::LONG,
53 'full' => IntlDateFormatter::FULL,
54 );
55
56 $formatter = IntlDateFormatter::create(
57 $locale !== null ? $locale : Locale::getDefault(),
58 $formatValues[$dateFormat],
59 $formatValues[$timeFormat],
60 $date->getTimezone()->getName(),
61 IntlDateFormatter::GREGORIAN,
62 $format
63 );
64
65 return $formatter->format($date->getTimestamp());
66}
diff --git a/inc/Twig/Extensions/Extension/Text.php b/inc/Twig/Extensions/Extension/Text.php
new file mode 100644
index 00000000..0a3dc35e
--- /dev/null
+++ b/inc/Twig/Extensions/Extension/Text.php
@@ -0,0 +1,109 @@
1<?php
2
3/**
4 * This file is part of Twig.
5 *
6 * (c) 2009 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * @author Henrik Bjornskov <hb@peytz.dk>
12 * @package Twig
13 * @subpackage Twig-extensions
14 */
15class Twig_Extensions_Extension_Text extends Twig_Extension
16{
17 /**
18 * Returns a list of filters.
19 *
20 * @return array
21 */
22 public function getFilters()
23 {
24 $filters = array(
25 'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => true)),
26 'wordwrap' => new Twig_Filter_Function('twig_wordwrap_filter', array('needs_environment' => true)),
27 );
28
29 if (version_compare(Twig_Environment::VERSION, '1.5.0-DEV', '<')) {
30 $filters['nl2br'] = new Twig_Filter_Function('twig_nl2br_filter', array('pre_escape' => 'html', 'is_safe' => array('html')));
31 }
32
33 return $filters;
34 }
35
36 /**
37 * Name of this extension
38 *
39 * @return string
40 */
41 public function getName()
42 {
43 return 'Text';
44 }
45}
46
47function twig_nl2br_filter($value, $sep = '<br />')
48{
49 return str_replace("\n", $sep."\n", $value);
50}
51
52if (function_exists('mb_get_info')) {
53 function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
54 {
55 if (mb_strlen($value, $env->getCharset()) > $length) {
56 if ($preserve) {
57 if (false !== ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
58 $length = $breakpoint;
59 }
60 }
61
62 return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator;
63 }
64
65 return $value;
66 }
67
68 function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
69 {
70 $sentences = array();
71
72 $previous = mb_regex_encoding();
73 mb_regex_encoding($env->getCharset());
74
75 $pieces = mb_split($separator, $value);
76 mb_regex_encoding($previous);
77
78 foreach ($pieces as $piece) {
79 while(!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
80 $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
81 $piece = mb_substr($piece, $length, 2048, $env->getCharset());
82 }
83
84 $sentences[] = $piece;
85 }
86
87 return implode($separator, $sentences);
88 }
89} else {
90 function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
91 {
92 if (strlen($value) > $length) {
93 if ($preserve) {
94 if (false !== ($breakpoint = strpos($value, ' ', $length))) {
95 $length = $breakpoint;
96 }
97 }
98
99 return rtrim(substr($value, 0, $length)) . $separator;
100 }
101
102 return $value;
103 }
104
105 function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
106 {
107 return wordwrap($value, $length, $separator, !$preserve);
108 }
109} \ No newline at end of file