1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
<?php
/**
* Text Helper
*
* Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View.Helper
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('AppHelper', 'View/Helper');
/**
* Text helper library.
*
* Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
*
* @package Cake.View.Helper
* @property HtmlHelper $Html
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
* @see String
*/
class TextHelper extends AppHelper {
/**
* helpers
*
* @var array
*/
public $helpers = array('Html');
/**
* An array of md5sums and their contents.
* Used when inserting links into text.
*
* @var array
*/
protected $_placeholders = array();
/**
* String utility instance
*/
protected $_engine;
/**
* Constructor
*
* ### Settings:
*
* - `engine` Class name to use to replace String functionality.
* The class needs to be placed in the `Utility` directory.
*
* @param View $View the view object the helper is attached to.
* @param array $settings Settings array Settings array
* @throws CakeException when the engine class could not be found.
*/
public function __construct(View $View, $settings = array()) {
$settings = Hash::merge(array('engine' => 'String'), $settings);
parent::__construct($View, $settings);
list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
App::uses($engineClass, $plugin . 'Utility');
if (class_exists($engineClass)) {
$this->_engine = new $engineClass($settings);
} else {
throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
}
}
/**
* Call methods from String utility class
*/
public function __call($method, $params) {
return call_user_func_array(array($this->_engine, $method), $params);
}
/**
* Adds links (<a href=....) to a given text, by finding text that begins with
* strings like http:// and ftp://.
*
* ### Options
*
* - `escape` Control HTML escaping of input. Defaults to true.
*
* @param string $text Text
* @param array $options Array of HTML options, and options listed above.
* @return string The text with links
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
*/
public function autoLinkUrls($text, $options = array()) {
$this->_placeholders = array();
$options += array('escape' => true);
$text = preg_replace_callback(
'#(?<!href="|src="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i',
array(&$this, '_insertPlaceHolder'),
$text
);
$text = preg_replace_callback(
'#(?<!href="|">)(?<!\b[[:punct:]])(?<!http://|https://|ftp://|nntp://)www.[^\n\%\ <]+[^<\n\%\,\.\ <](?<!\))#i',
array(&$this, '_insertPlaceHolder'),
$text
);
if ($options['escape']) {
$text = h($text);
}
return $this->_linkUrls($text, $options);
}
/**
* Saves the placeholder for a string, for later use. This gets around double
* escaping content in URL's.
*
* @param array $matches An array of regexp matches.
* @return string Replaced values.
*/
protected function _insertPlaceHolder($matches) {
$key = md5($matches[0]);
$this->_placeholders[$key] = $matches[0];
return $key;
}
/**
* Replace placeholders with links.
*
* @param string $text The text to operate on.
* @param array $htmlOptions The options for the generated links.
* @return string The text with links inserted.
*/
protected function _linkUrls($text, $htmlOptions) {
$replace = array();
foreach ($this->_placeholders as $hash => $url) {
$link = $url;
if (!preg_match('#^[a-z]+\://#', $url)) {
$url = 'http://' . $url;
}
$replace[$hash] = $this->Html->link($link, $url, $htmlOptions);
}
return strtr($text, $replace);
}
/**
* Links email addresses
*
* @param string $text The text to operate on
* @param array $options An array of options to use for the HTML.
* @return string
* @see TextHelper::autoLinkEmails()
*/
protected function _linkEmails($text, $options) {
$replace = array();
foreach ($this->_placeholders as $hash => $url) {
$replace[$hash] = $this->Html->link($url, 'mailto:' . $url, $options);
}
return strtr($text, $replace);
}
/**
* Adds email links (<a href="mailto:....) to a given text.
*
* ### Options
*
* - `escape` Control HTML escaping of input. Defaults to true.
*
* @param string $text Text
* @param array $options Array of HTML options, and options listed above.
* @return string The text with links
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
*/
public function autoLinkEmails($text, $options = array()) {
$options += array('escape' => true);
$this->_placeholders = array();
$atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
$text = preg_replace_callback(
'/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)+)/i',
array(&$this, '_insertPlaceholder'),
$text
);
if ($options['escape']) {
$text = h($text);
}
return $this->_linkEmails($text, $options);
}
/**
* Convert all links and email addresses to HTML links.
*
* ### Options
*
* - `escape` Control HTML escaping of input. Defaults to true.
*
* @param string $text Text
* @param array $options Array of HTML options, and options listed above.
* @return string The text with links
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
*/
public function autoLink($text, $options = array()) {
$text = $this->autoLinkUrls($text, $options);
return $this->autoLinkEmails($text, array_merge($options, array('escape' => false)));
}
/**
* @see String::highlight()
*
* @param string $text Text to search the phrase in
* @param string $phrase The phrase that will be searched
* @param array $options An array of html attributes and options.
* @return string The highlighted text
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
*/
public function highlight($text, $phrase, $options = array()) {
return $this->_engine->highlight($text, $phrase, $options);
}
/**
* @see String::stripLinks()
*
* @param string $text Text
* @return string The text without links
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
*/
public function stripLinks($text) {
return $this->_engine->stripLinks($text);
}
/**
* @see String::truncate()
*
* @param string $text String to truncate.
* @param integer $length Length of returned string, including ellipsis.
* @param array $options An array of html attributes and options.
* @return string Trimmed string.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
*/
public function truncate($text, $length = 100, $options = array()) {
return $this->_engine->truncate($text, $length, $options);
}
/**
* @see String::excerpt()
*
* @param string $text String to search the phrase in
* @param string $phrase Phrase that will be searched for
* @param integer $radius The amount of characters that will be returned on each side of the founded phrase
* @param string $ending Ending that will be appended
* @return string Modified string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
*/
public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
return $this->_engine->excerpt($text, $phrase, $radius, $ending);
}
/**
* @see String::toList()
*
* @param array $list The list to be joined
* @param string $and The word used to join the last and second last items together with. Defaults to 'and'
* @param string $separator The separator used to join all the other items together. Defaults to ', '
* @return string The glued together string.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
*/
public function toList($list, $and = 'and', $separator = ', ') {
return $this->_engine->toList($list, $and, $separator);
}
}
|