Make wptexturize faster. Props ecb29. fixes #2980
git-svn-id: http://svn.automattic.com/wordpress/trunk@4511 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
102885ebbc
commit
6f8d9c92a5
|
@ -2,56 +2,47 @@
|
|||
|
||||
function wptexturize($text) {
|
||||
global $wp_cockneyreplace;
|
||||
$next = true;
|
||||
$output = '';
|
||||
// Capture tags and everything inside them
|
||||
$textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
$stop = count($textarr); $next = true; // loop stuff
|
||||
for ($i = 0; $i < $stop; $i++) {
|
||||
$curl = $textarr[$i];
|
||||
$curl = '';
|
||||
$textarr = preg_split('/(<.*>)/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
$stop = count($textarr);
|
||||
|
||||
// if a plugin has provided an autocorrect array, use it
|
||||
if ( isset($wp_cockneyreplace) ) {
|
||||
$cockney = array_keys($wp_cockneyreplace);
|
||||
$cockney_replace = array_values($wp_cockneyreplace);
|
||||
} else {
|
||||
$cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
|
||||
$cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
|
||||
}
|
||||
|
||||
$static_characters = array_merge(array('---', ' -- ', '--', 'xn–', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
|
||||
$static_replacements = array_merge(array('—', ' — ', '–', 'xn--', '…', '“', '’s', '”', ' ™'), $cockneyreplace);
|
||||
|
||||
$dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
|
||||
$dynamic_replacements = array('’$1','$1‘', '$1″', '$1′', '$1’$2', '$1“$2', '”$1', '’$1', '$1×$2');
|
||||
|
||||
for ( $i = 0; $i < $stop; $i++ ) {
|
||||
$curl = $textarr[$i];
|
||||
|
||||
if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
|
||||
$curl = str_replace('---', '—', $curl);
|
||||
$curl = str_replace(' -- ', ' — ', $curl);
|
||||
$curl = str_replace('--', '–', $curl);
|
||||
$curl = str_replace('xn–', 'xn--', $curl);
|
||||
$curl = str_replace('...', '…', $curl);
|
||||
$curl = str_replace('``', '“', $curl);
|
||||
|
||||
// if a plugin has provided an autocorrect array, use it
|
||||
if ( isset($wp_cockneyreplace) ) {
|
||||
$cockney = array_keys($wp_cockneyreplace);
|
||||
$cockney_replace = array_values($wp_cockneyreplace);
|
||||
} else {
|
||||
$cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
|
||||
$cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
|
||||
}
|
||||
|
||||
$curl = str_replace($cockney, $cockneyreplace, $curl);
|
||||
|
||||
$curl = preg_replace("/'s/", '’s', $curl);
|
||||
$curl = preg_replace("/'(\d\d(?:’|')?s)/", "’$1", $curl);
|
||||
$curl = preg_replace('/(\s|\A|")\'/', '$1‘', $curl);
|
||||
$curl = preg_replace('/(\d+)"/', '$1″', $curl);
|
||||
$curl = preg_replace("/(\d+)'/", '$1′', $curl);
|
||||
$curl = preg_replace("/(\S)'([^'\s])/", "$1’$2", $curl);
|
||||
$curl = preg_replace('/(\s|\A)"(?!\s)/', '$1“$2', $curl);
|
||||
$curl = preg_replace('/"(\s|\S|\Z)/', '”$1', $curl);
|
||||
$curl = preg_replace("/'([\s.]|\Z)/", '’$1', $curl);
|
||||
$curl = preg_replace("/ \(tm\)/i", ' ™', $curl);
|
||||
$curl = str_replace("''", '”', $curl);
|
||||
|
||||
$curl = preg_replace('/(\d+)x(\d+)/', "$1×$2", $curl);
|
||||
// static strings
|
||||
$curl = str_replace($static_characters, $static_replacements, $curl);
|
||||
|
||||
// regular expressions
|
||||
$curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
|
||||
} elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
|
||||
// strstr is fast
|
||||
$next = false;
|
||||
} else {
|
||||
$next = true;
|
||||
}
|
||||
|
||||
$curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl);
|
||||
$output .= $curl;
|
||||
}
|
||||
return $output;
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
function clean_pre($text) {
|
||||
|
|
Loading…
Reference in New Issue