blob: cfb2b94f6b9c77c65f77a960bc4833587d0e599a [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Typography Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/typography_helper.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
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 */
Derek Jones0b59f272008-05-13 04:22:33 +000037if ( ! function_exists('nl2br_except_pre'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000038{
Derek Jones269b9422008-01-28 21:00:20 +000039 function nl2br_except_pre($str)
Derek Allardd2df9bc2007-04-15 17:41:17 +000040 {
Derek Jones269b9422008-01-28 21:00:20 +000041 $ex = explode("pre>",$str);
42 $ct = count($ex);
Derek Allardd2df9bc2007-04-15 17:41:17 +000043
Derek Jones269b9422008-01-28 21:00:20 +000044 $newstr = "";
45 for ($i = 0; $i < $ct; $i++)
46 {
47 if (($i % 2) == 0)
48 {
49 $newstr .= nl2br($ex[$i]);
50 }
51 else
52 {
53 $newstr .= $ex[$i];
54 }
55
56 if ($ct - 1 != $i)
57 $newstr .= "pre>";
58 }
59
60 return $newstr;
61 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000062}
63
64// ------------------------------------------------------------------------
65
66/**
67 * Auto Typography Wrapper Function
68 *
69 *
70 * @access public
71 * @param string
72 * @return string
73 */
Derek Jones0b59f272008-05-13 04:22:33 +000074if ( ! function_exists('auto_typography'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000075{
Derek Jones269b9422008-01-28 21:00:20 +000076 function auto_typography($str)
77 {
78 $TYPE = new Auto_typography();
79 return $TYPE->convert($str);
80 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000081}
82
83// ------------------------------------------------------------------------
84
85/**
86 * Auto Typography Class
87 *
88 *
89 * @access private
90 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000091 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000092 * @link http://codeigniter.com/user_guide/helpers/
Derek Allardd2df9bc2007-04-15 17:41:17 +000093 */
94class Auto_typography {
95
96 // Block level elements that should not be wrapped inside <p> tags
Derek Allardbab7ed92008-01-19 15:41:28 +000097 var $block_elements = 'div|blockquote|pre|code|h\d|script|ol|ul';
Derek Allardd2df9bc2007-04-15 17:41:17 +000098
99 // Elements that should not have <p> and <br /> tags within them.
100 var $skip_elements = 'pre|ol|ul';
101
102 // Tags we want the parser to completely ignore when splitting the string.
103 var $ignore_elements = 'a|b|i|em|strong|span|img|li';
104
105
106 /**
107 * Main Processing Function
108 *
109 */
110 function convert($str)
111 {
112 if ($str == '')
113 {
114 return '';
115 }
116
117 $str = ' '.$str.' ';
118
119 // Standardize Newlines to make matching easier
Derek Jones0b59f272008-05-13 04:22:33 +0000120 if (strpos($str, "\r") !== FALSE)
121 {
122 $str = str_replace(array("\r\n", "\r"), "\n", $str);
123 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000124
125 /*
126 * Reduce line breaks
127 *
128 * If there are more than two consecutive line
129 * breaks we'll compress them down to a maximum
130 * of two since there's no benefit to more.
131 *
132 */
133 $str = preg_replace("/\n\n+/", "\n\n", $str);
134
135 /*
136 * Convert quotes within tags to temporary marker
137 *
138 * We don't want quotes converted within
139 * tags so we'll temporarily convert them to
140 * {@DQ} and {@SQ}
141 *
142 */
143 if (preg_match_all("#\<.+?>#si", $str, $matches))
144 {
145 for ($i = 0; $i < count($matches['0']); $i++)
146 {
147 $str = str_replace($matches['0'][$i],
148 str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
149 $str);
150 }
151 }
152
153
154 /*
155 * Add closing/opening paragraph tags before/after "block" elements
156 *
157 * Since block elements (like <blockquotes>, <pre>, etc.) do not get
158 * wrapped in paragraph tags we will add a closing </p> tag just before
159 * each block element starts and an opening <p> tag right after the block element
160 * ends. Later on we'll do some further clean up.
161 *
162 */
Derek Jones8b251912008-01-17 00:34:37 +0000163 $str = preg_replace("#(<)(".$this->block_elements.")(.*?>)#", "</p>\\1\\2\\3", $str);
164 $str = preg_replace("#(</)(".$this->block_elements.")(.*?>)#", "\\1\\2\\3<p>", $str);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000165
166 /*
167 * Convert "ignore" tags to temporary marker
168 *
169 * The parser splits out the string at every tag
170 * it encounters. Certain inline tags, like image
171 * tags, links, span tags, etc. will be adversely
172 * affected if they are split out so we'll convert
173 * the opening < temporarily to: {@TAG}
174 *
175 */
176 $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str);
177
178 /*
179 * Split the string at every tag
180 *
181 * This creates an array with this prototype:
182 *
183 * [array]
184 * {
185 * [0] = <opening tag>
186 * [1] = Content contained between the tags
187 * [2] = <closing tag>
188 * Etc...
189 * }
190 *
191 */
192 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
193
194 /*
195 * Build our finalized string
196 *
197 * We'll cycle through the array, skipping tags,
198 * and processing the contained text
199 *
200 */
201 $str = '';
202 $process = TRUE;
203 foreach ($chunks as $chunk)
204 {
205 /*
206 * Are we dealing with a tag?
207 *
208 * If so, we'll skip the processing for this cycle.
209 * Well also set the "process" flag which allows us
210 * to skip <pre> tags and a few other things.
211 *
212 */
213 if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
214 {
215 if (preg_match("#".$this->skip_elements."#", $match['2']))
216 {
217 $process = ($match['1'] == '/') ? TRUE : FALSE;
218 }
219
220 $str .= $chunk;
221 continue;
222 }
223
224 if ($process == FALSE)
225 {
226 $str .= $chunk;
227 continue;
228 }
229
230 // Convert Newlines into <p> and <br /> tags
231 $str .= $this->format_newlines($chunk);
232 }
233
234 // FINAL CLEAN UP
235 // IMPORTANT: DO NOT ALTER THE ORDER OF THE ITEMS BELOW!
236
237 /*
238 * Clean up paragraph tags before/after "block" elements
239 *
240 * Earlier we added <p></p> tags before/after block level elements.
241 * Then, we added paragraph tags around double line breaks. This
242 * potentially created incorrectly formatted paragraphs so we'll
243 * clean it up here.
244 *
245 */
246 $str = preg_replace("#<p>({@TAG}.*?)(".$this->block_elements.")(.*?>)#", "\\1\\2\\3", $str);
247 $str = preg_replace("#({@TAG}/.*?)(".$this->block_elements.")(.*?>)</p>#", "\\1\\2\\3", $str);
248
249 // Convert Quotes and other characters
250 $str = $this->format_characters($str);
251
252 // Fix an artifact that happens during the paragraph replacement
253 $str = preg_replace('#(<p>\n*</p>)#', '', $str);
254
255 // If the user submitted their own paragraph tags with class data
256 // in them we will retain them instead of using our tags.
257 $str = preg_replace('#(<p.*?>)<p>#', "\\1", $str);
258
259 // Final clean up
260 $str = str_replace(
261 array(
262 '</p></p>',
263 '</p><p>',
264 '<p> ',
265 ' </p>',
266 '{@TAG}',
267 '{@DQ}',
268 '{@SQ}',
269 '<p></p>'
270 ),
271 array(
272 '</p>',
273 '<p>',
274 '<p>',
275 '</p>',
276 '<',
277 '"',
278 "'",
279 ''
280 ),
281 $str
282 );
283
284 return $str;
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Format Characters
291 *
292 * This function mainly converts double and single quotes
293 * to entities, but since these are directional, it does
294 * it based on some rules. It also converts em-dashes
295 * and a couple other things.
296 */
297 function format_characters($str)
298 {
299 $table = array(
300 ' "' => " &#8220;",
301 '" ' => "&#8221; ",
302 " '" => " &#8216;",
303 "' " => "&#8217; ",
304
305 '>"' => ">&#8220;",
306 '"<' => "&#8221;<",
307 ">'" => ">&#8216;",
308 "'<" => "&#8217;<",
309
310 "\"." => "&#8221;.",
311 "\"," => "&#8221;,",
312 "\";" => "&#8221;;",
313 "\":" => "&#8221;:",
314 "\"!" => "&#8221;!",
315 "\"?" => "&#8221;?",
316
317 ". " => ".&nbsp; ",
318 "? " => "?&nbsp; ",
319 "! " => "!&nbsp; ",
320 ": " => ":&nbsp; ",
321 );
322
323 // These deal with quotes within quotes, like: "'hi here'"
324 $start = 0;
325 $space = array("\n", "\t", " ");
326
327 while(TRUE)
328 {
329 $current = strpos(substr($str, $start), "\"'");
330
331 if ($current === FALSE) break;
332
333 $one_before = substr($str, $start+$current-1, 1);
334 $one_after = substr($str, $start+$current+2, 1);
335
Derek Jones0b59f272008-05-13 04:22:33 +0000336 if ( ! in_array($one_after, $space, TRUE) && $one_after != "<")
Derek Allardd2df9bc2007-04-15 17:41:17 +0000337 {
338 $str = str_replace( $one_before."\"'".$one_after,
339 $one_before."&#8220;&#8216;".$one_after,
340 $str);
341 }
Derek Jones0b59f272008-05-13 04:22:33 +0000342 elseif ( ! in_array($one_before, $space, TRUE) && (in_array($one_after, $space, TRUE) OR $one_after == '<'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000343 {
344 $str = str_replace( $one_before."\"'".$one_after,
345 $one_before."&#8221;&#8217;".$one_after,
346 $str);
347 }
348
349 $start = $start+$current+2;
350 }
351
352 $start = 0;
353
354 while(TRUE)
355 {
356 $current = strpos(substr($str, $start), "'\"");
357
358 if ($current === FALSE) break;
359
360 $one_before = substr($str, $start+$current-1, 1);
361 $one_after = substr($str, $start+$current+2, 1);
362
363 if ( in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE) && $one_after != "<")
364 {
365 $str = str_replace( $one_before."'\"".$one_after,
366 $one_before."&#8216;&#8220;".$one_after,
367 $str);
368 }
Derek Jones0b59f272008-05-13 04:22:33 +0000369 elseif ( ! in_array($one_before, $space, TRUE) && $one_before != ">")
Derek Allardd2df9bc2007-04-15 17:41:17 +0000370 {
371 $str = str_replace( $one_before."'\"".$one_after,
372 $one_before."&#8217;&#8221;".$one_after,
373 $str);
374 }
375
376 $start = $start+$current+2;
377 }
378
379 // Are there quotes within a word, as in: ("something")
380 if (preg_match_all("/(.)\"(\S+?)\"(.)/", $str, $matches))
381 {
382 for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i)
383 {
Derek Jones0b59f272008-05-13 04:22:33 +0000384 if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000385 {
386 $str = str_replace( $matches['0'][$i],
387 $matches['1'][$i]."&#8220;".$matches['2'][$i]."&#8221;".$matches['3'][$i],
388 $str);
389 }
390 }
391 }
392
393 if (preg_match_all("/(.)\'(\S+?)\'(.)/", $str, $matches))
394 {
395 for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i)
396 {
Derek Jones0b59f272008-05-13 04:22:33 +0000397 if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000398 {
399 $str = str_replace( $matches['0'][$i],
400 $matches['1'][$i]."&#8216;".$matches['2'][$i]."&#8217;".$matches['3'][$i],
401 $str);
402 }
403 }
404 }
405
406 // How about one apostrophe, as in Rick's
407 $start = 0;
408
409 while(TRUE)
410 {
411 $current = strpos(substr($str, $start), "'");
412
413 if ($current === FALSE) break;
414
415 $one_before = substr($str, $start+$current-1, 1);
416 $one_after = substr($str, $start+$current+1, 1);
417
Derek Jones0b59f272008-05-13 04:22:33 +0000418 if ( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000419 {
420 $str = str_replace( $one_before."'".$one_after,
421 $one_before."&#8217;".$one_after,
422 $str);
423 }
424
425 $start = $start+$current+2;
426 }
427
428 // Em-dashes
429 $start = 0;
430 while(TRUE)
431 {
432 $current = strpos(substr($str, $start), "--");
433
434 if ($current === FALSE) break;
435
436 $one_before = substr($str, $start+$current-1, 1);
437 $one_after = substr($str, $start+$current+2, 1);
438 $two_before = substr($str, $start+$current-2, 1);
439 $two_after = substr($str, $start+$current+3, 1);
440
Derek Jones0b59f272008-05-13 04:22:33 +0000441 if (( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000442 OR
Derek Jones0b59f272008-05-13 04:22:33 +0000443 ( ! in_array($two_before, $space, TRUE) && ! in_array($two_after, $space, TRUE) && $one_before == ' ' && $one_after == ' ')
Derek Allardd2df9bc2007-04-15 17:41:17 +0000444 )
445 {
446 $str = str_replace( $two_before.$one_before."--".$one_after.$two_after,
447 $two_before.trim($one_before)."&#8212;".trim($one_after).$two_after,
448 $str);
449 }
450
451 $start = $start+$current+2;
452 }
453
454 // Ellipsis
455 $str = preg_replace("#(\w)\.\.\.(\s|<br />|</p>)#", "\\1&#8230;\\2", $str);
456 $str = preg_replace("#(\s|<br />|</p>)\.\.\.(\w)#", "\\1&#8230;\\2", $str);
457
458 // Run the translation array we defined above
459 $str = str_replace(array_keys($table), array_values($table), $str);
460
461 // If there are any stray double quotes we'll catch them here
462
463 $start = 0;
464
465 while(TRUE)
466 {
467 $current = strpos(substr($str, $start), '"');
468
469 if ($current === FALSE) break;
470
471 $one_before = substr($str, $start+$current-1, 1);
472 $one_after = substr($str, $start+$current+1, 1);
473
Derek Jones0b59f272008-05-13 04:22:33 +0000474 if ( ! in_array($one_after, $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000475 {
476 $str = str_replace( $one_before.'"'.$one_after,
477 $one_before."&#8220;".$one_after,
478 $str);
479 }
Derek Jones0b59f272008-05-13 04:22:33 +0000480 elseif( ! in_array($one_before, $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000481 {
482 $str = str_replace( $one_before."'".$one_after,
483 $one_before."&#8221;".$one_after,
484 $str);
485 }
486
487 $start = $start+$current+2;
488 }
489
490 $start = 0;
491
492 while(TRUE)
493 {
494 $current = strpos(substr($str, $start), "'");
495
496 if ($current === FALSE) break;
497
498 $one_before = substr($str, $start+$current-1, 1);
499 $one_after = substr($str, $start+$current+1, 1);
500
Derek Jones0b59f272008-05-13 04:22:33 +0000501 if ( ! in_array($one_after, $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000502 {
503 $str = str_replace( $one_before."'".$one_after,
504 $one_before."&#8216;".$one_after,
505 $str);
506 }
Derek Jones0b59f272008-05-13 04:22:33 +0000507 elseif( ! in_array($one_before, $space, TRUE))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000508 {
509 $str = str_replace( $one_before."'".$one_after,
510 $one_before."&#8217;".$one_after,
511 $str);
512 }
513
514 $start = $start+$current+2;
515 }
516
517 return $str;
518 }
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Format Newlines
524 *
525 * Converts newline characters into either <p> tags or <br />
526 *
527 */
528 function format_newlines($str)
529 {
530 if ($str == '')
531 {
532 return $str;
533 }
534
535 if (strpos($str, "\n") === FALSE)
536 {
537 return '<p>'.$str.'</p>';
538 }
539
540 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
541 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
542
543 return '<p>'.$str.'</p>';
544 }
545}
546
547
Derek Jones0b59f272008-05-13 04:22:33 +0000548
549/* End of file typography_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000550/* Location: ./system/helpers/typography_helper.php */