blob: 21bbad038e29624f0026c247ff1ee7d317539b2f [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
39 // Block level elements that should not be wrapped inside <p> tags
Andrey Andreev7dc8c442011-12-25 16:52:37 +020040 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 +020041
Derek Allard2067d1a2008-11-13 22:59:24 +000042 // Elements that should not have <p> and <br /> tags within them.
Andrey Andreev7dc8c442011-12-25 16:52:37 +020043 public $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // Tags we want the parser to completely ignore when splitting the string.
Andrey Andreev7dc8c442011-12-25 16:52:37 +020046 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 +020047
Derek Jonesd5738d92008-11-14 16:53:34 +000048 // array of block level elements that require inner content to be within another block level element
Andrey Andreev7dc8c442011-12-25 16:52:37 +020049 public $inner_block_required = array('blockquote');
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Jonesd5738d92008-11-14 16:53:34 +000051 // the last block element parsed
Andrey Andreev7dc8c442011-12-25 16:52:37 +020052 public $last_block_element = '';
Barry Mienydd671972010-10-04 16:33:58 +020053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 // whether or not to protect quotes within { curly braces }
Andrey Andreev7dc8c442011-12-25 16:52:37 +020055 public $protect_braced_quotes = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020056
Derek Allard2067d1a2008-11-13 22:59:24 +000057 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000058 * Auto Typography
59 *
60 * This function converts text, making it typographically correct:
Barry Mienydd671972010-10-04 16:33:58 +020061 * - Converts double spaces into paragraphs.
62 * - Converts single line breaks into <br /> tags
63 * - Converts single and double quotes into correctly facing curly quote entities.
64 * - Converts three dots into ellipsis.
65 * - Converts double dashes into em-dashes.
Derek Jones4b9c6292011-07-01 17:40:48 -050066 * - Converts two spaces into entities
Derek Allard2067d1a2008-11-13 22:59:24 +000067 *
Derek Allard2067d1a2008-11-13 22:59:24 +000068 * @param string
69 * @param bool whether to reduce more then two consecutive newlines to two
70 * @return string
71 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020072 public function auto_typography($str, $reduce_linebreaks = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
74 if ($str == '')
75 {
76 return '';
77 }
78
79 // Standardize Newlines to make matching easier
80 if (strpos($str, "\r") !== FALSE)
81 {
Barry Mienydd671972010-10-04 16:33:58 +020082 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +000083 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Jones4b9c6292011-07-01 17:40:48 -050085 // Reduce line breaks. If there are more than two consecutive linebreaks
Derek Allard2067d1a2008-11-13 22:59:24 +000086 // we'll compress them down to a maximum of two since there's no benefit to more.
87 if ($reduce_linebreaks === TRUE)
88 {
89 $str = preg_replace("/\n\n+/", "\n\n", $str);
Barry Mienydd671972010-10-04 16:33:58 +020090 }
Derek Allard2067d1a2008-11-13 22:59:24 +000091
Derek Jonesa633ec22008-12-11 14:31:33 +000092 // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
93 $html_comments = array();
Andrey Andreevd69082b2012-03-26 16:14:26 +030094 if (strpos($str, '<!--') !== FALSE && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches))
Derek Jonesa633ec22008-12-11 14:31:33 +000095 {
Andrey Andreevd69082b2012-03-26 16:14:26 +030096 for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
Derek Jonesa633ec22008-12-11 14:31:33 +000097 {
Andrey Andreevd69082b2012-03-26 16:14:26 +030098 $html_comments[] = $matches[0][$i];
99 $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
Derek Jonesa633ec22008-12-11 14:31:33 +0000100 }
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Jones4b9c6292011-07-01 17:40:48 -0500103 // 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 +0000104 // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
105 if (strpos($str, '<pre') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300107 $str = preg_replace_callback('#<pre.*?>.*?</pre>#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Jones7deecfb2008-12-11 15:38:01 +0000110 // Convert quotes within tags to temporary markers.
Andrey Andreevd69082b2012-03-26 16:14:26 +0300111 $str = preg_replace_callback('#<.+?>#si', array($this, '_protect_characters'), $str);
Derek Jones7deecfb2008-12-11 15:38:01 +0000112
113 // Do the same with braces if necessary
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if ($this->protect_braced_quotes === TRUE)
115 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300116 $str = preg_replace_callback('#\{.+?\}#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Jones4b9c6292011-07-01 17:40:48 -0500119 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
120 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
Andrey Andreevd69082b2012-03-26 16:14:26 +0300122 $str = preg_replace('#<(/*)('.$this->inline_elements.')([ >])#i', '{@TAG}\\1\\2\\3', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200124 /* Split the string at every tag. This expression creates an array with this prototype:
125 *
126 * [array]
127 * {
128 * [0] = <opening tag>
129 * [1] = Content...
130 * [2] = <closing tag>
131 * Etc...
132 * }
133 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Jones4b9c6292011-07-01 17:40:48 -0500136 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 $str = '';
138 $process = TRUE;
139 $paragraph = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200140
Ronald Beilsma64b01362011-12-28 12:59:45 +0100141 for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200142 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
144 // 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 +0300145 if (preg_match('#<(/*)('.$this->block_elements.').*?>#', $chunks[$i], $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300147 if (preg_match('#'.$this->skip_elements.'#', $match[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200149 $process = ($match[1] === '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jonesd5738d92008-11-14 16:53:34 +0000152 if ($match[1] == '')
153 {
154 $this->last_block_element = $match[2];
155 }
156
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200157 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 continue;
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200161 if ($process === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
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
Derek Jones4b9c6292011-07-01 17:40:48 -0500167 // Force a newline to make sure end tags get processed by _format_newlines()
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200168 if ($i === $c)
Derek Jonese4702122009-01-14 21:57:32 +0000169 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200170 $chunks[$i] .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Jones4b9c6292011-07-01 17:40:48 -0500173 // Convert Newlines into <p> and <br /> tags
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200174 $str .= $this->_format_newlines($chunks[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Andrey Andreevd69082b2012-03-26 16:14:26 +0300177 // No opening block level tag? Add it if needed.
178 if ( ! preg_match('/^\s*<(?:'.$this->block_elements.')/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300180 $str = preg_replace('/^(.*?)<('.$this->block_elements.')/i', '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Jones7deecfb2008-12-11 15:38:01 +0000183 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
184 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Jonesa633ec22008-12-11 14:31:33 +0000186 // restore HTML comments
187 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
188 {
Derek Jones4777fb82008-12-11 17:55:42 +0000189 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
190 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
191 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
192 $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 +0000193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // Final clean up
196 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // If the user submitted their own paragraph tags within the text
199 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500200 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // Reduce multiple instances of opening/closing paragraph tags to a single one
203 '#(</p>)+#' => '</p>',
204 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // Clean up stray paragraph tags that appear before block level elements
207 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 // Clean up stray non-breaking spaces preceeding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500210 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // Replace the temporary markers we added earlier
213 '/\{@TAG\}/' => '<',
214 '/\{@DQ\}/' => '"',
215 '/\{@SQ\}/' => "'",
216 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500217 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Derek Jonesc2067542010-10-01 07:19:57 -0500219 // An unintended consequence of the _format_newlines function is that
220 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200221 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500222 // This forces a newline after such occurrences, which looks much nicer.
223 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Jonesc2067542010-10-01 07:19:57 -0500225 // Similarly, there might be cases where a closing </block> will follow
226 // a closing </p> tag, so we'll correct it by adding a newline in between
Andrey Andreevd69082b2012-03-26 16:14:26 +0300227 '#</p></#' => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 );
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // Do we need to reduce empty lines?
231 if ($reduce_linebreaks === TRUE)
232 {
233 $table['#<p>\n*</p>#'] = '';
234 }
235 else
236 {
237 // If we have empty paragraph tags we add a non-breaking space
238 // otherwise most browsers won't treat them as true paragraphs
239 $table['#<p></p>#'] = '<p>&nbsp;</p>';
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 return preg_replace(array_keys($table), $table, $str);
243
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * Format Characters
250 *
251 * This function mainly converts double and single quotes
252 * to curly entities, but it also converts em-dashes,
253 * double spaces, and ampersands
254 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @param string
256 * @return string
257 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200258 public function format_characters($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 if ( ! isset($table))
263 {
Barry Mienydd671972010-10-04 16:33:58 +0200264 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // nested smart quotes, opening and closing
266 // note that rules for grammar (English) allow only for two levels deep
267 // and that single quotes are _supposed_ to always be on the outside
268 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000269 // Note that in all cases, whitespace is the primary determining factor
270 // on which direction to curl, with non-word characters like punctuation
271 // being a secondary factor only after whitespace is addressed.
272 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000273 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000274 '/\'"(\W)/' => '&#8217;&#8221;$1',
275 '/(\W)\'"/' => '$1&#8216;&#8220;',
276 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000277 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000278 '/"\'(\W)/' => '&#8221;&#8217;$1',
279 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000280
281 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000282 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000283 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000284 '/\'(\W)/' => '&#8217;$1',
285 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000286
287 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000288 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000289 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000290 '/"(\W)/' => '&#8221;$1',
291 '/(\W)"/' => '$1&#8220;',
292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000294 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000295
296 // Em dash and ellipses dots
297 '/\s?\-\-\s?/' => '&#8212;',
298 '/(\w)\.{3}/' => '$1&#8230;',
299
300 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500301 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
303 // ampersands, if not a character entity
304 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000305 );
306 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000307
308 return preg_replace(array_keys($table), $table, $str);
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
312
313 /**
314 * Format Newlines
315 *
316 * Converts newline characters into either <p> tags or <br />
317 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @param string
319 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200321 protected function _format_newlines($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300323 if ($str == '' OR (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 return $str;
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // Convert two consecutive newlines to paragraphs
329 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // Convert single spaces to <br /> tags
Andrey Andreevd69082b2012-03-26 16:14:26 +0300332 $str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // Wrap the whole enchilada in enclosing paragraphs
335 if ($str != "\n")
336 {
Derek Jonesc2067542010-10-01 07:19:57 -0500337 // We trim off the right-side new line so that the closing </p> tag
338 // will be positioned immediately following the string, matching
339 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500340 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
342
343 // Remove empty paragraphs if they are on the first line, as this
344 // is a potential unintended consequence of the previous code
Andrey Andreevd69082b2012-03-26 16:14:26 +0300345 return preg_replace('/<p><\/p>(.*)/', '\\1', $str, 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000351 * Protect Characters
352 *
353 * Protects special characters from being formatted later
354 * 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 +0200355 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
356 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000357 *
Derek Jones7deecfb2008-12-11 15:38:01 +0000358 * @param array
359 * @return string
360 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200361 protected function _protect_characters($match)
Derek Jones7deecfb2008-12-11 15:38:01 +0000362 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500363 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000364 }
365
366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Jones7deecfb2008-12-11 15:38:01 +0000368 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * Convert newlines to HTML line breaks except within PRE tags
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param string
372 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200373 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200374 public function nl2br_except_pre($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200376 $newstr = '';
377 for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200379 $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
380 if ($ct - 1 !== $i)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200382 $newstr .= 'pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 return $newstr;
387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389}
Derek Allard2067d1a2008-11-13 22:59:24 +0000390
391/* End of file Typography.php */
Andrey Andreevd69082b2012-03-26 16:14:26 +0300392/* Location: ./system/libraries/Typography.php */