blob: d83bf519bc8950fb6034315fffffe894678210f2 [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 *
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Typography Class
31 *
Andrey Andreevd69082b2012-03-26 16:14:26 +030032 * @package CodeIgniter
33 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Gerry6f2b2642011-09-25 00:30:52 +080036 * @link http://codeigniter.com/user_guide/libraries/typography.html
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
38class CI_Typography {
39
Timothy Warren0688ac92012-04-20 10:25:04 -040040 /**
41 * Block level elements that should not be wrapped inside <p> tags
Andrey Andreev56454792012-05-17 14:32:19 +030042 *
Timothy Warren0688ac92012-04-20 10:25:04 -040043 * @var string
44 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020045 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 +020046
Timothy Warren0688ac92012-04-20 10:25:04 -040047 /**
48 * Elements that should not have <p> and <br /> tags within them.
49 *
50 * @var string
51 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020052 public $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d';
Barry Mienydd671972010-10-04 16:33:58 +020053
Timothy Warren0688ac92012-04-20 10:25:04 -040054 /**
55 * Tags we want the parser to completely ignore when splitting the string.
Andrey Andreev56454792012-05-17 14:32:19 +030056 *
Timothy Warren0688ac92012-04-20 10:25:04 -040057 * @var string
58 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020059 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 +020060
Timothy Warren0688ac92012-04-20 10:25:04 -040061 /**
62 * array of block level elements that require inner content to be within another block level element
63 *
64 * @var array
65 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020066 public $inner_block_required = array('blockquote');
Barry Mienydd671972010-10-04 16:33:58 +020067
Timothy Warren0688ac92012-04-20 10:25:04 -040068 /**
69 * the last block element parsed
70 *
71 * @var string
72 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020073 public $last_block_element = '';
Barry Mienydd671972010-10-04 16:33:58 +020074
Timothy Warren0688ac92012-04-20 10:25:04 -040075 /**
76 * whether or not to protect quotes within { curly braces }
77 *
78 * @var bool
79 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020080 public $protect_braced_quotes = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * Auto Typography
84 *
85 * This function converts text, making it typographically correct:
Barry Mienydd671972010-10-04 16:33:58 +020086 * - Converts double spaces into paragraphs.
87 * - Converts single line breaks into <br /> tags
88 * - Converts single and double quotes into correctly facing curly quote entities.
89 * - Converts three dots into ellipsis.
90 * - Converts double dashes into em-dashes.
Derek Jones4b9c6292011-07-01 17:40:48 -050091 * - Converts two spaces into entities
Derek Allard2067d1a2008-11-13 22:59:24 +000092 *
Derek Allard2067d1a2008-11-13 22:59:24 +000093 * @param string
94 * @param bool whether to reduce more then two consecutive newlines to two
95 * @return string
96 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +020097 public function auto_typography($str, $reduce_linebreaks = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Alex Bilbied261b1e2012-06-02 11:12:16 +010099 if ($str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 return '';
102 }
103
104 // Standardize Newlines to make matching easier
105 if (strpos($str, "\r") !== FALSE)
106 {
Barry Mienydd671972010-10-04 16:33:58 +0200107 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Jones4b9c6292011-07-01 17:40:48 -0500110 // Reduce line breaks. If there are more than two consecutive linebreaks
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // we'll compress them down to a maximum of two since there's no benefit to more.
112 if ($reduce_linebreaks === TRUE)
113 {
114 $str = preg_replace("/\n\n+/", "\n\n", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200115 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000116
Derek Jonesa633ec22008-12-11 14:31:33 +0000117 // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
118 $html_comments = array();
Andrey Andreevd69082b2012-03-26 16:14:26 +0300119 if (strpos($str, '<!--') !== FALSE && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches))
Derek Jonesa633ec22008-12-11 14:31:33 +0000120 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300121 for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
Derek Jonesa633ec22008-12-11 14:31:33 +0000122 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300123 $html_comments[] = $matches[0][$i];
124 $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
Derek Jonesa633ec22008-12-11 14:31:33 +0000125 }
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Jones4b9c6292011-07-01 17:40:48 -0500128 // 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 +0000129 // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
130 if (strpos($str, '<pre') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300132 $str = preg_replace_callback('#<pre.*?>.*?</pre>#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Jones7deecfb2008-12-11 15:38:01 +0000135 // Convert quotes within tags to temporary markers.
Andrey Andreevd69082b2012-03-26 16:14:26 +0300136 $str = preg_replace_callback('#<.+?>#si', array($this, '_protect_characters'), $str);
Derek Jones7deecfb2008-12-11 15:38:01 +0000137
138 // Do the same with braces if necessary
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 if ($this->protect_braced_quotes === TRUE)
140 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300141 $str = preg_replace_callback('#\{.+?\}#si', array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Jones4b9c6292011-07-01 17:40:48 -0500144 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
145 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
Andrey Andreevd69082b2012-03-26 16:14:26 +0300147 $str = preg_replace('#<(/*)('.$this->inline_elements.')([ >])#i', '{@TAG}\\1\\2\\3', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200149 /* Split the string at every tag. This expression creates an array with this prototype:
150 *
151 * [array]
152 * {
153 * [0] = <opening tag>
154 * [1] = Content...
155 * [2] = <closing tag>
156 * Etc...
157 * }
158 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Jones4b9c6292011-07-01 17:40:48 -0500161 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 $str = '';
163 $process = TRUE;
164 $paragraph = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200165
Ronald Beilsma64b01362011-12-28 12:59:45 +0100166 for ($i = 0, $c = count($chunks) - 1; $i <= $c; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200167 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
169 // 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 +0300170 if (preg_match('#<(/*)('.$this->block_elements.').*?>#', $chunks[$i], $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300172 if (preg_match('#'.$this->skip_elements.'#', $match[2]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200174 $process = ($match[1] === '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Alex Bilbied261b1e2012-06-02 11:12:16 +0100177 if ($match[1] === '')
Derek Jonesd5738d92008-11-14 16:53:34 +0000178 {
179 $this->last_block_element = $match[2];
180 }
181
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200182 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 continue;
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200186 if ($process === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200188 $str .= $chunks[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 continue;
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Jones4b9c6292011-07-01 17:40:48 -0500192 // Force a newline to make sure end tags get processed by _format_newlines()
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200193 if ($i === $c)
Derek Jonese4702122009-01-14 21:57:32 +0000194 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200195 $chunks[$i] .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Jones4b9c6292011-07-01 17:40:48 -0500198 // Convert Newlines into <p> and <br /> tags
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200199 $str .= $this->_format_newlines($chunks[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Andrey Andreevd69082b2012-03-26 16:14:26 +0300202 // No opening block level tag? Add it if needed.
203 if ( ! preg_match('/^\s*<(?:'.$this->block_elements.')/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreevd69082b2012-03-26 16:14:26 +0300205 $str = preg_replace('/^(.*?)<('.$this->block_elements.')/i', '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Jones7deecfb2008-12-11 15:38:01 +0000208 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
209 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Jonesa633ec22008-12-11 14:31:33 +0000211 // restore HTML comments
212 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
213 {
Derek Jones4777fb82008-12-11 17:55:42 +0000214 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
215 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
216 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
217 $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 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // Final clean up
221 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // If the user submitted their own paragraph tags within the text
224 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500225 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // Reduce multiple instances of opening/closing paragraph tags to a single one
228 '#(</p>)+#' => '</p>',
229 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // Clean up stray paragraph tags that appear before block level elements
232 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // Clean up stray non-breaking spaces preceeding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500235 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 // Replace the temporary markers we added earlier
238 '/\{@TAG\}/' => '<',
239 '/\{@DQ\}/' => '"',
240 '/\{@SQ\}/' => "'",
241 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500242 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000243
Derek Jonesc2067542010-10-01 07:19:57 -0500244 // An unintended consequence of the _format_newlines function is that
245 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200246 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500247 // This forces a newline after such occurrences, which looks much nicer.
248 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Jonesc2067542010-10-01 07:19:57 -0500250 // Similarly, there might be cases where a closing </block> will follow
251 // a closing </p> tag, so we'll correct it by adding a newline in between
Andrey Andreevd69082b2012-03-26 16:14:26 +0300252 '#</p></#' => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 );
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // Do we need to reduce empty lines?
256 if ($reduce_linebreaks === TRUE)
257 {
258 $table['#<p>\n*</p>#'] = '';
259 }
260 else
261 {
262 // If we have empty paragraph tags we add a non-breaking space
263 // otherwise most browsers won't treat them as true paragraphs
264 $table['#<p></p>#'] = '<p>&nbsp;</p>';
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 return preg_replace(array_keys($table), $table, $str);
268
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * Format Characters
275 *
276 * This function mainly converts double and single quotes
277 * to curly entities, but it also converts em-dashes,
278 * double spaces, and ampersands
279 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @param string
281 * @return string
282 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200283 public function format_characters($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
285 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if ( ! isset($table))
288 {
Barry Mienydd671972010-10-04 16:33:58 +0200289 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // nested smart quotes, opening and closing
291 // note that rules for grammar (English) allow only for two levels deep
292 // and that single quotes are _supposed_ to always be on the outside
293 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000294 // Note that in all cases, whitespace is the primary determining factor
295 // on which direction to curl, with non-word characters like punctuation
296 // being a secondary factor only after whitespace is addressed.
297 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000298 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000299 '/\'"(\W)/' => '&#8217;&#8221;$1',
300 '/(\W)\'"/' => '$1&#8216;&#8220;',
301 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000302 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000303 '/"\'(\W)/' => '&#8221;&#8217;$1',
304 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
306 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000307 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000308 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000309 '/\'(\W)/' => '&#8217;$1',
310 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
312 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000313 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000314 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000315 '/"(\W)/' => '&#8221;$1',
316 '/(\W)"/' => '$1&#8220;',
317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000319 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
321 // Em dash and ellipses dots
322 '/\s?\-\-\s?/' => '&#8212;',
323 '/(\w)\.{3}/' => '$1&#8230;',
324
325 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500326 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
328 // ampersands, if not a character entity
329 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000330 );
331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
333 return preg_replace(array_keys($table), $table, $str);
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
339 * Format Newlines
340 *
341 * Converts newline characters into either <p> tags or <br />
342 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @param string
344 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200345 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200346 protected function _format_newlines($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100348 if ($str === '' OR (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 return $str;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // Convert two consecutive newlines to paragraphs
354 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // Convert single spaces to <br /> tags
Andrey Andreevd69082b2012-03-26 16:14:26 +0300357 $str = preg_replace("/([^\n])(\n)([^\n])/", '\\1<br />\\2\\3', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // Wrap the whole enchilada in enclosing paragraphs
Alex Bilbied261b1e2012-06-02 11:12:16 +0100360 if ($str !== "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
Derek Jonesc2067542010-10-01 07:19:57 -0500362 // We trim off the right-side new line so that the closing </p> tag
363 // will be positioned immediately following the string, matching
364 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500365 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367
368 // Remove empty paragraphs if they are on the first line, as this
369 // is a potential unintended consequence of the previous code
Andrey Andreevd69082b2012-03-26 16:14:26 +0300370 return preg_replace('/<p><\/p>(.*)/', '\\1', $str, 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000376 * Protect Characters
377 *
378 * Protects special characters from being formatted later
379 * 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 +0200380 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
381 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000382 *
Derek Jones7deecfb2008-12-11 15:38:01 +0000383 * @param array
384 * @return string
385 */
Andrey Andreev43aa8e42011-12-26 16:53:18 +0200386 protected function _protect_characters($match)
Derek Jones7deecfb2008-12-11 15:38:01 +0000387 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500388 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000389 }
390
391 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Jones7deecfb2008-12-11 15:38:01 +0000393 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * Convert newlines to HTML line breaks except within PRE tags
395 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @param string
397 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200398 */
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200399 public function nl2br_except_pre($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200401 $newstr = '';
402 for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200404 $newstr .= (($i % 2) === 0) ? nl2br($ex[$i]) : $ex[$i];
405 if ($ct - 1 !== $i)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Andrey Andreev7dc8c442011-12-25 16:52:37 +0200407 $newstr .= 'pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 return $newstr;
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414}
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
416/* End of file Typography.php */
Andrey Andreevd69082b2012-03-26 16:14:26 +0300417/* Location: ./system/libraries/Typography.php */