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 | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 33 | var $skip_elements = 'pre|ol|ul|p'; |
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; |
| 105 | foreach ($chunks as $chunk) |
| 106 | { |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 107 | // Are we dealing with a tag? If so, we'll skip the processing for this cycle. |
| 108 | // 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] | 109 | if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match)) |
| 110 | { |
| 111 | if (preg_match("#".$this->skip_elements."#", $match['2'])) |
| 112 | { |
| 113 | $process = ($match['1'] == '/') ? TRUE : FALSE; |
| 114 | } |
| 115 | |
| 116 | $str .= $chunk; |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | if ($process == FALSE) |
| 121 | { |
| 122 | $str .= $chunk; |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | // Convert Newlines into <p> and <br /> tags |
| 127 | $str .= $this->format_newlines($chunk); |
| 128 | } |
| 129 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 130 | // Convert Quotes, elipsis, and em-dashes |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 131 | $str = $this->format_characters($str); |
| 132 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 133 | // Do we need to reduce empty lines? |
| 134 | if ($this->reduce_empty_lines == TRUE) |
| 135 | { |
| 136 | $str = preg_replace('#(<p>\n*</p>)#', '', $str); |
| 137 | } |
| 138 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 139 | // Final clean up |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 140 | $table = array( |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 141 | |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 142 | // If the user submitted their own paragraph tags within the text |
| 143 | // we will retain them instead of using our tags. |
| 144 | '/(<p.*?>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix |
| 145 | |
| 146 | // Reduce multiple paragraphs to a single one |
| 147 | '/(<\/p>)+/' => '</p>', |
| 148 | '/(<p><p>)+/' => '<p>', |
| 149 | |
| 150 | // Clean up stray paragraph tags that appear before block level elements |
| 151 | '/<p><\/p><('.$this->block_elements.')/' => '<$1', |
| 152 | |
| 153 | // Replace the temporary markers we added earlier |
| 154 | '/\{@TAG\}/' => '<', |
| 155 | '/\{@DQ\}/' => '"', |
| 156 | '/\{@SQ\}/' => "'" |
| 157 | |
| 158 | ); |
| 159 | |
| 160 | return preg_replace(array_keys($table), $table, $str); |
| 161 | |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // -------------------------------------------------------------------- |
| 165 | |
| 166 | /** |
| 167 | * Format Characters |
| 168 | * |
| 169 | * This function mainly converts double and single quotes |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 170 | * to curly entities, but it also converts em-dashes, |
| 171 | * double spaces, and ampersands |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 172 | */ |
| 173 | function format_characters($str) |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 174 | { |
| 175 | static $table; |
| 176 | |
| 177 | if ( ! isset($table)) |
| 178 | { |
| 179 | $table = array( |
| 180 | // nested smart quotes, opening and closing |
| 181 | // note that rules for grammar (English) allow only for two levels deep |
| 182 | // and that single quotes are _supposed_ to always be on the outside |
| 183 | // but we'll accommodate both |
| 184 | '/(^|\W|\s)\'"/' => '$1‘“', |
| 185 | '/\'"(\s|\W|$)/' => '’”$1', |
| 186 | '/(^|\W|\s)"\'/' => '$1“‘', |
| 187 | '/"\'(\s|\W|$)/' => '”’$1', |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 188 | |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 189 | // single quote smart quotes |
| 190 | '/\'(\s|\W|$)/' => '’$1', |
| 191 | '/(^|\W|\s)\'/' => '$1‘', |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 192 | |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 193 | // double quote smart quotes |
| 194 | '/"(\s|\W|$)/' => '”$1', |
| 195 | '/(^|\W|\s)"/' => '$1“', |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 196 | |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 197 | // apostrophes |
| 198 | "/(\w)'(\w)/" => '$1’$2', |
| 199 | |
| 200 | // Em dash and ellipses dots |
| 201 | '/\s?\-\-\s?/' => '—', |
| 202 | '/\w\.{3}/' => '…', |
| 203 | |
| 204 | // double space after sentences |
Derek Jones | 50500a2 | 2008-09-11 18:21:18 +0000 | [diff] [blame] | 205 | '/(\W) /' => '$1 ', |
Derek Jones | ab504b8 | 2008-09-11 17:04:30 +0000 | [diff] [blame] | 206 | |
| 207 | // ampersands, if not a character entity |
| 208 | '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&' |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | return preg_replace(array_keys($table), $table, $str); |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Format Newlines |
| 219 | * |
| 220 | * Converts newline characters into either <p> tags or <br /> |
| 221 | * |
| 222 | */ |
| 223 | function format_newlines($str) |
| 224 | { |
| 225 | if ($str == '') |
| 226 | { |
| 227 | return $str; |
| 228 | } |
| 229 | |
| 230 | if (strpos($str, "\n") === FALSE) |
| 231 | { |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 232 | return $str; |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 233 | } |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 234 | |
| 235 | $str = str_replace("\n\n", "</p>\n\n<p>", $str); |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 236 | $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str); |
| 237 | |
| 238 | return '<p>'.$str.'</p>'; |
Rick Ellis | 18bd8b5 | 2008-09-10 23:40:35 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
| 244 | * Allow JavaScript Event Handlers? |
| 245 | * |
| 246 | * For security reasons, by default we disallow JS event handlers |
| 247 | * |
| 248 | */ |
| 249 | function allow_js_event_handlers($val = FALSE) |
| 250 | { |
| 251 | $this->allow_js_event_handlers = ($val === FALSE) ? FALSE : TRUE; |
| 252 | } |
Rick Ellis | 16e3c8c | 2008-09-11 20:12:26 +0000 | [diff] [blame^] | 253 | |
| 254 | // -------------------------------------------------------------------- |
| 255 | |
| 256 | /** |
| 257 | * Reduce empty lines |
| 258 | * |
| 259 | * Sets a flag that tells the parser to reduce any instances of more than |
| 260 | * two consecutive linebreaks down to two |
| 261 | * |
| 262 | */ |
| 263 | function reduce_empty_lines($val = FALSE) |
| 264 | { |
| 265 | $this->reduce_empty_lines = ($val === FALSE) ? FALSE : TRUE; |
| 266 | } |
Rick Ellis | 4c938ae | 2008-09-10 22:58:38 +0000 | [diff] [blame] | 267 | } |
| 268 | // END Typography Class |
| 269 | |
| 270 | /* End of file Typography.php */ |
| 271 | /* Location: ./system/libraries/Typography.php */ |