blob: af6ca2bf22addd5cdf4306bcecd5dec977935fcc [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Typography Class
32 *
33 *
34 * @access private
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Gerry6f2b2642011-09-25 00:30:52 +080037 * @link http://codeigniter.com/user_guide/libraries/typography.html
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39class CI_Typography {
40
41 // Block level elements that should not be wrapped inside <p> tags
42 var $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
Barry Mienydd671972010-10-04 16:33:58 +020043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 // Elements that should not have <p> and <br /> tags within them.
Robin Sowell8db2d5e2009-07-17 22:07:04 +000045 var $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d';
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 // Tags we want the parser to completely ignore when splitting the string.
48 var $inline_elements = 'a|abbr|acronym|b|bdo|big|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|q|samp|select|small|span|strong|sub|sup|textarea|tt|var';
Barry Mienydd671972010-10-04 16:33:58 +020049
Derek Jonesd5738d92008-11-14 16:53:34 +000050 // array of block level elements that require inner content to be within another block level element
51 var $inner_block_required = array('blockquote');
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Jonesd5738d92008-11-14 16:53:34 +000053 // the last block element parsed
54 var $last_block_element = '';
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 // whether or not to protect quotes within { curly braces }
57 var $protect_braced_quotes = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000060 * Auto Typography
61 *
62 * This function converts text, making it typographically correct:
Barry Mienydd671972010-10-04 16:33:58 +020063 * - Converts double spaces into paragraphs.
64 * - Converts single line breaks into <br /> tags
65 * - Converts single and double quotes into correctly facing curly quote entities.
66 * - Converts three dots into ellipsis.
67 * - Converts double dashes into em-dashes.
Derek Jones4b9c6292011-07-01 17:40:48 -050068 * - Converts two spaces into entities
Derek Allard2067d1a2008-11-13 22:59:24 +000069 *
70 * @access public
71 * @param string
72 * @param bool whether to reduce more then two consecutive newlines to two
73 * @return string
74 */
75 function auto_typography($str, $reduce_linebreaks = FALSE)
76 {
77 if ($str == '')
78 {
79 return '';
80 }
81
82 // Standardize Newlines to make matching easier
83 if (strpos($str, "\r") !== FALSE)
84 {
Barry Mienydd671972010-10-04 16:33:58 +020085 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Jones4b9c6292011-07-01 17:40:48 -050088 // Reduce line breaks. If there are more than two consecutive linebreaks
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // we'll compress them down to a maximum of two since there's no benefit to more.
90 if ($reduce_linebreaks === TRUE)
91 {
92 $str = preg_replace("/\n\n+/", "\n\n", $str);
Barry Mienydd671972010-10-04 16:33:58 +020093 }
Derek Allard2067d1a2008-11-13 22:59:24 +000094
Derek Jonesa633ec22008-12-11 14:31:33 +000095 // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
96 $html_comments = array();
97 if (strpos($str, '<!--') !== FALSE)
98 {
99 if (preg_match_all("#(<!\-\-.*?\-\->)#s", $str, $matches))
100 {
101 for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
102 {
103 $html_comments[] = $matches[0][$i];
104 $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
105 }
106 }
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Jones4b9c6292011-07-01 17:40:48 -0500109 // match and yank <pre> tags if they exist. It's cheaper to do this separately since most content will
Derek Jones7deecfb2008-12-11 15:38:01 +0000110 // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
111 if (strpos($str, '<pre') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Derek Jones7deecfb2008-12-11 15:38:01 +0000113 $str = preg_replace_callback("#<pre.*?>.*?</pre>#si", array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Jones7deecfb2008-12-11 15:38:01 +0000116 // Convert quotes within tags to temporary markers.
117 $str = preg_replace_callback("#<.+?>#si", array($this, '_protect_characters'), $str);
118
119 // Do the same with braces if necessary
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 if ($this->protect_braced_quotes === TRUE)
121 {
Barry Mienydd671972010-10-04 16:33:58 +0200122 $str = preg_replace_callback("#\{.+?\}#si", array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Jones4b9c6292011-07-01 17:40:48 -0500125 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
126 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
128 $str = preg_replace("#<(/*)(".$this->inline_elements.")([ >])#i", "{@TAG}\\1\\2\\3", $str);
129
Derek Jones4b9c6292011-07-01 17:40:48 -0500130 // Split the string at every tag. This expression creates an array with this prototype:
Barry Mienydd671972010-10-04 16:33:58 +0200131 //
132 // [array]
133 // {
134 // [0] = <opening tag>
135 // [1] = Content...
136 // [2] = <closing tag>
137 // Etc...
138 // }
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Jones4b9c6292011-07-01 17:40:48 -0500141 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 $str = '';
143 $process = TRUE;
144 $paragraph = FALSE;
Derek Jonese4702122009-01-14 21:57:32 +0000145 $current_chunk = 0;
146 $total_chunks = count($chunks);
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 foreach ($chunks as $chunk)
Barry Mienydd671972010-10-04 16:33:58 +0200149 {
Derek Jonese4702122009-01-14 21:57:32 +0000150 $current_chunk++;
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
153 // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
Derek Jones7deecfb2008-12-11 15:38:01 +0000154 if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunk, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 if (preg_match("#".$this->skip_elements."#", $match[2]))
157 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500158 $process = ($match[1] == '/') ? TRUE : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Jonesd5738d92008-11-14 16:53:34 +0000161 if ($match[1] == '')
162 {
163 $this->last_block_element = $match[2];
164 }
165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 $str .= $chunk;
167 continue;
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 if ($process == FALSE)
171 {
Derek Jones7deecfb2008-12-11 15:38:01 +0000172 $str .= $chunk;
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 continue;
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jones4b9c6292011-07-01 17:40:48 -0500176 // Force a newline to make sure end tags get processed by _format_newlines()
Derek Jonese4702122009-01-14 21:57:32 +0000177 if ($current_chunk == $total_chunks)
178 {
Barry Mienydd671972010-10-04 16:33:58 +0200179 $chunk .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Jones4b9c6292011-07-01 17:40:48 -0500182 // Convert Newlines into <p> and <br /> tags
Derek Jones7deecfb2008-12-11 15:38:01 +0000183 $str .= $this->_format_newlines($chunk);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Jones4b9c6292011-07-01 17:40:48 -0500186 // No opening block level tag? Add it if needed.
Derek Jonese4702122009-01-14 21:57:32 +0000187 if ( ! preg_match("/^\s*<(?:".$this->block_elements.")/i", $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
Derek Jonese4702122009-01-14 21:57:32 +0000189 $str = preg_replace("/^(.*?)<(".$this->block_elements.")/i", '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Jones7deecfb2008-12-11 15:38:01 +0000192 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
193 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Jonesa633ec22008-12-11 14:31:33 +0000195 // restore HTML comments
196 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
197 {
Derek Jones4777fb82008-12-11 17:55:42 +0000198 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
199 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
200 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
201 $str = preg_replace('#(?(?=<p>\{@HC'.$i.'\})<p>\{@HC'.$i.'\}(\s*</p>)|\{@HC'.$i.'\})#s', $html_comments[$i], $str);
Derek Jonesa633ec22008-12-11 14:31:33 +0000202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // Final clean up
205 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 // If the user submitted their own paragraph tags within the text
208 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500209 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // Reduce multiple instances of opening/closing paragraph tags to a single one
212 '#(</p>)+#' => '</p>',
213 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 // Clean up stray paragraph tags that appear before block level elements
216 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // Clean up stray non-breaking spaces preceeding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500219 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // Replace the temporary markers we added earlier
222 '/\{@TAG\}/' => '<',
223 '/\{@DQ\}/' => '"',
224 '/\{@SQ\}/' => "'",
225 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500226 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
Derek Jonesc2067542010-10-01 07:19:57 -0500228 // An unintended consequence of the _format_newlines function is that
229 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200230 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500231 // This forces a newline after such occurrences, which looks much nicer.
232 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Jonesc2067542010-10-01 07:19:57 -0500234 // Similarly, there might be cases where a closing </block> will follow
235 // a closing </p> tag, so we'll correct it by adding a newline in between
236 "#</p></#" => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 );
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 // Do we need to reduce empty lines?
240 if ($reduce_linebreaks === TRUE)
241 {
242 $table['#<p>\n*</p>#'] = '';
243 }
244 else
245 {
246 // If we have empty paragraph tags we add a non-breaking space
247 // otherwise most browsers won't treat them as true paragraphs
248 $table['#<p></p>#'] = '<p>&nbsp;</p>';
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 return preg_replace(array_keys($table), $table, $str);
252
253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * Format Characters
259 *
260 * This function mainly converts double and single quotes
261 * to curly entities, but it also converts em-dashes,
262 * double spaces, and ampersands
263 *
264 * @access public
265 * @param string
266 * @return string
267 */
268 function format_characters($str)
269 {
270 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if ( ! isset($table))
273 {
Barry Mienydd671972010-10-04 16:33:58 +0200274 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // nested smart quotes, opening and closing
276 // note that rules for grammar (English) allow only for two levels deep
277 // and that single quotes are _supposed_ to always be on the outside
278 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000279 // Note that in all cases, whitespace is the primary determining factor
280 // on which direction to curl, with non-word characters like punctuation
281 // being a secondary factor only after whitespace is addressed.
282 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000283 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000284 '/\'"(\W)/' => '&#8217;&#8221;$1',
285 '/(\W)\'"/' => '$1&#8216;&#8220;',
286 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000287 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000288 '/"\'(\W)/' => '&#8221;&#8217;$1',
289 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000290
291 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000292 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000293 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000294 '/\'(\W)/' => '&#8217;$1',
295 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000296
297 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000298 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000299 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000300 '/"(\W)/' => '&#8221;$1',
301 '/(\W)"/' => '$1&#8220;',
302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000304 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
306 // Em dash and ellipses dots
307 '/\s?\-\-\s?/' => '&#8212;',
308 '/(\w)\.{3}/' => '$1&#8230;',
309
310 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500311 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000312
313 // ampersands, if not a character entity
314 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000315 );
316 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000317
318 return preg_replace(array_keys($table), $table, $str);
319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // --------------------------------------------------------------------
322
323 /**
324 * Format Newlines
325 *
326 * Converts newline characters into either <p> tags or <br />
327 *
328 * @access public
329 * @param string
330 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200331 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 function _format_newlines($str)
333 {
334 if ($str == '')
335 {
336 return $str;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Jones4b9c6292011-07-01 17:40:48 -0500339 if (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required))
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 return $str;
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // Convert two consecutive newlines to paragraphs
345 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // Convert single spaces to <br /> tags
348 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // Wrap the whole enchilada in enclosing paragraphs
351 if ($str != "\n")
352 {
Derek Jonesc2067542010-10-01 07:19:57 -0500353 // We trim off the right-side new line so that the closing </p> tag
354 // will be positioned immediately following the string, matching
355 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500356 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358
359 // Remove empty paragraphs if they are on the first line, as this
360 // is a potential unintended consequence of the previous code
361 $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 return $str;
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000369 * Protect Characters
370 *
371 * Protects special characters from being formatted later
372 * We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ}
Barry Mienydd671972010-10-04 16:33:58 +0200373 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
374 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000375 *
376 * @access public
377 * @param array
378 * @return string
379 */
380 function _protect_characters($match)
381 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500382 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000383 }
384
385 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Jones7deecfb2008-12-11 15:38:01 +0000387 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * Convert newlines to HTML line breaks except within PRE tags
389 *
390 * @access public
391 * @param string
392 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200393 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 function nl2br_except_pre($str)
395 {
396 $ex = explode("pre>",$str);
397 $ct = count($ex);
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 $newstr = "";
400 for ($i = 0; $i < $ct; $i++)
401 {
402 if (($i % 2) == 0)
403 {
404 $newstr .= nl2br($ex[$i]);
405 }
406 else
407 {
408 $newstr .= $ex[$i];
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 if ($ct - 1 != $i)
412 $newstr .= "pre>";
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 return $newstr;
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418}
419// END Typography Class
420
421/* End of file Typography.php */
Gerry6f2b2642011-09-25 00:30:52 +0800422/* Location: ./system/libraries/Typography.php */