1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Twig; |
4
|
|
|
|
5
|
|
|
use allejo\stakx\Utilities\HtmlUtils; |
6
|
|
|
use Twig_Environment; |
7
|
|
|
use Twig_Extension; |
8
|
|
|
use Twig_SimpleFilter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This file is part of Twig. |
12
|
|
|
* |
13
|
|
|
* (c) 2009 Fabien Potencier |
14
|
|
|
* |
15
|
|
|
* For the full copyright and license information, please view the LICENSE |
16
|
|
|
* file that was distributed with this source code. |
17
|
|
|
* |
18
|
|
|
* @author Henrik Bjornskov <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class TextExtension extends Twig_Extension |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Returns a list of filters. |
24
|
|
|
* |
25
|
|
|
* @return Twig_SimpleFilter[] |
26
|
|
|
*/ |
27
|
1 |
|
public function getFilters() |
28
|
|
|
{ |
29
|
|
|
return array( |
30
|
1 |
|
new Twig_SimpleFilter('summary', array($this, 'twig_summary_filter')), |
31
|
1 |
|
new Twig_SimpleFilter('truncate', array($this, 'twig_truncate_filter'), array('needs_environment' => true)), |
32
|
1 |
|
new Twig_SimpleFilter('wordwrap', array($this, 'twig_wordwrap_filter'), array('needs_environment' => true)), |
33
|
1 |
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Name of this extension. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
14 |
|
public function getName() |
42
|
|
|
{ |
43
|
14 |
|
return 'Text'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function twig_summary_filter($value, $paragraphCount = 1) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
if (!extension_loaded('dom')) |
49
|
|
|
{ |
50
|
|
|
@trigger_error('The DOM Extension is not loaded and is necessary for the "summary" Twig filter.', E_WARNING); |
|
|
|
|
51
|
|
|
return $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$dom = new \DOMDocument(); |
55
|
|
|
$paragraphs = HtmlUtils::htmlXPath($dom, $value, sprintf('//body/p[position() <= %d]', $paragraphCount)); |
56
|
|
|
|
57
|
|
|
$summary = ''; |
58
|
|
|
|
59
|
|
|
foreach ($paragraphs as $paragraph) |
60
|
|
|
{ |
61
|
|
|
$summary .= $dom->saveHTML($paragraph); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $summary; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (mb_strlen($value, $env->getCharset()) > $length) |
70
|
|
|
{ |
71
|
|
|
if ($preserve) |
72
|
|
|
{ |
73
|
|
|
// If breakpoint is on the last word, return the value without separator. |
74
|
|
|
if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) |
75
|
|
|
{ |
76
|
|
|
return $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$length = $breakpoint; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$sentences = array(); |
91
|
|
|
|
92
|
|
|
$previous = mb_regex_encoding(); |
93
|
|
|
mb_regex_encoding($env->getCharset()); |
94
|
|
|
|
95
|
|
|
$pieces = mb_split($separator, $value); |
96
|
|
|
mb_regex_encoding($previous); |
97
|
|
|
|
98
|
|
|
foreach ($pieces as $piece) |
99
|
|
|
{ |
100
|
|
|
while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) |
101
|
|
|
{ |
102
|
|
|
$sentences[] = mb_substr($piece, 0, $length, $env->getCharset()); |
103
|
|
|
$piece = mb_substr($piece, $length, 2048, $env->getCharset()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$sentences[] = $piece; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return implode($separator, $sentences); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.