1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/
29 // Block level elements that should not be wrapped inside <p> tags
30 var $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
32 // Elements that should not have <p> and <br /> tags within them.
33 var $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d';
35 // Tags we want the parser to completely ignore when splitting the string.
36 var $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';
38 // array of block level elements that require inner content to be within another block level element
39 var $inner_block_required = array('blockquote');
41 // the last block element parsed
42 var $last_block_element = '';
44 // whether or not to protect quotes within { curly braces }
45 var $protect_braced_quotes = FALSE;
48 * Nothing to do here...
51 function CI_Typography()
58 * This function converts text, making it typographically correct:
59 * - Converts double spaces into paragraphs.
60 * - Converts single line breaks into <br /> tags
61 * - Converts single and double quotes into correctly facing curly quote entities.
62 * - Converts three dots into ellipsis.
63 * - Converts double dashes into em-dashes.
64 * - Converts two spaces into entities
68 * @param bool whether to reduce more then two consecutive newlines to two
71 function auto_typography($str, $reduce_linebreaks = FALSE)
78 // Standardize Newlines to make matching easier
79 if (strpos($str, "\r") !== FALSE)
81 $str = str_replace(array("\r\n", "\r"), "\n", $str);
84 // Reduce line breaks. If there are more than two consecutive linebreaks
85 // we'll compress them down to a maximum of two since there's no benefit to more.
86 if ($reduce_linebreaks === TRUE)
88 $str = preg_replace("/\n\n+/", "\n\n", $str);
91 // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
92 $html_comments = array();
93 if (strpos($str, '<!--') !== FALSE)
95 if (preg_match_all("#(<!\-\-.*?\-\->)#s", $str, $matches))
97 for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
99 $html_comments[] = $matches[0][$i];
100 $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
105 // match and yank <pre> tags if they exist. It's cheaper to do this separately since most content will
106 // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
107 if (strpos($str, '<pre') !== FALSE)
109 $str = preg_replace_callback("#<pre.*?>.*?</pre>#si", array($this, '_protect_characters'), $str);
112 // Convert quotes within tags to temporary markers.
113 $str = preg_replace_callback("#<.+?>#si", array($this, '_protect_characters'), $str);
115 // Do the same with braces if necessary
116 if ($this->protect_braced_quotes === TRUE)
118 $str = preg_replace_callback("#\{.+?\}#si", array($this, '_protect_characters'), $str);
121 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
122 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
123 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
124 $str = preg_replace("#<(/*)(".$this->inline_elements.")([ >])#i", "{@TAG}\\1\\2\\3", $str);
126 // Split the string at every tag. This expression creates an array with this prototype:
130 // [0] = <opening tag>
132 // [2] = <closing tag>
135 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
137 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
142 $total_chunks = count($chunks);
144 foreach ($chunks as $chunk)
148 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
149 // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
150 if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunk, $match))
152 if (preg_match("#".$this->skip_elements."#", $match[2]))
154 $process = ($match[1] == '/') ? TRUE : FALSE;
159 $this->last_block_element = $match[2];
166 if ($process == FALSE)
172 // Force a newline to make sure end tags get processed by _format_newlines()
173 if ($current_chunk == $total_chunks)
178 // Convert Newlines into <p> and <br /> tags
179 $str .= $this->_format_newlines($chunk);
182 // No opening block level tag? Add it if needed.
183 if ( ! preg_match("/^\s*<(?:".$this->block_elements.")/i", $str))
185 $str = preg_replace("/^(.*?)<(".$this->block_elements.")/i", '<p>$1</p><$2', $str);
188 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
189 $str = $this->format_characters($str);
191 // restore HTML comments
192 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
194 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
195 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
196 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
197 $str = preg_replace('#(?(?=<p>\{@HC'.$i.'\})<p>\{@HC'.$i.'\}(\s*</p>)|\{@HC'.$i.'\})#s', $html_comments[$i], $str);
203 // If the user submitted their own paragraph tags within the text
204 // we will retain them instead of using our tags.
205 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
207 // Reduce multiple instances of opening/closing paragraph tags to a single one
208 '#(</p>)+#' => '</p>',
209 '/(<p>\W*<p>)+/' => '<p>',
211 // Clean up stray paragraph tags that appear before block level elements
212 '#<p></p><('.$this->block_elements.')#' => '<$1',
214 // Clean up stray non-breaking spaces preceeding block elements
215 '#( \s*)+<('.$this->block_elements.')#' => ' <$2',
217 // Replace the temporary markers we added earlier
226 // Do we need to reduce empty lines?
227 if ($reduce_linebreaks === TRUE)
229 $table['#<p>\n*</p>#'] = '';
233 // If we have empty paragraph tags we add a non-breaking space
234 // otherwise most browsers won't treat them as true paragraphs
235 $table['#<p></p>#'] = '<p> </p>';
238 return preg_replace(array_keys($table), $table, $str);
242 // --------------------------------------------------------------------
247 * This function mainly converts double and single quotes
248 * to curly entities, but it also converts em-dashes,
249 * double spaces, and ampersands
255 function format_characters($str)
259 if ( ! isset($table))
262 // nested smart quotes, opening and closing
263 // note that rules for grammar (English) allow only for two levels deep
264 // and that single quotes are _supposed_ to always be on the outside
265 // but we'll accommodate both
266 // Note that in all cases, whitespace is the primary determining factor
267 // on which direction to curl, with non-word characters like punctuation
268 // being a secondary factor only after whitespace is addressed.
269 '/\'"(\s|$)/' => '’”$1',
270 '/(^|\s|<p>)\'"/' => '$1‘“',
271 '/\'"(\W)/' => '’”$1',
272 '/(\W)\'"/' => '$1‘“',
273 '/"\'(\s|$)/' => '”’$1',
274 '/(^|\s|<p>)"\'/' => '$1“‘',
275 '/"\'(\W)/' => '”’$1',
276 '/(\W)"\'/' => '$1“‘',
278 // single quote smart quotes
279 '/\'(\s|$)/' => '’$1',
280 '/(^|\s|<p>)\'/' => '$1‘',
281 '/\'(\W)/' => '’$1',
282 '/(\W)\'/' => '$1‘',
284 // double quote smart quotes
285 '/"(\s|$)/' => '”$1',
286 '/(^|\s|<p>)"/' => '$1“',
287 '/"(\W)/' => '”$1',
288 '/(\W)"/' => '$1“',
291 "/(\w)'(\w)/" => '$1’$2',
293 // Em dash and ellipses dots
294 '/\s?\-\-\s?/' => '—',
295 '/(\w)\.{3}/' => '$1…',
297 // double space after sentences
298 '/(\W) /' => '$1 ',
300 // ampersands, if not a character entity
301 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&'
305 return preg_replace(array_keys($table), $table, $str);
308 // --------------------------------------------------------------------
313 * Converts newline characters into either <p> tags or <br />
319 function _format_newlines($str)
326 if (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required))
331 // Convert two consecutive newlines to paragraphs
332 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
334 // Convert single spaces to <br /> tags
335 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
337 // Wrap the whole enchilada in enclosing paragraphs
340 $str = '<p>'.$str.'</p>';
343 // Remove empty paragraphs if they are on the first line, as this
344 // is a potential unintended consequence of the previous code
345 $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1);
350 // ------------------------------------------------------------------------
355 * Protects special characters from being formatted later
356 * We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ}
357 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
358 * likewise double spaces are converted to {@NBS} to prevent entity conversion
364 function _protect_characters($match)
366 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
369 // --------------------------------------------------------------------
372 * Convert newlines to HTML line breaks except within PRE tags
378 function nl2br_except_pre($str)
380 $ex = explode("pre>",$str);
384 for ($i = 0; $i < $ct; $i++)
388 $newstr .= nl2br($ex[$i]);
403 // END Typography Class
405 /* End of file Typography.php */
406 /* Location: ./system/libraries/Typography.php */