@', '', 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 = '
', '
', "\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('@?(br|p)\s*/?>@', '', 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 = ' elements.
*/
function _codefilter_escape_code_tag($matches) {
return codefilter_escape($matches[1], 'code');
}
/**
* Callback to replace , [?php ?], <% %>, and [% %] elements.
*/
function _codefilter_escape_php_tag($matches) {
return codefilter_escape($matches[2], 'php');
}
/**
* 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_callback('@(.+?)
@s', '_codefilter_escape_code_tag', $text);
$text = preg_replace_callback('@[\[<](\?php|%)(.+?)(\?|%)[\]>]@s', '_codefilter_escape_php_tag', $text);
return $text;
case 'process':
$text = preg_replace_callback('@\[codefilter_code\](.+?)\[/codefilter_code\]@s', 'codefilter_process_code', $text);
$text = preg_replace_callback('@\[codefilter_php\](.+?)\[/codefilter_php\]@s', 'codefilter_process_php', $text);
return $text;
default:
return $text;
}
}