@', '', 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_process_code_callback($matches) {
return codefilter_process_code($matches[1]);
}
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;
}
/**
* Callback to escape content of elements.
*/
function _codefilter_escape_code_tag_callback($matches) {
return codefilter_escape($matches[1], 'code');
}
/**
* Callback to escape content of , [?php ?], <% %>, and [% %] elements.
*/
function _codefilter_escape_php_tag_callback($matches) {
return codefilter_escape($matches[2], 'php');
}
/**
* Implement hook_filter_info().
*/
function codefilter_filter_info() {
$filters['codefilter'] = array(
'title' => t('Code filter'),
'description' => t('Allows users to post code verbatim using <code> and <?php ?> tags.'),
'prepare callback' => '_codefilter_prepare',
'process callback' => '_codefilter_process',
'tips callback' => '_codefilter_tips',
);
return $filters;
}
function _codefilter_prepare($text, $format) {
/* Note: we replace
, , [?php ?], <% %>, and [% %]
to prevent other filters from acting on them. */
$text = preg_replace_callback('@(.+?)
@s', '_codefilter_escape_code_tag_callback', $text);
$text = preg_replace_callback('@[\[<](\?php|%)(.+?)(\?|%)[\]>]@s', '_codefilter_escape_php_tag_callback', $text);
return $text;
}
function _codefilter_process($text, $format) {
$text = preg_replace_callback('@\[codefilter_code\](.+?)\[/codefilter_code\]@s', '_codefilter_process_code_callback', $text);
$text = preg_replace_callback('@\[codefilter_php\](.+?)\[/codefilter_php\]@s', '_codefilter_process_php_callback', $text);
return $text;
}
function _codefilter_tips($format, $long = FALSE) {
if ($long) {
return t('To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.');
}
else {
return t('You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.');
}
}