Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Typography Class |
| 20 | * |
| 21 | * |
| 22 | * @access private |
| 23 | * @category Helpers |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/helpers/ |
| 26 | */ |
| 27 | class CI_Typography { |
| 28 | |
| 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'; |
| 31 | |
| 32 | // Elements that should not have <p> and <br /> tags within them. |
Robin Sowell | 8db2d5e | 2009-07-17 22:07:04 +0000 | [diff] [blame] | 33 | var $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | |
| 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'; |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 37 | |
| 38 | // array of block level elements that require inner content to be within another block level element |
| 39 | var $inner_block_required = array('blockquote'); |
| 40 | |
| 41 | // the last block element parsed |
| 42 | var $last_block_element = ''; |
| 43 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | // whether or not to protect quotes within { curly braces } |
| 45 | var $protect_braced_quotes = FALSE; |
| 46 | |
| 47 | /** |
| 48 | * Nothing to do here... |
| 49 | * |
| 50 | */ |
| 51 | function CI_Typography() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Auto Typography |
| 57 | * |
| 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 |
| 65 | * |
| 66 | * @access public |
| 67 | * @param string |
| 68 | * @param bool whether to reduce more then two consecutive newlines to two |
| 69 | * @return string |
| 70 | */ |
| 71 | function auto_typography($str, $reduce_linebreaks = FALSE) |
| 72 | { |
| 73 | if ($str == '') |
| 74 | { |
| 75 | return ''; |
| 76 | } |
| 77 | |
| 78 | // Standardize Newlines to make matching easier |
| 79 | if (strpos($str, "\r") !== FALSE) |
| 80 | { |
| 81 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
| 82 | } |
| 83 | |
| 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) |
| 87 | { |
| 88 | $str = preg_replace("/\n\n+/", "\n\n", $str); |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 89 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 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) |
| 94 | { |
| 95 | if (preg_match_all("#(<!\-\-.*?\-\->)#s", $str, $matches)) |
| 96 | { |
| 97 | for ($i = 0, $total = count($matches[0]); $i < $total; $i++) |
| 98 | { |
| 99 | $html_comments[] = $matches[0][$i]; |
| 100 | $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str); |
| 101 | } |
| 102 | } |
| 103 | } |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 104 | |
| 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) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 109 | $str = preg_replace_callback("#<pre.*?>.*?</pre>#si", array($this, '_protect_characters'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 112 | // Convert quotes within tags to temporary markers. |
| 113 | $str = preg_replace_callback("#<.+?>#si", array($this, '_protect_characters'), $str); |
| 114 | |
| 115 | // Do the same with braces if necessary |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | if ($this->protect_braced_quotes === TRUE) |
| 117 | { |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 118 | $str = preg_replace_callback("#\{.+?\}#si", array($this, '_protect_characters'), $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | } |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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); |
| 125 | |
| 126 | // Split the string at every tag. This expression creates an array with this prototype: |
| 127 | // |
| 128 | // [array] |
| 129 | // { |
| 130 | // [0] = <opening tag> |
| 131 | // [1] = Content... |
| 132 | // [2] = <closing tag> |
| 133 | // Etc... |
| 134 | // } |
| 135 | $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); |
| 136 | |
| 137 | // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text |
| 138 | $str = ''; |
| 139 | $process = TRUE; |
| 140 | $paragraph = FALSE; |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 141 | $current_chunk = 0; |
| 142 | $total_chunks = count($chunks); |
| 143 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | foreach ($chunks as $chunk) |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 145 | { |
| 146 | $current_chunk++; |
| 147 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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. |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 150 | if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunk, $match)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | { |
| 152 | if (preg_match("#".$this->skip_elements."#", $match[2])) |
| 153 | { |
| 154 | $process = ($match[1] == '/') ? TRUE : FALSE; |
| 155 | } |
| 156 | |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 157 | if ($match[1] == '') |
| 158 | { |
| 159 | $this->last_block_element = $match[2]; |
| 160 | } |
| 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | $str .= $chunk; |
| 163 | continue; |
| 164 | } |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 165 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | if ($process == FALSE) |
| 167 | { |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 168 | $str .= $chunk; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | continue; |
| 170 | } |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 171 | |
| 172 | // Force a newline to make sure end tags get processed by _format_newlines() |
| 173 | if ($current_chunk == $total_chunks) |
| 174 | { |
| 175 | $chunk .= "\n"; |
| 176 | } |
| 177 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | // Convert Newlines into <p> and <br /> tags |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 179 | $str .= $this->_format_newlines($chunk); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | } |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 181 | |
| 182 | // No opening block level tag? Add it if needed. |
| 183 | if ( ! preg_match("/^\s*<(?:".$this->block_elements.")/i", $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | { |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 185 | $str = preg_replace("/^(.*?)<(".$this->block_elements.")/i", '<p>$1</p><$2', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 187 | |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 188 | // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands |
| 189 | $str = $this->format_characters($str); |
| 190 | |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 191 | // restore HTML comments |
| 192 | for ($i = 0, $total = count($html_comments); $i < $total; $i++) |
| 193 | { |
Derek Jones | 4777fb8 | 2008-12-11 17:55:42 +0000 | [diff] [blame] | 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); |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 198 | } |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | // Final clean up |
| 201 | $table = array( |
| 202 | |
| 203 | // If the user submitted their own paragraph tags within the text |
| 204 | // we will retain them instead of using our tags. |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame^] | 205 | '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | |
| 207 | // Reduce multiple instances of opening/closing paragraph tags to a single one |
| 208 | '#(</p>)+#' => '</p>', |
| 209 | '/(<p>\W*<p>)+/' => '<p>', |
| 210 | |
| 211 | // Clean up stray paragraph tags that appear before block level elements |
| 212 | '#<p></p><('.$this->block_elements.')#' => '<$1', |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 213 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | // Clean up stray non-breaking spaces preceeding block elements |
Derek Jones | ffa4c84 | 2008-12-11 16:37:04 +0000 | [diff] [blame] | 215 | '#( \s*)+<('.$this->block_elements.')#' => ' <$2', |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 216 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | // Replace the temporary markers we added earlier |
| 218 | '/\{@TAG\}/' => '<', |
| 219 | '/\{@DQ\}/' => '"', |
| 220 | '/\{@SQ\}/' => "'", |
| 221 | '/\{@DD\}/' => '--', |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame^] | 222 | '/\{@NBS\}/' => ' ', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame^] | 224 | // An unintended consequence of the _format_newlines function is that |
| 225 | // some of the newlines get truncated, resulting in <p> tags |
| 226 | // starting immediately after <block> tags on the same line. |
| 227 | // This forces a newline after such occurrences, which looks much nicer. |
| 228 | "/><p>\n/" => ">\n<p>", |
| 229 | |
| 230 | // Similarly, there might be cases where a closing </block> will follow |
| 231 | // a closing </p> tag, so we'll correct it by adding a newline in between |
| 232 | "#</p></#" => "</p>\n</" |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | ); |
Derek Jones | a633ec2 | 2008-12-11 14:31:33 +0000 | [diff] [blame] | 234 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | // Do we need to reduce empty lines? |
| 236 | if ($reduce_linebreaks === TRUE) |
| 237 | { |
| 238 | $table['#<p>\n*</p>#'] = ''; |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | // If we have empty paragraph tags we add a non-breaking space |
| 243 | // otherwise most browsers won't treat them as true paragraphs |
| 244 | $table['#<p></p>#'] = '<p> </p>'; |
| 245 | } |
Derek Jones | e470212 | 2009-01-14 21:57:32 +0000 | [diff] [blame] | 246 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | return preg_replace(array_keys($table), $table, $str); |
| 248 | |
| 249 | } |
| 250 | |
| 251 | // -------------------------------------------------------------------- |
| 252 | |
| 253 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | * Format Characters |
| 255 | * |
| 256 | * This function mainly converts double and single quotes |
| 257 | * to curly entities, but it also converts em-dashes, |
| 258 | * double spaces, and ampersands |
| 259 | * |
| 260 | * @access public |
| 261 | * @param string |
| 262 | * @return string |
| 263 | */ |
| 264 | function format_characters($str) |
| 265 | { |
| 266 | static $table; |
| 267 | |
| 268 | if ( ! isset($table)) |
| 269 | { |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 270 | $table = array( |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | // nested smart quotes, opening and closing |
| 272 | // note that rules for grammar (English) allow only for two levels deep |
| 273 | // and that single quotes are _supposed_ to always be on the outside |
| 274 | // but we'll accommodate both |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 275 | // Note that in all cases, whitespace is the primary determining factor |
| 276 | // on which direction to curl, with non-word characters like punctuation |
| 277 | // being a secondary factor only after whitespace is addressed. |
| 278 | '/\'"(\s|$)/' => '’”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 279 | '/(^|\s|<p>)\'"/' => '$1‘“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 280 | '/\'"(\W)/' => '’”$1', |
| 281 | '/(\W)\'"/' => '$1‘“', |
| 282 | '/"\'(\s|$)/' => '”’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 283 | '/(^|\s|<p>)"\'/' => '$1“‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 284 | '/"\'(\W)/' => '”’$1', |
| 285 | '/(\W)"\'/' => '$1“‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | |
| 287 | // single quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 288 | '/\'(\s|$)/' => '’$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 289 | '/(^|\s|<p>)\'/' => '$1‘', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 290 | '/\'(\W)/' => '’$1', |
| 291 | '/(\W)\'/' => '$1‘', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | |
| 293 | // double quote smart quotes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 294 | '/"(\s|$)/' => '”$1', |
Derek Jones | 232484c | 2009-01-15 19:10:48 +0000 | [diff] [blame] | 295 | '/(^|\s|<p>)"/' => '$1“', |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 296 | '/"(\W)/' => '”$1', |
| 297 | '/(\W)"/' => '$1“', |
| 298 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | // apostrophes |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 300 | "/(\w)'(\w)/" => '$1’$2', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | |
| 302 | // Em dash and ellipses dots |
| 303 | '/\s?\-\-\s?/' => '—', |
| 304 | '/(\w)\.{3}/' => '$1…', |
| 305 | |
| 306 | // double space after sentences |
| 307 | '/(\W) /' => '$1 ', |
| 308 | |
| 309 | // ampersands, if not a character entity |
| 310 | '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&' |
Derek Jones | b859df8 | 2008-11-18 15:24:20 +0000 | [diff] [blame] | 311 | ); |
| 312 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | |
| 314 | return preg_replace(array_keys($table), $table, $str); |
| 315 | } |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
| 320 | * Format Newlines |
| 321 | * |
| 322 | * Converts newline characters into either <p> tags or <br /> |
| 323 | * |
| 324 | * @access public |
| 325 | * @param string |
| 326 | * @return string |
| 327 | */ |
| 328 | function _format_newlines($str) |
| 329 | { |
| 330 | if ($str == '') |
| 331 | { |
| 332 | return $str; |
| 333 | } |
Derek Jones | d5738d9 | 2008-11-14 16:53:34 +0000 | [diff] [blame] | 334 | |
| 335 | if (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | { |
| 337 | return $str; |
| 338 | } |
| 339 | |
| 340 | // Convert two consecutive newlines to paragraphs |
| 341 | $str = str_replace("\n\n", "</p>\n\n<p>", $str); |
| 342 | |
| 343 | // Convert single spaces to <br /> tags |
| 344 | $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str); |
| 345 | |
| 346 | // Wrap the whole enchilada in enclosing paragraphs |
| 347 | if ($str != "\n") |
| 348 | { |
Derek Jones | c206754 | 2010-10-01 07:19:57 -0500 | [diff] [blame^] | 349 | // We trim off the right-side new line so that the closing </p> tag |
| 350 | // will be positioned immediately following the string, matching |
| 351 | // the behavior of the opening <p> tag |
| 352 | $str = '<p>'.rtrim($str).'</p>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Remove empty paragraphs if they are on the first line, as this |
| 356 | // is a potential unintended consequence of the previous code |
| 357 | $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1); |
| 358 | |
| 359 | return $str; |
| 360 | } |
| 361 | |
| 362 | // ------------------------------------------------------------------------ |
| 363 | |
| 364 | /** |
Derek Jones | 7deecfb | 2008-12-11 15:38:01 +0000 | [diff] [blame] | 365 | * Protect Characters |
| 366 | * |
| 367 | * Protects special characters from being formatted later |
| 368 | * We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ} |
| 369 | * and we don't want double dashes converted to emdash entities, so they are marked with {@DD} |
| 370 | * likewise double spaces are converted to {@NBS} to prevent entity conversion |
| 371 | * |
| 372 | * @access public |
| 373 | * @param array |
| 374 | * @return string |
| 375 | */ |
| 376 | function _protect_characters($match) |
| 377 | { |
| 378 | return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]); |
| 379 | } |
| 380 | |
| 381 | // -------------------------------------------------------------------- |
| 382 | |
| 383 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | * Convert newlines to HTML line breaks except within PRE tags |
| 385 | * |
| 386 | * @access public |
| 387 | * @param string |
| 388 | * @return string |
| 389 | */ |
| 390 | function nl2br_except_pre($str) |
| 391 | { |
| 392 | $ex = explode("pre>",$str); |
| 393 | $ct = count($ex); |
| 394 | |
| 395 | $newstr = ""; |
| 396 | for ($i = 0; $i < $ct; $i++) |
| 397 | { |
| 398 | if (($i % 2) == 0) |
| 399 | { |
| 400 | $newstr .= nl2br($ex[$i]); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | $newstr .= $ex[$i]; |
| 405 | } |
| 406 | |
| 407 | if ($ct - 1 != $i) |
| 408 | $newstr .= "pre>"; |
| 409 | } |
| 410 | |
| 411 | return $newstr; |
| 412 | } |
| 413 | |
| 414 | } |
| 415 | // END Typography Class |
| 416 | |
| 417 | /* End of file Typography.php */ |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 418 | /* Location: ./system/libraries/Typography.php */ |