@', '', str_replace('\"', '"', $text)); // Undo the escaping in the prepare step $text = decode_entities($text); // Trim leading and trailing linebreaks $text = trim($text, "\r\n"); // Highlight as PHP $text = '
'. highlight_string("", 1) .'
'; // In PHP4, highlight_string() returns font tags; replace them with span tags. $text = str_replace(array(''), $text); // Remove newlines to avoid clashing with the linebreak filter $text = str_replace("\n", '', $text); return codefilter_fix_spaces($text); } /** * Helper function for codefilter_process_code(). */ function codefilter_process_php_inline($matches) { // Undo nl2br $text = str_replace('
', '', $matches[0]); // Decode entities (the highlighter re-entifies) and highlight text $text = highlight_string(decode_entities($text), 1); // In PHP4, highlight_string() returns font tags; replace them with span tags. $text = str_replace(array(''), $text); // Remove PHP's own added code tags $text = str_replace(array('', '', "\n"), array('', '', ''), $text); return $text; } /** * Processes chunks of escaped code into HTML. */ function codefilter_process_code($text) { // Undo linebreak escaping $text = str_replace(' ', "\n", $text); // Inline or block level piece? $multiline = strpos($text, "\n") !== FALSE; // Note, pay attention to odd preg_replace-with-/e behaviour on slashes $text = preg_replace("/^\n/", '', preg_replace('@@', '', str_replace('\"', '"', $text))); // Trim leading and trailing linebreaks $text = trim($text, "\n"); // Escape newlines $text = nl2br($text); // PHP code in regular code $text = preg_replace_callback('/<\?php.+?\?>/s', 'codefilter_process_php_inline', $text); $text = ''. codefilter_fix_spaces(str_replace(' ', ' ', $text)) .''; if ($multiline) $text = '
'. $text .'
'; // Remove newlines to avoid clashing with the linebreak filter return str_replace("\n", '', $text); } function codefilter_fix_spaces($text) { return preg_replace('@ (?! )@', ' ', $text); } /** * Escape code blocks during input filter 'prepare'. * * @param $text * The string to escape. * @param $type * The type of code block, either 'code' or 'php'. * @return * The escaped string. */ function codefilter_escape($text, $type = 'code') { // Note, pay attention to odd preg_replace-with-/e behaviour on slashes $text = check_plain(str_replace('\"', '"', $text)); // Protect newlines from line break converter $text = str_replace(array("\r", "\n"), array('', ' '), $text); // Add codefilter escape tags $text = "[codefilter_$type]{$text}[/codefilter_$type]"; return $text; } /** * Implementation of hook_filter() */ function codefilter_filter($op, $delta = 0, $format = -1, $text = '') { switch ($op) { case 'list': return array(0 => t('Code filter')); case 'description': return t('Allows users to post code verbatim using <code> and <?php ?> tags.'); case 'prepare': /* Note: we replace , , [?php ?], <% %>, and [% %] to prevent other filters from acting on them. */ $text = preg_replace('@(.+?)@se', "codefilter_escape('$1', 'code')", $text); $text = preg_replace('@[\[<](\?php|%)(.+?)(\?|%)[\]>]@se', "codefilter_escape('$2', 'php')", $text); return $text; case 'process': $text = preg_replace('@\[codefilter_code\](.+?)\[/codefilter_code\]@se', "codefilter_process_code('$1')", $text); $text = preg_replace('@\[codefilter_php\](.+?)\[/codefilter_php\]@se', "codefilter_process_php('$1')", $text); return $text; default: return $text; } }