blob: e67138c1aaa3d6cee2a6a27c5c222796699461b2 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
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 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://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;
Barry Mienydd671972010-10-04 16:33:58 +0200175
Ronald Beilsma64b01362011-12-28 12:59:45 +0100176 for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200177 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
179 // 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 +0300180 if (preg_match('#<(/*)('.$this->block_elements.').*?>#', $chunks[$i], $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300182 if (preg_match('#'.$this->skip_elements.'#', $match[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200184 $process = ($match[1] === '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Alex Bilbied261b1e2012-06-02 11:12:16 +0100187 if ($match[1] === '')
Derek Jonesd5738d92008-11-14 16:53:34 +0000188 {
189 $this->last_block_element = $match[2];
190 }
191
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200192 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 continue;
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200196 if ($process === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200198 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 continue;
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Jones4b9c6292011-07-01 17:40:48 -0500202 // Force a newline to make sure end tags get processed by _format_newlines()
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200203 if ($i === $c)
Derek Jonese4702122009-01-14 21:57:32 +0000204 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200205 $chunks[$i] .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Jones4b9c6292011-07-01 17:40:48 -0500208 // Convert Newlines into <p> and <br /> tags
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200209 $str .= $this->_format_newlines($chunks[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Andrey Andreevd69082b2012-03-26 16:14:26 +0300212 // No opening block level tag? Add it if needed.
213 if ( ! preg_match('/^\s*<(?:'.$this->block_elements.')/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300215 $str = preg_replace('/^(.*?)<('.$this->block_elements.')/i', '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Jones7deecfb2008-12-11 15:38:01 +0000218 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
219 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Jonesa633ec22008-12-11 14:31:33 +0000221 // restore HTML comments
222 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
223 {
Derek Jones4777fb82008-12-11 17:55:42 +0000224 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
225 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
226 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
227 $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 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // Final clean up
231 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // If the user submitted their own paragraph tags within the text
234 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500235 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 // Reduce multiple instances of opening/closing paragraph tags to a single one
238 '#(</p>)+#' => '</p>',
239 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // Clean up stray paragraph tags that appear before block level elements
242 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000243
Andrey Andreev71d8f722017-01-17 12:01:00 +0200244 // Clean up stray non-breaking spaces preceding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500245 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // Replace the temporary markers we added earlier
248 '/\{@TAG\}/' => '<',
249 '/\{@DQ\}/' => '"',
250 '/\{@SQ\}/' => "'",
251 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500252 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
Derek Jonesc2067542010-10-01 07:19:57 -0500254 // An unintended consequence of the _format_newlines function is that
255 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200256 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500257 // This forces a newline after such occurrences, which looks much nicer.
258 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Jonesc2067542010-10-01 07:19:57 -0500260 // Similarly, there might be cases where a closing </block> will follow
261 // a closing </p> tag, so we'll correct it by adding a newline in between
Andrey Andreevd69082b2012-03-26 16:14:26 +0300262 '#</p></#' => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 );
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // Do we need to reduce empty lines?
266 if ($reduce_linebreaks === TRUE)
267 {
268 $table['#<p>\n*</p>#'] = '';
269 }
270 else
271 {
272 // If we have empty paragraph tags we add a non-breaking space
273 // otherwise most browsers won't treat them as true paragraphs
274 $table['#<p></p>#'] = '<p>&nbsp;</p>';
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 return preg_replace(array_keys($table), $table, $str);
278
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * Format Characters
285 *
286 * This function mainly converts double and single quotes
287 * to curly entities, but it also converts em-dashes,
288 * double spaces, and ampersands
289 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @param string
291 * @return string
292 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200293 public function format_characters($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 if ( ! isset($table))
298 {
Barry Mienydd671972010-10-04 16:33:58 +0200299 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // nested smart quotes, opening and closing
301 // note that rules for grammar (English) allow only for two levels deep
302 // and that single quotes are _supposed_ to always be on the outside
303 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000304 // Note that in all cases, whitespace is the primary determining factor
305 // on which direction to curl, with non-word characters like punctuation
306 // being a secondary factor only after whitespace is addressed.
307 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000308 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000309 '/\'"(\W)/' => '&#8217;&#8221;$1',
310 '/(\W)\'"/' => '$1&#8216;&#8220;',
311 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000312 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000313 '/"\'(\W)/' => '&#8221;&#8217;$1',
314 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000315
316 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000317 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000318 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000319 '/\'(\W)/' => '&#8217;$1',
320 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000321
322 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000323 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000324 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000325 '/"(\W)/' => '&#8221;$1',
326 '/(\W)"/' => '$1&#8220;',
327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000329 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000330
331 // Em dash and ellipses dots
332 '/\s?\-\-\s?/' => '&#8212;',
333 '/(\w)\.{3}/' => '$1&#8230;',
334
335 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500336 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000337
338 // ampersands, if not a character entity
339 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000340 );
341 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000342
343 return preg_replace(array_keys($table), $table, $str);
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Format Newlines
350 *
351 * Converts newline characters into either <p> tags or <br />
352 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * @param string
354 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200355 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200356 protected function _format_newlines($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100358 if ($str === '' OR (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 return $str;
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Convert two consecutive newlines to paragraphs
364 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // Convert single spaces to <br /> tags
Andrey Andreevd69082b2012-03-26 16:14:26 +0300367 $str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // Wrap the whole enchilada in enclosing paragraphs
Alex Bilbied261b1e2012-06-02 11:12:16 +0100370 if ($str !== "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Derek Jonesc2067542010-10-01 07:19:57 -0500372 // We trim off the right-side new line so that the closing </p> tag
373 // will be positioned immediately following the string, matching
374 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500375 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377
378 // Remove empty paragraphs if they are on the first line, as this
379 // is a potential unintended consequence of the previous code
Andrey Andreevd69082b2012-03-26 16:14:26 +0300380 return preg_replace('/<p><\/p>(.*)/', '\\1', $str, 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000386 * Protect Characters
387 *
388 * Protects special characters from being formatted later
389 * 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 +0200390 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
391 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000392 *
Derek Jones7deecfb2008-12-11 15:38:01 +0000393 * @param array
394 * @return string
395 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200396 protected function _protect_characters($match)
Derek Jones7deecfb2008-12-11 15:38:01 +0000397 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500398 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000399 }
400
401 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Jones7deecfb2008-12-11 15:38:01 +0000403 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * Convert newlines to HTML line breaks except within PRE tags
405 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @param string
407 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200408 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200409 public function nl2br_except_pre($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200411 $newstr = '';
412 for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200414 $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
415 if ($ct - 1 !== $i)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200417 $newstr .= 'pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 return $newstr;
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424}