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