blob: 6aaa993ae400e86c3115eaccbc565b8dd599befa [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Typography Class
30 *
Andrey Andreevd69082b2012-03-26 16:14:26 +030031 * @package CodeIgniter
32 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Gerry6f2b2642011-09-25 00:30:52 +080035 * @link http://codeigniter.com/user_guide/libraries/typography.html
Derek Allard2067d1a2008-11-13 22:59:24 +000036 */
37class CI_Typography {
38
Timothy Warren0688ac92012-04-20 10:25:04 -040039 /**
40 * Block level elements that should not be wrapped inside <p> tags
Andrey Andreev56454792012-05-17 14:32:19 +030041 *
Timothy Warren0688ac92012-04-20 10:25:04 -040042 * @var string
43 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020044 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 +020045
Timothy Warren0688ac92012-04-20 10:25:04 -040046 /**
47 * Elements that should not have <p> and <br /> tags within them.
48 *
49 * @var string
50 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020051 public $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d';
Barry Mienydd671972010-10-04 16:33:58 +020052
Timothy Warren0688ac92012-04-20 10:25:04 -040053 /**
54 * Tags we want the parser to completely ignore when splitting the string.
Andrey Andreev56454792012-05-17 14:32:19 +030055 *
Timothy Warren0688ac92012-04-20 10:25:04 -040056 * @var string
57 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020058 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 +020059
Timothy Warren0688ac92012-04-20 10:25:04 -040060 /**
61 * array of block level elements that require inner content to be within another block level element
62 *
63 * @var array
64 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020065 public $inner_block_required = array('blockquote');
Barry Mienydd671972010-10-04 16:33:58 +020066
Timothy Warren0688ac92012-04-20 10:25:04 -040067 /**
68 * the last block element parsed
69 *
70 * @var string
71 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020072 public $last_block_element = '';
Barry Mienydd671972010-10-04 16:33:58 +020073
Timothy Warren0688ac92012-04-20 10:25:04 -040074 /**
75 * whether or not to protect quotes within { curly braces }
76 *
77 * @var bool
78 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020079 public $protect_braced_quotes = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000082 * Auto Typography
83 *
84 * This function converts text, making it typographically correct:
Barry Mienydd671972010-10-04 16:33:58 +020085 * - Converts double spaces into paragraphs.
86 * - Converts single line breaks into <br /> tags
87 * - Converts single and double quotes into correctly facing curly quote entities.
88 * - Converts three dots into ellipsis.
89 * - Converts double dashes into em-dashes.
Derek Jones4b9c6292011-07-01 17:40:48 -050090 * - Converts two spaces into entities
Derek Allard2067d1a2008-11-13 22:59:24 +000091 *
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * @param string
93 * @param bool whether to reduce more then two consecutive newlines to two
94 * @return string
95 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020096 public function auto_typography($str, $reduce_linebreaks = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 if ($str == '')
99 {
100 return '';
101 }
102
103 // Standardize Newlines to make matching easier
104 if (strpos($str, "\r") !== FALSE)
105 {
Barry Mienydd671972010-10-04 16:33:58 +0200106 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Jones4b9c6292011-07-01 17:40:48 -0500109 // Reduce line breaks. If there are more than two consecutive linebreaks
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // we'll compress them down to a maximum of two since there's no benefit to more.
111 if ($reduce_linebreaks === TRUE)
112 {
113 $str = preg_replace("/\n\n+/", "\n\n", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200114 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
Derek Jonesa633ec22008-12-11 14:31:33 +0000116 // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
117 $html_comments = array();
Andrey Andreevd69082b2012-03-26 16:14:26 +0300118 if (strpos($str, '<!--') !== FALSE && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches))
Derek Jonesa633ec22008-12-11 14:31:33 +0000119 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300120 for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
Derek Jonesa633ec22008-12-11 14:31:33 +0000121 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300122 $html_comments[] = $matches[0][$i];
123 $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
Derek Jonesa633ec22008-12-11 14:31:33 +0000124 }
125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Jones4b9c6292011-07-01 17:40:48 -0500127 // 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 +0000128 // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
129 if (strpos($str, '<pre') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300131 $str = preg_replace_callback('#<pre.*?>.*?</pre>#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Jones7deecfb2008-12-11 15:38:01 +0000134 // Convert quotes within tags to temporary markers.
Andrey Andreevd69082b2012-03-26 16:14:26 +0300135 $str = preg_replace_callback('#<.+?>#si', array($this, '_protect_characters'), $str);
Derek Jones7deecfb2008-12-11 15:38:01 +0000136
137 // Do the same with braces if necessary
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 if ($this->protect_braced_quotes === TRUE)
139 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300140 $str = preg_replace_callback('#\{.+?\}#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Jones4b9c6292011-07-01 17:40:48 -0500143 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
144 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
Andrey Andreevd69082b2012-03-26 16:14:26 +0300146 $str = preg_replace('#<(/*)('.$this->inline_elements.')([ >])#i', '{@TAG}\\1\\2\\3', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000147
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200148 /* Split the string at every tag. This expression creates an array with this prototype:
149 *
150 * [array]
151 * {
152 * [0] = <opening tag>
153 * [1] = Content...
154 * [2] = <closing tag>
155 * Etc...
156 * }
157 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Jones4b9c6292011-07-01 17:40:48 -0500160 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $str = '';
162 $process = TRUE;
163 $paragraph = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200164
Ronald Beilsma64b01362011-12-28 12:59:45 +0100165 for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200166 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
168 // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
Andrey Andreevd69082b2012-03-26 16:14:26 +0300169 if (preg_match('#<(/*)('.$this->block_elements.').*?>#', $chunks[$i], $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300171 if (preg_match('#'.$this->skip_elements.'#', $match[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200173 $process = ($match[1] === '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jonesd5738d92008-11-14 16:53:34 +0000176 if ($match[1] == '')
177 {
178 $this->last_block_element = $match[2];
179 }
180
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200181 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 continue;
183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200185 if ($process === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200187 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 continue;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Jones4b9c6292011-07-01 17:40:48 -0500191 // Force a newline to make sure end tags get processed by _format_newlines()
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200192 if ($i === $c)
Derek Jonese4702122009-01-14 21:57:32 +0000193 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200194 $chunks[$i] .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Jones4b9c6292011-07-01 17:40:48 -0500197 // Convert Newlines into <p> and <br /> tags
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200198 $str .= $this->_format_newlines($chunks[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Andrey Andreevd69082b2012-03-26 16:14:26 +0300201 // No opening block level tag? Add it if needed.
202 if ( ! preg_match('/^\s*<(?:'.$this->block_elements.')/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300204 $str = preg_replace('/^(.*?)<('.$this->block_elements.')/i', '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Jones7deecfb2008-12-11 15:38:01 +0000207 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
208 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Jonesa633ec22008-12-11 14:31:33 +0000210 // restore HTML comments
211 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
212 {
Derek Jones4777fb82008-12-11 17:55:42 +0000213 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
214 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
215 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
216 $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 +0000217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // Final clean up
220 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // If the user submitted their own paragraph tags within the text
223 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500224 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // Reduce multiple instances of opening/closing paragraph tags to a single one
227 '#(</p>)+#' => '</p>',
228 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // Clean up stray paragraph tags that appear before block level elements
231 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // Clean up stray non-breaking spaces preceeding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500234 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 // Replace the temporary markers we added earlier
237 '/\{@TAG\}/' => '<',
238 '/\{@DQ\}/' => '"',
239 '/\{@SQ\}/' => "'",
240 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500241 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000242
Derek Jonesc2067542010-10-01 07:19:57 -0500243 // An unintended consequence of the _format_newlines function is that
244 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200245 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500246 // This forces a newline after such occurrences, which looks much nicer.
247 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Jonesc2067542010-10-01 07:19:57 -0500249 // Similarly, there might be cases where a closing </block> will follow
250 // a closing </p> tag, so we'll correct it by adding a newline in between
Andrey Andreevd69082b2012-03-26 16:14:26 +0300251 '#</p></#' => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 );
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // Do we need to reduce empty lines?
255 if ($reduce_linebreaks === TRUE)
256 {
257 $table['#<p>\n*</p>#'] = '';
258 }
259 else
260 {
261 // If we have empty paragraph tags we add a non-breaking space
262 // otherwise most browsers won't treat them as true paragraphs
263 $table['#<p></p>#'] = '<p>&nbsp;</p>';
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 return preg_replace(array_keys($table), $table, $str);
267
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * Format Characters
274 *
275 * This function mainly converts double and single quotes
276 * to curly entities, but it also converts em-dashes,
277 * double spaces, and ampersands
278 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * @param string
280 * @return string
281 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200282 public function format_characters($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ( ! isset($table))
287 {
Barry Mienydd671972010-10-04 16:33:58 +0200288 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // nested smart quotes, opening and closing
290 // note that rules for grammar (English) allow only for two levels deep
291 // and that single quotes are _supposed_ to always be on the outside
292 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000293 // Note that in all cases, whitespace is the primary determining factor
294 // on which direction to curl, with non-word characters like punctuation
295 // being a secondary factor only after whitespace is addressed.
296 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000297 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000298 '/\'"(\W)/' => '&#8217;&#8221;$1',
299 '/(\W)\'"/' => '$1&#8216;&#8220;',
300 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000301 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000302 '/"\'(\W)/' => '&#8221;&#8217;$1',
303 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000304
305 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000306 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000307 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000308 '/\'(\W)/' => '&#8217;$1',
309 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000310
311 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000312 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000313 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000314 '/"(\W)/' => '&#8221;$1',
315 '/(\W)"/' => '$1&#8220;',
316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000318 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000319
320 // Em dash and ellipses dots
321 '/\s?\-\-\s?/' => '&#8212;',
322 '/(\w)\.{3}/' => '$1&#8230;',
323
324 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500325 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000326
327 // ampersands, if not a character entity
328 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000329 );
330 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
332 return preg_replace(array_keys($table), $table, $str);
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
336
337 /**
338 * Format Newlines
339 *
340 * Converts newline characters into either <p> tags or <br />
341 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @param string
343 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200344 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200345 protected function _format_newlines($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300347 if ($str == '' OR (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 return $str;
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // Convert two consecutive newlines to paragraphs
353 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Convert single spaces to <br /> tags
Andrey Andreevd69082b2012-03-26 16:14:26 +0300356 $str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // Wrap the whole enchilada in enclosing paragraphs
359 if ($str != "\n")
360 {
Derek Jonesc2067542010-10-01 07:19:57 -0500361 // We trim off the right-side new line so that the closing </p> tag
362 // will be positioned immediately following the string, matching
363 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500364 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
367 // Remove empty paragraphs if they are on the first line, as this
368 // is a potential unintended consequence of the previous code
Andrey Andreevd69082b2012-03-26 16:14:26 +0300369 return preg_replace('/<p><\/p>(.*)/', '\\1', $str, 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000375 * Protect Characters
376 *
377 * Protects special characters from being formatted later
378 * 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 +0200379 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
380 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000381 *
Derek Jones7deecfb2008-12-11 15:38:01 +0000382 * @param array
383 * @return string
384 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200385 protected function _protect_characters($match)
Derek Jones7deecfb2008-12-11 15:38:01 +0000386 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500387 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000388 }
389
390 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Jones7deecfb2008-12-11 15:38:01 +0000392 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * Convert newlines to HTML line breaks except within PRE tags
394 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @param string
396 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200397 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200398 public function nl2br_except_pre($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200400 $newstr = '';
401 for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200403 $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
404 if ($ct - 1 !== $i)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200406 $newstr .= 'pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return $newstr;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413}
Derek Allard2067d1a2008-11-13 22:59:24 +0000414
415/* End of file Typography.php */
Andrey Andreevd69082b2012-03-26 16:14:26 +0300416/* Location: ./system/libraries/Typography.php */