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