Public Member Functions | |
| CI_Typography () | |
| Nothing to do here. | |
| auto_typography ($str, $strip_js_event_handlers=TRUE, $reduce_linebreaks=FALSE) | |
| Auto Typography. | |
| format_characters ($str) | |
| Format Characters. | |
| _format_newlines ($str) | |
| Format Newlines. | |
| nl2br_except_pre ($str) | |
| Convert newlines to HTML line breaks except within PRE tags. | |
Public Attributes | |
| $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul' | |
| $skip_elements = 'p|pre|ol|ul|dl|object|table' | |
| $inline_elements = 'a|abbr|acronym|b|bdo|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|samp|select|span|strong|sub|sup|textarea|var' | |
| $protect_braced_quotes = FALSE | |
Definition at line 27 of file Typography.php.
| CI_Typography::_format_newlines | ( | $ | str | ) |
Format Newlines.
Converts newline characters into either
tags or
public
| string |
Definition at line 274 of file Typography.php.
Referenced by auto_typography().
00275 { 00276 if ($str == '') 00277 { 00278 return $str; 00279 } 00280 00281 if (strpos($str, "\n") === FALSE) 00282 { 00283 return $str; 00284 } 00285 00286 // Convert two consecutive newlines to paragraphs 00287 $str = str_replace("\n\n", "</p>\n\n<p>", $str); 00288 00289 // Convert single spaces to <br /> tags 00290 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str); 00291 00292 // Wrap the whole enchilada in enclosing paragraphs 00293 if ($str != "\n") 00294 { 00295 $str = '<p>'.$str.'</p>'; 00296 } 00297 00298 // Remove empty paragraphs if they are on the first line, as this 00299 // is a potential unintended consequence of the previous code 00300 $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1); 00301 00302 return $str; 00303 }

| CI_Typography::auto_typography | ( | $ | str, | |
| $ | strip_js_event_handlers = TRUE, |
|||
| $ | reduce_linebreaks = FALSE | |||
| ) |
Auto Typography.
This function converts text, making it typographically correct:
Converts two spaces into entities
public
| string | ||
| bool | whether to strip javascript event handlers for security | |
| bool | whether to reduce more then two consecutive newlines to two |
Definition at line 66 of file Typography.php.
References _format_newlines(), and format_characters().
00067 { 00068 if ($str == '') 00069 { 00070 return ''; 00071 } 00072 00073 // Standardize Newlines to make matching easier 00074 if (strpos($str, "\r") !== FALSE) 00075 { 00076 $str = str_replace(array("\r\n", "\r"), "\n", $str); 00077 } 00078 00079 // Reduce line breaks. If there are more than two consecutive linebreaks 00080 // we'll compress them down to a maximum of two since there's no benefit to more. 00081 if ($reduce_linebreaks === TRUE) 00082 { 00083 $str = preg_replace("/\n\n+/", "\n\n", $str); 00084 } 00085 00086 // Do we allow JavaScript event handlers? If not, we strip them from within all tags 00087 if ($strip_js_event_handlers === TRUE) 00088 { 00089 $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str); 00090 } 00091 00092 // Convert quotes within tags to temporary markers. We don't want quotes converted 00093 // within tags so we'll temporarily convert them to {@DQ} and {@SQ} 00094 if (preg_match_all("#<.+?>#si", $str, $matches)) 00095 { 00096 for ($i = 0; $i < count($matches['0']); $i++) 00097 { 00098 $str = str_replace($matches['0'][$i], 00099 str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]), 00100 $str); 00101 } 00102 } 00103 00104 if ($this->protect_braced_quotes === TRUE) 00105 { 00106 if (preg_match_all("#\{.+?}#si", $str, $matches)) 00107 { 00108 for ($i = 0; $i < count($matches['0']); $i++) 00109 { 00110 $str = str_replace($matches['0'][$i], 00111 str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]), 00112 $str); 00113 } 00114 } 00115 } 00116 00117 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag 00118 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be 00119 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG} 00120 $str = preg_replace("#<(/*)(".$this->inline_elements.")([ >])#i", "{@TAG}\\1\\2\\3", $str); 00121 00122 // Split the string at every tag. This expression creates an array with this prototype: 00123 // 00124 // [array] 00125 // { 00126 // [0] = <opening tag> 00127 // [1] = Content... 00128 // [2] = <closing tag> 00129 // Etc... 00130 // } 00131 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); 00132 00133 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text 00134 $str = ''; 00135 $process = TRUE; 00136 $paragraph = FALSE; 00137 foreach ($chunks as $chunk) 00138 { 00139 // Are we dealing with a tag? If so, we'll skip the processing for this cycle. 00140 // Well also set the "process" flag which allows us to skip <pre> tags and a few other things. 00141 if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunk, $match)) 00142 { 00143 if (preg_match("#".$this->skip_elements."#", $match[2])) 00144 { 00145 $process = ($match[1] == '/') ? TRUE : FALSE; 00146 } 00147 00148 $str .= $chunk; 00149 continue; 00150 } 00151 00152 if ($process == FALSE) 00153 { 00154 $str .= $chunk; 00155 continue; 00156 } 00157 00158 // Convert Newlines into <p> and <br /> tags 00159 $str .= $this->_format_newlines($chunk); 00160 } 00161 00162 // is the whole of the content inside a block level element? 00163 if ( ! preg_match("/^<(?:".$this->block_elements.")/i", $str, $match)) 00164 { 00165 $str = "<p>{$str}</p>"; 00166 } 00167 00168 // Convert quotes, elipsis, and em-dashes 00169 $str = $this->format_characters($str); 00170 00171 // Final clean up 00172 $table = array( 00173 00174 // If the user submitted their own paragraph tags within the text 00175 // we will retain them instead of using our tags. 00176 '/(<p.*?>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix 00177 00178 // Reduce multiple instances of opening/closing paragraph tags to a single one 00179 '#(</p>)+#' => '</p>', 00180 '/(<p><p>)+/' => '<p>', 00181 00182 // Clean up stray paragraph tags that appear before block level elements 00183 '#<p></p><('.$this->block_elements.')#' => '<$1', 00184 00185 // Replace the temporary markers we added earlier 00186 '/\{@TAG\}/' => '<', 00187 '/\{@DQ\}/' => '"', 00188 '/\{@SQ\}/' => "'" 00189 00190 ); 00191 00192 // Do we need to reduce empty lines? 00193 if ($reduce_linebreaks === TRUE) 00194 { 00195 $table['#<p>\n*</p>#'] = ''; 00196 } 00197 else 00198 { 00199 // If we have empty paragraph tags we add a non-breaking space 00200 // otherwise most browsers won't treat them as true paragraphs 00201 $table['#<p></p>#'] = '<p> </p>'; 00202 } 00203 00204 return preg_replace(array_keys($table), $table, $str); 00205 00206 }

| CI_Typography::CI_Typography | ( | ) |
| CI_Typography::format_characters | ( | $ | str | ) |
Format Characters.
This function mainly converts double and single quotes to curly entities, but it also converts em-dashes, double spaces, and ampersands
public
| string |
Definition at line 221 of file Typography.php.
Referenced by auto_typography().
00222 { 00223 static $table; 00224 00225 if ( ! isset($table)) 00226 { 00227 $table = array( 00228 // nested smart quotes, opening and closing 00229 // note that rules for grammar (English) allow only for two levels deep 00230 // and that single quotes are _supposed_ to always be on the outside 00231 // but we'll accommodate both 00232 '/(^|\W|\s)\'"/' => '$1‘“', 00233 '/\'"(\s|\W|$)/' => '’”$1', 00234 '/(^|\W|\s)"\'/' => '$1“‘', 00235 '/"\'(\s|\W|$)/' => '”’$1', 00236 00237 // single quote smart quotes 00238 '/\'(\s|\W|$)/' => '’$1', 00239 '/(^|\W|\s)\'/' => '$1‘', 00240 00241 // double quote smart quotes 00242 '/"(\s|\W|$)/' => '”$1', 00243 '/(^|\W|\s)"/' => '$1“', 00244 00245 // apostrophes 00246 "/(\w)'(\w)/" => '$1’$2', 00247 00248 // Em dash and ellipses dots 00249 '/\s?\-\-\s?/' => '—', 00250 '/(\w)\.{3}/' => '$1…', 00251 00252 // double space after sentences 00253 '/(\W) /' => '$1 ', 00254 00255 // ampersands, if not a character entity 00256 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&' 00257 ); 00258 } 00259 00260 return preg_replace(array_keys($table), $table, $str); 00261 }

| CI_Typography::nl2br_except_pre | ( | $ | str | ) |
Convert newlines to HTML line breaks except within PRE tags.
public
| string |
Definition at line 314 of file Typography.php.
00315 { 00316 $ex = explode("pre>",$str); 00317 $ct = count($ex); 00318 00319 $newstr = ""; 00320 for ($i = 0; $i < $ct; $i++) 00321 { 00322 if (($i % 2) == 0) 00323 { 00324 $newstr .= nl2br($ex[$i]); 00325 } 00326 else 00327 { 00328 $newstr .= $ex[$i]; 00329 } 00330 00331 if ($ct - 1 != $i) 00332 $newstr .= "pre>"; 00333 } 00334 00335 return $newstr; 00336 }
| CI_Typography::$block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul' |
Definition at line 30 of file Typography.php.
| CI_Typography::$inline_elements = 'a|abbr|acronym|b|bdo|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|samp|select|span|strong|sub|sup|textarea|var' |
Definition at line 36 of file Typography.php.
| CI_Typography::$protect_braced_quotes = FALSE |
Definition at line 39 of file Typography.php.
| CI_Typography::$skip_elements = 'p|pre|ol|ul|dl|object|table' |
Definition at line 33 of file Typography.php.