blob: 5ab16c9248ad16bb16cb2058dc38859778ae0160 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev7dc8c442011-12-25 16:52:37 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev7dc8c442011-12-25 16:52:37 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Typography Class
42 *
Andrey Andreevd69082b2012-03-26 16:14:26 +030043 * @package CodeIgniter
44 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Gerry6f2b2642011-09-25 00:30:52 +080047 * @link http://codeigniter.com/user_guide/libraries/typography.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_Typography {
50
Timothy Warren0688ac92012-04-20 10:25:04 -040051 /**
52 * Block level elements that should not be wrapped inside <p> tags
Andrey Andreev56454792012-05-17 14:32:19 +030053 *
Timothy Warren0688ac92012-04-20 10:25:04 -040054 * @var string
55 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020056 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 +020057
Timothy Warren0688ac92012-04-20 10:25:04 -040058 /**
59 * Elements that should not have <p> and <br /> tags within them.
60 *
61 * @var string
62 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020063 public $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d';
Barry Mienydd671972010-10-04 16:33:58 +020064
Timothy Warren0688ac92012-04-20 10:25:04 -040065 /**
66 * Tags we want the parser to completely ignore when splitting the string.
Andrey Andreev56454792012-05-17 14:32:19 +030067 *
Timothy Warren0688ac92012-04-20 10:25:04 -040068 * @var string
69 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020070 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 +020071
Timothy Warren0688ac92012-04-20 10:25:04 -040072 /**
73 * array of block level elements that require inner content to be within another block level element
74 *
75 * @var array
76 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020077 public $inner_block_required = array('blockquote');
Barry Mienydd671972010-10-04 16:33:58 +020078
Timothy Warren0688ac92012-04-20 10:25:04 -040079 /**
80 * the last block element parsed
81 *
82 * @var string
83 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020084 public $last_block_element = '';
Barry Mienydd671972010-10-04 16:33:58 +020085
Timothy Warren0688ac92012-04-20 10:25:04 -040086 /**
87 * whether or not to protect quotes within { curly braces }
88 *
89 * @var bool
90 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020091 public $protect_braced_quotes = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000094 * Auto Typography
95 *
96 * This function converts text, making it typographically correct:
Barry Mienydd671972010-10-04 16:33:58 +020097 * - Converts double spaces into paragraphs.
98 * - Converts single line breaks into <br /> tags
99 * - Converts single and double quotes into correctly facing curly quote entities.
100 * - Converts three dots into ellipsis.
101 * - Converts double dashes into em-dashes.
Derek Jones4b9c6292011-07-01 17:40:48 -0500102 * - Converts two spaces into entities
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 * @param string
105 * @param bool whether to reduce more then two consecutive newlines to two
106 * @return string
107 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200108 public function auto_typography($str, $reduce_linebreaks = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100110 if ($str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 return '';
113 }
114
115 // Standardize Newlines to make matching easier
116 if (strpos($str, "\r") !== FALSE)
117 {
Barry Mienydd671972010-10-04 16:33:58 +0200118 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Jones4b9c6292011-07-01 17:40:48 -0500121 // Reduce line breaks. If there are more than two consecutive linebreaks
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // we'll compress them down to a maximum of two since there's no benefit to more.
123 if ($reduce_linebreaks === TRUE)
124 {
125 $str = preg_replace("/\n\n+/", "\n\n", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200126 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000127
Derek Jonesa633ec22008-12-11 14:31:33 +0000128 // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
129 $html_comments = array();
Andrey Andreevd69082b2012-03-26 16:14:26 +0300130 if (strpos($str, '<!--') !== FALSE && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches))
Derek Jonesa633ec22008-12-11 14:31:33 +0000131 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300132 for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
Derek Jonesa633ec22008-12-11 14:31:33 +0000133 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300134 $html_comments[] = $matches[0][$i];
135 $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
Derek Jonesa633ec22008-12-11 14:31:33 +0000136 }
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Jones4b9c6292011-07-01 17:40:48 -0500139 // 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 +0000140 // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
141 if (strpos($str, '<pre') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300143 $str = preg_replace_callback('#<pre.*?>.*?</pre>#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Jones7deecfb2008-12-11 15:38:01 +0000146 // Convert quotes within tags to temporary markers.
Andrey Andreevd69082b2012-03-26 16:14:26 +0300147 $str = preg_replace_callback('#<.+?>#si', array($this, '_protect_characters'), $str);
Derek Jones7deecfb2008-12-11 15:38:01 +0000148
149 // Do the same with braces if necessary
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 if ($this->protect_braced_quotes === TRUE)
151 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300152 $str = preg_replace_callback('#\{.+?\}#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Jones4b9c6292011-07-01 17:40:48 -0500155 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
156 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
Andrey Andreevd69082b2012-03-26 16:14:26 +0300158 $str = preg_replace('#<(/*)('.$this->inline_elements.')([ >])#i', '{@TAG}\\1\\2\\3', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200160 /* Split the string at every tag. This expression creates an array with this prototype:
161 *
162 * [array]
163 * {
164 * [0] = <opening tag>
165 * [1] = Content...
166 * [2] = <closing tag>
167 * Etc...
168 * }
169 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Jones4b9c6292011-07-01 17:40:48 -0500172 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $str = '';
174 $process = TRUE;
175 $paragraph = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200176
Ronald Beilsma64b01362011-12-28 12:59:45 +0100177 for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200178 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
180 // 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 +0300181 if (preg_match('#<(/*)('.$this->block_elements.').*?>#', $chunks[$i], $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300183 if (preg_match('#'.$this->skip_elements.'#', $match[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200185 $process = ($match[1] === '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Alex Bilbied261b1e2012-06-02 11:12:16 +0100188 if ($match[1] === '')
Derek Jonesd5738d92008-11-14 16:53:34 +0000189 {
190 $this->last_block_element = $match[2];
191 }
192
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200193 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 continue;
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200197 if ($process === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200199 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 continue;
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Jones4b9c6292011-07-01 17:40:48 -0500203 // Force a newline to make sure end tags get processed by _format_newlines()
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200204 if ($i === $c)
Derek Jonese4702122009-01-14 21:57:32 +0000205 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200206 $chunks[$i] .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Jones4b9c6292011-07-01 17:40:48 -0500209 // Convert Newlines into <p> and <br /> tags
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200210 $str .= $this->_format_newlines($chunks[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Andrey Andreevd69082b2012-03-26 16:14:26 +0300213 // No opening block level tag? Add it if needed.
214 if ( ! preg_match('/^\s*<(?:'.$this->block_elements.')/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300216 $str = preg_replace('/^(.*?)<('.$this->block_elements.')/i', '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Jones7deecfb2008-12-11 15:38:01 +0000219 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
220 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Jonesa633ec22008-12-11 14:31:33 +0000222 // restore HTML comments
223 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
224 {
Derek Jones4777fb82008-12-11 17:55:42 +0000225 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
226 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
227 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
228 $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 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // Final clean up
232 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // If the user submitted their own paragraph tags within the text
235 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500236 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 // Reduce multiple instances of opening/closing paragraph tags to a single one
239 '#(</p>)+#' => '</p>',
240 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // Clean up stray paragraph tags that appear before block level elements
243 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // Clean up stray non-breaking spaces preceeding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500246 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // Replace the temporary markers we added earlier
249 '/\{@TAG\}/' => '<',
250 '/\{@DQ\}/' => '"',
251 '/\{@SQ\}/' => "'",
252 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500253 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000254
Derek Jonesc2067542010-10-01 07:19:57 -0500255 // An unintended consequence of the _format_newlines function is that
256 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200257 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500258 // This forces a newline after such occurrences, which looks much nicer.
259 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Jonesc2067542010-10-01 07:19:57 -0500261 // Similarly, there might be cases where a closing </block> will follow
262 // a closing </p> tag, so we'll correct it by adding a newline in between
Andrey Andreevd69082b2012-03-26 16:14:26 +0300263 '#</p></#' => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 );
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // Do we need to reduce empty lines?
267 if ($reduce_linebreaks === TRUE)
268 {
269 $table['#<p>\n*</p>#'] = '';
270 }
271 else
272 {
273 // If we have empty paragraph tags we add a non-breaking space
274 // otherwise most browsers won't treat them as true paragraphs
275 $table['#<p></p>#'] = '<p>&nbsp;</p>';
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 return preg_replace(array_keys($table), $table, $str);
279
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * Format Characters
286 *
287 * This function mainly converts double and single quotes
288 * to curly entities, but it also converts em-dashes,
289 * double spaces, and ampersands
290 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @param string
292 * @return string
293 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200294 public function format_characters($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
296 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 if ( ! isset($table))
299 {
Barry Mienydd671972010-10-04 16:33:58 +0200300 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // nested smart quotes, opening and closing
302 // note that rules for grammar (English) allow only for two levels deep
303 // and that single quotes are _supposed_ to always be on the outside
304 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000305 // Note that in all cases, whitespace is the primary determining factor
306 // on which direction to curl, with non-word characters like punctuation
307 // being a secondary factor only after whitespace is addressed.
308 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000309 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000310 '/\'"(\W)/' => '&#8217;&#8221;$1',
311 '/(\W)\'"/' => '$1&#8216;&#8220;',
312 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000313 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000314 '/"\'(\W)/' => '&#8221;&#8217;$1',
315 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
317 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000318 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000319 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000320 '/\'(\W)/' => '&#8217;$1',
321 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
323 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000324 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000325 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000326 '/"(\W)/' => '&#8221;$1',
327 '/(\W)"/' => '$1&#8220;',
328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000330 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
332 // Em dash and ellipses dots
333 '/\s?\-\-\s?/' => '&#8212;',
334 '/(\w)\.{3}/' => '$1&#8230;',
335
336 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500337 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
339 // ampersands, if not a character entity
340 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000341 );
342 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000343
344 return preg_replace(array_keys($table), $table, $str);
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // --------------------------------------------------------------------
348
349 /**
350 * Format Newlines
351 *
352 * Converts newline characters into either <p> tags or <br />
353 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * @param string
355 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200357 protected function _format_newlines($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100359 if ($str === '' OR (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
361 return $str;
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // Convert two consecutive newlines to paragraphs
365 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // Convert single spaces to <br /> tags
Andrey Andreevd69082b2012-03-26 16:14:26 +0300368 $str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // Wrap the whole enchilada in enclosing paragraphs
Alex Bilbied261b1e2012-06-02 11:12:16 +0100371 if ($str !== "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
Derek Jonesc2067542010-10-01 07:19:57 -0500373 // We trim off the right-side new line so that the closing </p> tag
374 // will be positioned immediately following the string, matching
375 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500376 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378
379 // Remove empty paragraphs if they are on the first line, as this
380 // is a potential unintended consequence of the previous code
Andrey Andreevd69082b2012-03-26 16:14:26 +0300381 return preg_replace('/<p><\/p>(.*)/', '\\1', $str, 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000387 * Protect Characters
388 *
389 * Protects special characters from being formatted later
390 * 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 +0200391 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
392 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000393 *
Derek Jones7deecfb2008-12-11 15:38:01 +0000394 * @param array
395 * @return string
396 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200397 protected function _protect_characters($match)
Derek Jones7deecfb2008-12-11 15:38:01 +0000398 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500399 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000400 }
401
402 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Jones7deecfb2008-12-11 15:38:01 +0000404 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * Convert newlines to HTML line breaks except within PRE tags
406 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * @param string
408 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200409 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200410 public function nl2br_except_pre($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200412 $newstr = '';
413 for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200415 $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
416 if ($ct - 1 !== $i)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200418 $newstr .= 'pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 return $newstr;
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425}
Derek Allard2067d1a2008-11-13 22:59:24 +0000426
427/* End of file Typography.php */
Andrey Andreevd69082b2012-03-26 16:14:26 +0300428/* Location: ./system/libraries/Typography.php */