sections in the content and escape certain characters, depending on the markup and lang attributes. (For backwards compatibility, "allow" works as a synonym of "markup".) Also remove newlines after and before . This prevents spurious linebreaks. */ global $wp_version; // TODO wrap multiline code tags in a pre element if not already wrapped // $content = preg_replace('%(?]*>[^>]*)(]*>.*?)%ims', '
\n$1\n
', $content); if ($wp_version < '2.3') { // escape backslashes so WordPress doesn't remove them (this problem was fixed in WP 2.3) $content = preg_replace_callback('!]*)>(.*?)!ims', 'tguy_cmu_escape_backslash_callback', $content); } // encode XML inside code blocks (removing newlines after and before ) $content = preg_replace_callback('!]*)>(?:\r\n|\n|\r|)(.*?)(?:\r\n|\n|\r|)!ims', 'tguy_cmu_encode_xml_callback', $content); return $content; } function tguy_cmu_tidy_code($content) { /* Fix two potential WordPress problems: when a post is displayed, - Double quotes inside a
 are prepended with a backslash.
	- Contents inside a  block after the first tag are texturized.
*/
	// unescape double quotes inside 
 blocks
	$content = preg_replace_callback('!]*)>(.*?)
!ims', 'tguy_cmu_unescape_qq_callback', $content); // untexturize the contents of blocks $content = preg_replace_callback('!]*)>(.*?)!ims', 'tguy_cmu_untexturize_code_callback', $content); return $content; } // ===== Callback functions ================================================== function tguy_cmu_encode_xml_callback($matches) { /* Encode XML in a tag. */ $attributes = $matches[1]; $escapedContent = $matches[2]; $attrMatches = array(); // $markup tells us what HTML special chars are allowed to remain unescaped. // This can be set to a space-separated list of tags. Can also be set to the // special values all, none or default. If missing, same as default. // Also remove the attribute once we've used it. $markup = 'default'; if (0 < preg_match('!^(.*?)\s+(?:markup|allow)="([^"]*)"(.*)$!i', $attributes, $attrMatches)) { $markup = strtolower($attrMatches[2]); $attributes = $attrMatches[1] . $attrMatches[3]; } // Depending on language, default handling may change if ($markup == 'default') { // See if lang is specified; also remove the attribute once we've used it. if (0 < preg_match('!^(.*)lang="([^"]*)"(.*)$!i', $attributes, $attrMatches)) { $lang = strtolower($attrMatches[2]); $attributes = $attrMatches[1] . $attrMatches[3]; if ($lang == 'html' || $lang == 'xhtml') { $markup = 'none'; } } } if ($markup == 'all') { // Nothing to do -- allow anything through. } else { // Could be default, none, or (possibly blank) space-separated list. if ($markup == 'none' || $markup == '') { $allowedTags = ''; } else if ($markup == 'default' || $markup == 'tags') { // 'tags' allowed for backward compatibility $allowedTags = 'em|strong|b|i|ins|del|a|span|comment'; } else { $allowedTags = preg_replace('!\s+!', '|', trim($markup)); } // Escape html special chars $escapedContent = htmlspecialchars($escapedContent, ENT_NOQUOTES); if ($allowedTags != '') { // Certain HTML tags are allowed: translate them back. $escapedContent = preg_replace_callback('!</?('.$allowedTags.')( .*?)?>!is', 'tguy_cmu_unescape_tag', $escapedContent); if (false !== strpos($allowedTags, 'comment')) { $escapedContent = preg_replace_callback('|<!--.*?-->|is', 'tguy_cmu_unescape_tag', $escapedContent); } } } return "$escapedContent"; } function tguy_cmu_unescape_tag($matches) { return str_replace( array(">", "<", """, "&"), array(">", "<", "\"", "&"), $matches[0]); } function tguy_cmu_escape_backslash_callback($matches) { /* Escape backslashes in a
 tag.
*/
	return "".str_replace('\\', '\\\\', $matches[2])."
"; } function tguy_cmu_unescape_qq_callback($matches) { /* Unescape double quotes in a
 tag.
*/
	return "".str_replace('\"', '"', $matches[2])."
"; } function tguy_cmu_untexturize_code_callback($matches) { /* Undo the effect of wptexturize() within a element. wptexturize() is meant to handle this but is buggy... BUGS: Turns --- into -- and `` into " */ $fancy = array('×', '‘', '’', '′', '“', '”', '″', '—', '–', '…', '“'); $plain = array('x' ,'\'' , '\'' , '\'' , '"' , '"' , '"' , '--' , '--' , '...' , '``' ); return "".str_replace($fancy, $plain, $matches[2]).""; } ?>