blob: 65924f9116a8473266c689d305b4c1e2111d6b42 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Typography Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
25 * @link http://www.codeigniter.com/user_guide/helpers/typography_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Convert newlines to HTML line breaks except within PRE tags
32 *
33 * @access public
34 * @param string
35 * @return string
36 */
37function nl2br_except_pre($str)
38{
39 $ex = explode("pre>",$str);
40 $ct = count($ex);
41
42 $newstr = "";
43 for ($i = 0; $i < $ct; $i++)
44 {
45 if (($i % 2) == 0)
46 {
47 $newstr .= nl2br($ex[$i]);
48 }
49 else
50 {
51 $newstr .= $ex[$i];
52 }
53
54 if ($ct - 1 != $i)
55 $newstr .= "pre>";
56 }
57
58 return $newstr;
59}
60
61// ------------------------------------------------------------------------
62
63/**
64 * Auto Typography Wrapper Function
65 *
66 *
67 * @access public
68 * @parm string
69 * @return string
70 */
71function auto_typography($str)
72{
73 $TYPE = new Auto_typography();
74 return $TYPE->convert($str);
75}
76
77// ------------------------------------------------------------------------
78
79/**
80 * Auto Typography Class
81 *
82 *
83 * @access private
84 * @category Helpers
85 * @author Rick Ellis
86 * @author Paul Burdick
87 * @link http://www.codeigniter.com/user_guide/helpers/
88 */
89class Auto_typography {
90
91 // Block level elements that should not be wrapped inside <p> tags
92 var $block_elements = 'div|blockquote|pre|code|h\d|script|ol|un';
93
94 // Elements that should not have <p> and <br /> tags within them.
95 var $skip_elements = 'pre|ol|ul';
96
97 // Tags we want the parser to completely ignore when splitting the string.
98 var $ignore_elements = 'a|b|i|em|strong|span|img|li';
99
100
101 /**
102 * Main Processing Function
103 *
104 */
105 function convert($str)
106 {
107 if ($str == '')
108 {
109 return '';
110 }
111
112 $str = ' '.$str.' ';
113
114 // Standardize Newlines to make matching easier
115 $str = preg_replace("/(\r\n|\r)/", "\n", $str);
116
117 /*
118 * Reduce line breaks
119 *
120 * If there are more than two consecutive line
121 * breaks we'll compress them down to a maximum
122 * of two since there's no benefit to more.
123 *
124 */
125 $str = preg_replace("/\n\n+/", "\n\n", $str);
126
127 /*
128 * Convert quotes within tags to tempoarary marker
129 *
130 * We don't want quotes converted within
131 * tags so we'll temporarily convert them to
132 * {{{DQ}}} and {{{SQ}}}
133 *
134 */
135 if (preg_match_all("#\<.+?>#si", $str, $matches))
136 {
137 for ($i = 0; $i < count($matches['0']); $i++)
138 {
139 $str = str_replace($matches['0'][$i],
140 str_replace(array("'",'"'), array('{{{SQ}}}', '{{{DQ}}}'), $matches['0'][$i]),
141 $str);
142 }
143 }
144
145 /*
146 * Convert "ignore" tags to tempoarary marker
147 *
148 * The parser splits out the string at every tag
149 * it encounters. Certain inline tags, like image
150 * tags, links, span tags, etc. will be adversely
151 * affected if they are split out so we'll convert
152 * the opening < temporarily to: {{{tag}}}
153 *
154 */
155 $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{{{tag}}}\\1\\2", $str);
156
157 /*
158 * Split the string at every tag
159 *
160 * This creates an array with this prototype:
161 *
162 * [array]
163 * {
164 * [0] = <opening tag>
165 * [1] = Content contained between the tags
166 * [2] = <closing tag>
167 * Etc...
168 * }
169 *
170 */
171 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
172
173 /*
174 * Build our finalized string
175 *
176 * We'll cycle through the array, skipping tags,
177 * and processing the contained text
178 *
179 */
180 $str = '';
181 $process = TRUE;
182 foreach ($chunks as $chunk)
183 {
184 /*
185 * Are we dealing with a tag?
186 *
187 * If so, we'll skip the processing for this cycle.
188 * Well also set the "process" flag which allows us
189 * to skip <pre> tags and a few other things.
190 *
191 */
192 if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
193 {
194 if (preg_match("#".$this->skip_elements."#", $match['2']))
195 {
196 $process = ($match['1'] == '/') ? TRUE : FALSE;
197 }
198
199 $str .= $chunk;
200 continue;
201 }
202
203 if ($process == FALSE)
204 {
205 $str .= $chunk;
206 continue;
207 }
208
209 // Convert Newlines into <p> and <br /> tags
210 $str .= $this->format_newlines($chunk);
211 }
212
213 // Convert Quotes and other characters
214 $str = $this->format_characters($str);
215
216 // We'll swap our temporary markers back and do some clean up.
217 $str = preg_replace('#(<p>\n*</p>)#', '', $str);
218 $str = preg_replace('#(<p.*?>)<p>#', "\\1", $str);
219
220 $str = str_replace(
221 array('</p></p>', '</p><p>', '{{{tag}}}', '{{{DQ}}}', '{{{SQ}}}'),
222 array('</p>', '<p>', '<', '"', "'"),
223 $str
224 );
225
226 return trim($str);
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Format Characters
233 *
234 * This function mainly converts double and single quotes
235 * to entities, but since these are directional, it does
236 * it based on some rules. It also converts em-dashes
237 * and a couple other things.
238 */
239 function format_characters($str)
240 {
241 $table = array(
242 ' "' => " &#8220;",
243 '" ' => "&#8221; ",
244 " '" => " &#8216;",
245 "' " => "&#8217; ",
246
247 '>"' => ">&#8220;",
248 '"<' => "&#8221;<",
249 ">'" => ">&#8216;",
250 "'<" => "&#8217;<",
251
252 "\"." => "&#8221;.",
253 "\"," => "&#8221;,",
254 "\";" => "&#8221;;",
255 "\":" => "&#8221;:",
256 "\"!" => "&#8221;!",
257 "\"?" => "&#8221;?",
258
259 ". " => ".&nbsp; ",
260 "? " => "?&nbsp; ",
261 "! " => "!&nbsp; ",
262 ": " => ":&nbsp; ",
263 );
264
265 // These deal with quotes within quotes, like: "'hi here'"
266 $start = 0;
267 $space = array("\n", "\t", " ");
268
269 while(TRUE)
270 {
271 $current = strpos(substr($str, $start), "\"'");
272
273 if ($current === FALSE) break;
274
275 $one_before = substr($str, $start+$current-1, 1);
276 $one_after = substr($str, $start+$current+2, 1);
277
adminee54c112006-09-28 17:13:38 +0000278 if ( ! in_array($one_after, $space, TRUE) && $one_after != "<")
adminb0dd10f2006-08-25 17:25:49 +0000279 {
280 $str = str_replace( $one_before."\"'".$one_after,
281 $one_before."&#8220;&#8216;".$one_after,
282 $str);
283 }
adminee54c112006-09-28 17:13:38 +0000284 elseif ( ! in_array($one_before, $space, TRUE) && (in_array($one_after, $space, TRUE) OR $one_after == '<'))
adminb0dd10f2006-08-25 17:25:49 +0000285 {
286 $str = str_replace( $one_before."\"'".$one_after,
287 $one_before."&#8221;&#8217;".$one_after,
288 $str);
289 }
290
291 $start = $start+$current+2;
292 }
293
294 $start = 0;
295
296 while(TRUE)
297 {
298 $current = strpos(substr($str, $start), "'\"");
299
300 if ($current === FALSE) break;
301
302 $one_before = substr($str, $start+$current-1, 1);
303 $one_after = substr($str, $start+$current+2, 1);
304
adminee54c112006-09-28 17:13:38 +0000305 if ( in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE) && $one_after != "<")
adminb0dd10f2006-08-25 17:25:49 +0000306 {
307 $str = str_replace( $one_before."'\"".$one_after,
308 $one_before."&#8216;&#8220;".$one_after,
309 $str);
310 }
adminee54c112006-09-28 17:13:38 +0000311 elseif ( ! in_array($one_before, $space, TRUE) && $one_before != ">")
adminb0dd10f2006-08-25 17:25:49 +0000312 {
313 $str = str_replace( $one_before."'\"".$one_after,
314 $one_before."&#8217;&#8221;".$one_after,
315 $str);
316 }
317
318 $start = $start+$current+2;
319 }
320
321 // Are there quotes within a word, as in: ("something")
322 if (preg_match_all("/(.)\"(\S+?)\"(.)/", $str, $matches))
323 {
324 for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i)
325 {
adminee54c112006-09-28 17:13:38 +0000326 if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000327 {
328 $str = str_replace( $matches['0'][$i],
329 $matches['1'][$i]."&#8220;".$matches['2'][$i]."&#8221;".$matches['3'][$i],
330 $str);
331 }
332 }
333 }
334
335 if (preg_match_all("/(.)\'(\S+?)\'(.)/", $str, $matches))
336 {
337 for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i)
338 {
adminee54c112006-09-28 17:13:38 +0000339 if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000340 {
341 $str = str_replace( $matches['0'][$i],
342 $matches['1'][$i]."&#8216;".$matches['2'][$i]."&#8217;".$matches['3'][$i],
343 $str);
344 }
345 }
346 }
347
348 // How about one apostrophe, as in Rick's
349 $start = 0;
350
351 while(TRUE)
352 {
353 $current = strpos(substr($str, $start), "'");
354
355 if ($current === FALSE) break;
356
357 $one_before = substr($str, $start+$current-1, 1);
358 $one_after = substr($str, $start+$current+1, 1);
359
adminee54c112006-09-28 17:13:38 +0000360 if ( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000361 {
362 $str = str_replace( $one_before."'".$one_after,
363 $one_before."&#8217;".$one_after,
364 $str);
365 }
366
367 $start = $start+$current+2;
368 }
369
370 // Em-dashes
371 $start = 0;
372 while(TRUE)
373 {
374 $current = strpos(substr($str, $start), "--");
375
376 if ($current === FALSE) break;
377
378 $one_before = substr($str, $start+$current-1, 1);
379 $one_after = substr($str, $start+$current+2, 1);
380 $two_before = substr($str, $start+$current-2, 1);
381 $two_after = substr($str, $start+$current+3, 1);
382
adminee54c112006-09-28 17:13:38 +0000383 if (( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000384 OR
adminee54c112006-09-28 17:13:38 +0000385 ( ! in_array($two_before, $space, TRUE) && ! in_array($two_after, $space, TRUE) && $one_before == ' ' && $one_after == ' ')
adminb0dd10f2006-08-25 17:25:49 +0000386 )
387 {
388 $str = str_replace( $two_before.$one_before."--".$one_after.$two_after,
389 $two_before.trim($one_before)."&#8212;".trim($one_after).$two_after,
390 $str);
391 }
392
393 $start = $start+$current+2;
394 }
395
396 // Ellipsis
397 $str = preg_replace("#(\w)\.\.\.(\s|<br />|</p>)#", "\\1&#8230;\\2", $str);
398 $str = preg_replace("#(\s|<br />|</p>)\.\.\.(\w)#", "\\1&#8230;\\2", $str);
399
400 // Run the translation array we defined above
401 $str = str_replace(array_keys($table), array_values($table), $str);
402
403 // If there are any stray double quotes we'll catch them here
404
405 $start = 0;
406
407 while(TRUE)
408 {
409 $current = strpos(substr($str, $start), '"');
410
411 if ($current === FALSE) break;
412
413 $one_before = substr($str, $start+$current-1, 1);
414 $one_after = substr($str, $start+$current+1, 1);
415
adminee54c112006-09-28 17:13:38 +0000416 if ( ! in_array($one_after, $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000417 {
418 $str = str_replace( $one_before.'"'.$one_after,
419 $one_before."&#8220;".$one_after,
420 $str);
421 }
adminee54c112006-09-28 17:13:38 +0000422 elseif( ! in_array($one_before, $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000423 {
424 $str = str_replace( $one_before."'".$one_after,
425 $one_before."&#8221;".$one_after,
426 $str);
427 }
428
429 $start = $start+$current+2;
430 }
431
432 $start = 0;
433
434 while(TRUE)
435 {
436 $current = strpos(substr($str, $start), "'");
437
438 if ($current === FALSE) break;
439
440 $one_before = substr($str, $start+$current-1, 1);
441 $one_after = substr($str, $start+$current+1, 1);
442
adminee54c112006-09-28 17:13:38 +0000443 if ( ! in_array($one_after, $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000444 {
445 $str = str_replace( $one_before."'".$one_after,
446 $one_before."&#8216;".$one_after,
447 $str);
448 }
adminee54c112006-09-28 17:13:38 +0000449 elseif( ! in_array($one_before, $space, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000450 {
451 $str = str_replace( $one_before."'".$one_after,
452 $one_before."&#8217;".$one_after,
453 $str);
454 }
455
456 $start = $start+$current+2;
457 }
458
459 return $str;
460 }
461
462 // --------------------------------------------------------------------
463
464 /**
465 * Format Newlines
466 *
467 * Converts newline characters into either <p> tags or <br />
468 *
469 */
470 function format_newlines($str)
471 {
472 if ($str == '')
473 {
474 return $str;
475 }
476
477 if (strpos($str, "\n") === FALSE)
478 {
479 return '<p>'.$str.'</p>';
480 }
481
482 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
483 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
484
485 return '<p>'.$str.'</p>';
486 }
487}
488
489
490?>