|
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 return 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 'nl2br' => new Twig_Filter_Function('twig_nl2br_filter', array('pre_escape' => 'html', 'is_safe' => array('html'))), |
|
28 ); |
|
29 } |
|
30 |
|
31 /** |
|
32 * Name of this extension |
|
33 * |
|
34 * @return string |
|
35 */ |
|
36 public function getName() |
|
37 { |
|
38 return 'Text'; |
|
39 } |
|
40 } |
|
41 |
|
42 function twig_nl2br_filter($value, $sep = '<br />') |
|
43 { |
|
44 return str_replace("\n", $sep."\n", $value); |
|
45 } |
|
46 |
|
47 if (function_exists('mb_get_info')) { |
|
48 function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') |
|
49 { |
|
50 if (mb_strlen($value, $env->getCharset()) > $length) { |
|
51 if ($preserve) { |
|
52 if (false !== ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) { |
|
53 $length = $breakpoint; |
|
54 } |
|
55 } |
|
56 |
|
57 return mb_substr($value, 0, $length, $env->getCharset()) . $separator; |
|
58 } |
|
59 |
|
60 return $value; |
|
61 } |
|
62 |
|
63 function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) |
|
64 { |
|
65 $sentences = array(); |
|
66 |
|
67 $previous = mb_regex_encoding(); |
|
68 mb_regex_encoding($env->getCharset()); |
|
69 |
|
70 $pieces = mb_split($separator, $value); |
|
71 mb_regex_encoding($previous); |
|
72 |
|
73 foreach ($pieces as $piece) { |
|
74 while(!$preserve && mb_strlen($piece, $env->getCharset()) > $length) { |
|
75 $sentences[] = mb_substr($piece, 0, $length, $env->getCharset()); |
|
76 $piece = mb_substr($piece, $length, 2048, $env->getCharset()); |
|
77 } |
|
78 |
|
79 $sentences[] = $piece; |
|
80 } |
|
81 |
|
82 return implode($separator, $sentences); |
|
83 } |
|
84 } else { |
|
85 function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') |
|
86 { |
|
87 if (strlen($value) > $length) { |
|
88 if ($preserve) { |
|
89 if (false !== ($breakpoint = strpos($value, ' ', $length))) { |
|
90 $length = $breakpoint; |
|
91 } |
|
92 } |
|
93 |
|
94 return substr($value, 0, $length) . $separator; |
|
95 } |
|
96 |
|
97 return $value; |
|
98 } |
|
99 |
|
100 function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) |
|
101 { |
|
102 return wordwrap($value, $length, $separator, !$preserve); |
|
103 } |
|
104 } |