diff options
Diffstat (limited to 'inc/Twig/Extensions/Extension/Text.php')
-rw-r--r-- | inc/Twig/Extensions/Extension/Text.php | 109 |
1 files changed, 109 insertions, 0 deletions
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 | */ | ||
15 | class 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 | |||
47 | function twig_nl2br_filter($value, $sep = '<br />') | ||
48 | { | ||
49 | return str_replace("\n", $sep."\n", $value); | ||
50 | } | ||
51 | |||
52 | if (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 | ||