blob: ac9486a6bbb7b551205442689fb0e0205e1c9dda [file] [log] [blame]
Andrey Andreev7dc8c442011-12-25 16:52:37 +02001<?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
Andrey Andreev7dc8c442011-12-25 16:52:37 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7dc8c442011-12-25 16:52:37 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 *
Andrey Andreev43aa8e42011-12-26 16:53:18 +020034 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @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
Andrey Andreev7dc8c442011-12-25 16:52:37 +020042 public $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.
Andrey Andreev7dc8c442011-12-25 16:52:37 +020045 public $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.
Andrey Andreev7dc8c442011-12-25 16:52:37 +020048 public $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
Andrey Andreev7dc8c442011-12-25 16:52:37 +020051 public $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
Andrey Andreev7dc8c442011-12-25 16:52:37 +020054 public $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 }
Andrey Andreev7dc8c442011-12-25 16:52:37 +020057 public $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 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020075 public function auto_typography($str, $reduce_linebreaks = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
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
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200130 /* Split the string at every tag. This expression creates an array with this prototype:
131 *
132 * [array]
133 * {
134 * [0] = <opening tag>
135 * [1] = Content...
136 * [2] = <closing tag>
137 * Etc...
138 * }
139 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Jones4b9c6292011-07-01 17:40:48 -0500142 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 $str = '';
144 $process = TRUE;
145 $paragraph = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200146
Ronald Beilsma64b01362011-12-28 12:59:45 +0100147 for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200148 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
150 // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200151 if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunks[$i], $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
153 if (preg_match("#".$this->skip_elements."#", $match[2]))
154 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200155 $process = ($match[1] === '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Jonesd5738d92008-11-14 16:53:34 +0000158 if ($match[1] == '')
159 {
160 $this->last_block_element = $match[2];
161 }
162
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200163 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 continue;
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200167 if ($process === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200169 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 continue;
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Jones4b9c6292011-07-01 17:40:48 -0500173 // Force a newline to make sure end tags get processed by _format_newlines()
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200174 if ($i === $c)
Derek Jonese4702122009-01-14 21:57:32 +0000175 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200176 $chunks[$i] .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Jones4b9c6292011-07-01 17:40:48 -0500179 // Convert Newlines into <p> and <br /> tags
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200180 $str .= $this->_format_newlines($chunks[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Jones4b9c6292011-07-01 17:40:48 -0500183 // No opening block level tag? Add it if needed.
Derek Jonese4702122009-01-14 21:57:32 +0000184 if ( ! preg_match("/^\s*<(?:".$this->block_elements.")/i", $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Derek Jonese4702122009-01-14 21:57:32 +0000186 $str = preg_replace("/^(.*?)<(".$this->block_elements.")/i", '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Jones7deecfb2008-12-11 15:38:01 +0000189 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
190 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Jonesa633ec22008-12-11 14:31:33 +0000192 // restore HTML comments
193 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
194 {
Derek Jones4777fb82008-12-11 17:55:42 +0000195 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
196 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
197 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
198 $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 +0000199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // Final clean up
202 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // If the user submitted their own paragraph tags within the text
205 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500206 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 // Reduce multiple instances of opening/closing paragraph tags to a single one
209 '#(</p>)+#' => '</p>',
210 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // Clean up stray paragraph tags that appear before block level elements
213 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 // Clean up stray non-breaking spaces preceeding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500216 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // Replace the temporary markers we added earlier
219 '/\{@TAG\}/' => '<',
220 '/\{@DQ\}/' => '"',
221 '/\{@SQ\}/' => "'",
222 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500223 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000224
Derek Jonesc2067542010-10-01 07:19:57 -0500225 // An unintended consequence of the _format_newlines function is that
226 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200227 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500228 // This forces a newline after such occurrences, which looks much nicer.
229 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Jonesc2067542010-10-01 07:19:57 -0500231 // Similarly, there might be cases where a closing </block> will follow
232 // a closing </p> tag, so we'll correct it by adding a newline in between
233 "#</p></#" => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 );
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 // Do we need to reduce empty lines?
237 if ($reduce_linebreaks === TRUE)
238 {
239 $table['#<p>\n*</p>#'] = '';
240 }
241 else
242 {
243 // If we have empty paragraph tags we add a non-breaking space
244 // otherwise most browsers won't treat them as true paragraphs
245 $table['#<p></p>#'] = '<p>&nbsp;</p>';
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 return preg_replace(array_keys($table), $table, $str);
249
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * Format Characters
256 *
257 * This function mainly converts double and single quotes
258 * to curly entities, but it also converts em-dashes,
259 * double spaces, and ampersands
260 *
261 * @access public
262 * @param string
263 * @return string
264 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200265 public function format_characters($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 if ( ! isset($table))
270 {
Barry Mienydd671972010-10-04 16:33:58 +0200271 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // nested smart quotes, opening and closing
273 // note that rules for grammar (English) allow only for two levels deep
274 // and that single quotes are _supposed_ to always be on the outside
275 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000276 // Note that in all cases, whitespace is the primary determining factor
277 // on which direction to curl, with non-word characters like punctuation
278 // being a secondary factor only after whitespace is addressed.
279 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000280 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000281 '/\'"(\W)/' => '&#8217;&#8221;$1',
282 '/(\W)\'"/' => '$1&#8216;&#8220;',
283 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000284 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000285 '/"\'(\W)/' => '&#8221;&#8217;$1',
286 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000287
288 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000289 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000290 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000291 '/\'(\W)/' => '&#8217;$1',
292 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000293
294 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000295 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000296 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000297 '/"(\W)/' => '&#8221;$1',
298 '/(\W)"/' => '$1&#8220;',
299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000301 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
303 // Em dash and ellipses dots
304 '/\s?\-\-\s?/' => '&#8212;',
305 '/(\w)\.{3}/' => '$1&#8230;',
306
307 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500308 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000309
310 // ampersands, if not a character entity
311 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000312 );
313 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000314
315 return preg_replace(array_keys($table), $table, $str);
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
319
320 /**
321 * Format Newlines
322 *
323 * Converts newline characters into either <p> tags or <br />
324 *
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200325 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @param string
327 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200328 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200329 protected function _format_newlines($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200331 if ($str == '' OR (strpos($str, "\n") === FALSE AND ! in_array($this->last_block_element, $this->inner_block_required)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 return $str;
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // Convert two consecutive newlines to paragraphs
337 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // Convert single spaces to <br /> tags
340 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // Wrap the whole enchilada in enclosing paragraphs
343 if ($str != "\n")
344 {
Derek Jonesc2067542010-10-01 07:19:57 -0500345 // We trim off the right-side new line so that the closing </p> tag
346 // will be positioned immediately following the string, matching
347 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500348 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350
351 // Remove empty paragraphs if they are on the first line, as this
352 // is a potential unintended consequence of the previous code
353 $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 return $str;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000361 * Protect Characters
362 *
363 * Protects special characters from being formatted later
364 * 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 +0200365 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
366 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000367 *
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200368 * @access protected
Derek Jones7deecfb2008-12-11 15:38:01 +0000369 * @param array
370 * @return string
371 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200372 protected function _protect_characters($match)
Derek Jones7deecfb2008-12-11 15:38:01 +0000373 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500374 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000375 }
376
377 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Jones7deecfb2008-12-11 15:38:01 +0000379 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * Convert newlines to HTML line breaks except within PRE tags
381 *
382 * @access public
383 * @param string
384 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200385 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200386 public function nl2br_except_pre($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200388 $newstr = '';
389 for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200391 $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
392 if ($ct - 1 !== $i)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200394 $newstr .= 'pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 return $newstr;
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401}
402// END Typography Class
403
404/* End of file Typography.php */
Gerry6f2b2642011-09-25 00:30:52 +0800405/* Location: ./system/libraries/Typography.php */