Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +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 |
Rick Ellis | 08e3812 | 2008-09-12 06:21:25 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc. |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +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 |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 30 | var $block_elements = 'p|div|blockquote|pre|code|h\d|script|ol|ul'; |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 31 | |
| 32 | // Elements that should not have <p> and <br /> tags within them. |
Rick Ellis | 3ef59b7 | 2008-09-11 21:35:39 +0000 | [diff] [blame] | 33 | var $skip_elements = 'p|pre|ol|ul'; |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 34 | |
| 35 | // Tags we want the parser to completely ignore when splitting the string. |
| 36 | var $ignore_elements = 'a|b|i|em|strong|span|img|li'; |
| 37 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 38 | /** |
Rick Ellis | 142f8ed | 2008-09-12 06:14:52 +0000 | [diff] [blame] | 39 | * Nothing to do here... |
| 40 | * |
| 41 | */ |
| 42 | function CI_Typography() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | /** |
Rick Ellis | 08e3812 | 2008-09-12 06:21:25 +0000 | [diff] [blame] | 47 | * Auto Typography |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 48 | * |
Rick Ellis | 08e3812 | 2008-09-12 06:21:25 +0000 | [diff] [blame] | 49 | * This function converts text, making it typographically correct: |
| 50 | * - Converts double spaces into paragraphs. |
| 51 | * - Converts single line breaks into <br /> tags |
| 52 | * - Converts single and double quotes into correctly facing curly quote entities. |
| 53 | * - Converts three dots into ellipsis. |
| 54 | * - Converts double dashes into em-dashes. |
| 55 | * - Converts two spaces into entities |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 56 | * |
| 57 | * @access public |
| 58 | * @param string |
| 59 | * @param bool whether to strip javascript event handlers for security |
| 60 | * @param bool whether to reduce more then two consecutive newlines to two |
| 61 | * @return string |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 62 | */ |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 63 | function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE) |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 64 | { |
| 65 | if ($str == '') |
| 66 | { |
| 67 | return ''; |
| 68 | } |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 69 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 70 | // Standardize Newlines to make matching easier |
| 71 | if (strpos($str, "\r") !== FALSE) |
| 72 | { |
| 73 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
| 74 | } |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 75 | |
| 76 | // Reduce line breaks. If there are more than two consecutive linebreaks |
| 77 | // we'll compress them down to a maximum of two since there's no benefit to more. |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 78 | if ($reduce_linebreaks === TRUE) |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 79 | { |
| 80 | $str = preg_replace("/\n\n+/", "\n\n", $str); |
| 81 | } |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 82 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 83 | // Do we allow JavaScript event handlers? If not, we strip them from within all tags |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 84 | if ($strip_js_event_handlers === TRUE) |
Rick Ellis | 18bd8b5 | 2008-09-10 23:40:35 +0000 | [diff] [blame] | 85 | { |
Rick Ellis | e320bc8 | 2008-09-11 18:11:31 +0000 | [diff] [blame] | 86 | $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str); |
Rick Ellis | 18bd8b5 | 2008-09-10 23:40:35 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Rick Ellis | 339fab7 | 2008-09-12 06:04:39 +0000 | [diff] [blame] | 89 | // Convert quotes within tags to temporary markers. We don't want quotes converted |
| 90 | // within tags so we'll temporarily convert them to {@DQ} and {@SQ} |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 91 | if (preg_match_all("#\<.+?>#si", $str, $matches)) |
| 92 | { |
| 93 | for ($i = 0; $i < count($matches['0']); $i++) |
| 94 | { |
| 95 | $str = str_replace($matches['0'][$i], |
| 96 | str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]), |
| 97 | $str); |
| 98 | } |
| 99 | } |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 100 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 101 | // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag |
| 102 | // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be |
Rick Ellis | 339fab7 | 2008-09-12 06:04:39 +0000 | [diff] [blame] | 103 | // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG} |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 104 | $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str); |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 105 | |
| 106 | // Split the string at every tag. This expression creates an array with this prototype: |
| 107 | // |
| 108 | // [array] |
| 109 | // { |
| 110 | // [0] = <opening tag> |
| 111 | // [1] = Content... |
| 112 | // [2] = <closing tag> |
| 113 | // Etc... |
| 114 | // } |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 115 | $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); |
| 116 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 117 | // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 118 | $str = ''; |
| 119 | $process = TRUE; |
Rick Ellis | 3ef59b7 | 2008-09-11 21:35:39 +0000 | [diff] [blame] | 120 | $paragraph = FALSE; |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 121 | foreach ($chunks as $chunk) |
| 122 | { |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 123 | // Are we dealing with a tag? If so, we'll skip the processing for this cycle. |
| 124 | // Well also set the "process" flag which allows us to skip <pre> tags and a few other things. |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 125 | if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match)) |
| 126 | { |
Rick Ellis | 3ef59b7 | 2008-09-11 21:35:39 +0000 | [diff] [blame] | 127 | if (preg_match("#".$this->skip_elements."#", $match[2])) |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 128 | { |
Rick Ellis | 3ef59b7 | 2008-09-11 21:35:39 +0000 | [diff] [blame] | 129 | $process = ($match[1] == '/') ? TRUE : FALSE; |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 130 | } |
Rick Ellis | 3ef59b7 | 2008-09-11 21:35:39 +0000 | [diff] [blame] | 131 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 132 | $str .= $chunk; |
| 133 | continue; |
| 134 | } |
Rick Ellis | 3ef59b7 | 2008-09-11 21:35:39 +0000 | [diff] [blame] | 135 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 136 | if ($process == FALSE) |
| 137 | { |
| 138 | $str .= $chunk; |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | // Convert Newlines into <p> and <br /> tags |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 143 | $str .= $this->_format_newlines($chunk); |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Rick Ellis | 339fab7 | 2008-09-12 06:04:39 +0000 | [diff] [blame] | 146 | // Convert quotes, elipsis, and em-dashes |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 147 | $str = $this->format_characters($str); |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 148 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 149 | // Final clean up |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 150 | $table = array( |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 151 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 152 | // If the user submitted their own paragraph tags within the text |
| 153 | // we will retain them instead of using our tags. |
| 154 | '/(<p.*?>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix |
| 155 | |
Rick Ellis | 25319a3 | 2008-09-11 20:13:25 +0000 | [diff] [blame] | 156 | // Reduce multiple instances of opening/closing paragraph tags to a single one |
Rick Ellis | 339fab7 | 2008-09-12 06:04:39 +0000 | [diff] [blame] | 157 | '#(</p>)+#' => '</p>', |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 158 | '/(<p><p>)+/' => '<p>', |
| 159 | |
| 160 | // Clean up stray paragraph tags that appear before block level elements |
Rick Ellis | 339fab7 | 2008-09-12 06:04:39 +0000 | [diff] [blame] | 161 | '#<p></p><('.$this->block_elements.')#' => '<$1', |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 162 | |
| 163 | // Replace the temporary markers we added earlier |
| 164 | '/\{@TAG\}/' => '<', |
| 165 | '/\{@DQ\}/' => '"', |
| 166 | '/\{@SQ\}/' => "'" |
| 167 | |
| 168 | ); |
| 169 | |
Rick Ellis | 339fab7 | 2008-09-12 06:04:39 +0000 | [diff] [blame] | 170 | // Do we need to reduce empty lines? |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 171 | if ($reduce_linebreaks === TRUE) |
Rick Ellis | 339fab7 | 2008-09-12 06:04:39 +0000 | [diff] [blame] | 172 | { |
| 173 | $table['#<p>\n*</p>#'] = ''; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | // If we have empty paragraph tags we add a non-breaking space |
| 178 | // otherwise most browsers won't treat them as true paragraphs |
| 179 | $table['#<p></p>#'] = '<p> </p>'; |
| 180 | } |
| 181 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 182 | return preg_replace(array_keys($table), $table, $str); |
| 183 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
| 189 | * Format Characters |
| 190 | * |
| 191 | * This function mainly converts double and single quotes |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 192 | * to curly entities, but it also converts em-dashes, |
| 193 | * double spaces, and ampersands |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 194 | * |
| 195 | * @access public |
| 196 | * @param string |
| 197 | * @return string |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 198 | */ |
| 199 | function format_characters($str) |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 200 | { |
| 201 | static $table; |
| 202 | |
| 203 | if ( ! isset($table)) |
| 204 | { |
| 205 | $table = array( |
| 206 | // nested smart quotes, opening and closing |
| 207 | // note that rules for grammar (English) allow only for two levels deep |
| 208 | // and that single quotes are _supposed_ to always be on the outside |
| 209 | // but we'll accommodate both |
| 210 | '/(^|\W|\s)\'"/' => '$1‘“', |
| 211 | '/\'"(\s|\W|$)/' => '’”$1', |
| 212 | '/(^|\W|\s)"\'/' => '$1“‘', |
| 213 | '/"\'(\s|\W|$)/' => '”’$1', |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 214 | |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 215 | // single quote smart quotes |
| 216 | '/\'(\s|\W|$)/' => '’$1', |
| 217 | '/(^|\W|\s)\'/' => '$1‘', |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 218 | |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 219 | // double quote smart quotes |
| 220 | '/"(\s|\W|$)/' => '”$1', |
| 221 | '/(^|\W|\s)"/' => '$1“', |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 222 | |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 223 | // apostrophes |
| 224 | "/(\w)'(\w)/" => '$1’$2', |
| 225 | |
| 226 | // Em dash and ellipses dots |
| 227 | '/\s?\-\-\s?/' => '—', |
| 228 | '/\w\.{3}/' => '…', |
| 229 | |
| 230 | // double space after sentences |
Derek Jones | 50500a2 | 2008-09-11 18:21:18 +0000 | [diff] [blame] | 231 | '/(\W) /' => '$1 ', |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 232 | |
| 233 | // ampersands, if not a character entity |
| 234 | '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&' |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | return preg_replace(array_keys($table), $table, $str); |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
| 244 | * Format Newlines |
| 245 | * |
| 246 | * Converts newline characters into either <p> tags or <br /> |
| 247 | * |
| 248 | */ |
Rick Ellis | 3bc791f | 2008-09-12 23:15:52 +0000 | [diff] [blame^] | 249 | function _format_newlines($str) |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 250 | { |
| 251 | if ($str == '') |
| 252 | { |
| 253 | return $str; |
| 254 | } |
| 255 | |
| 256 | if (strpos($str, "\n") === FALSE) |
| 257 | { |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 258 | return $str; |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 259 | } |
Rick Ellis | 3ef59b7 | 2008-09-11 21:35:39 +0000 | [diff] [blame] | 260 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame] | 261 | $str = str_replace("\n\n", "</p>\n\n<p>", $str); |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 262 | $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str); |
| 263 | |
| 264 | return '<p>'.$str.'</p>'; |
Rick Ellis | 18bd8b5 | 2008-09-10 23:40:35 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Rick Ellis | 9907df4 | 2008-09-12 07:22:21 +0000 | [diff] [blame] | 267 | // ------------------------------------------------------------------------ |
| 268 | |
| 269 | /** |
| 270 | * Convert newlines to HTML line breaks except within PRE tags |
| 271 | * |
| 272 | * @access public |
| 273 | * @param string |
| 274 | * @return string |
| 275 | */ |
| 276 | function nl2br_except_pre($str) |
| 277 | { |
| 278 | $ex = explode("pre>",$str); |
| 279 | $ct = count($ex); |
| 280 | |
| 281 | $newstr = ""; |
| 282 | for ($i = 0; $i < $ct; $i++) |
| 283 | { |
| 284 | if (($i % 2) == 0) |
| 285 | { |
| 286 | $newstr .= nl2br($ex[$i]); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | $newstr .= $ex[$i]; |
| 291 | } |
| 292 | |
| 293 | if ($ct - 1 != $i) |
| 294 | $newstr .= "pre>"; |
| 295 | } |
| 296 | |
| 297 | return $newstr; |
| 298 | } |
| 299 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 300 | } |
| 301 | // END Typography Class |
| 302 | |
| 303 | /* End of file Typography.php */ |
| 304 | /* Location: ./system/libraries/Typography.php */ |