View source
<?php
function codefilter_filter_tips($delta, $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.');
}
}
function codefilter_init() {
drupal_add_css(drupal_get_path('module', 'codefilter') . '/codefilter.css');
}
function codefilter_process_php($matches) {
$text = preg_replace('@</?(br|p)\\s*/?>@', '', str_replace('\\"', '"', $matches[1]));
$text = decode_entities($text);
$text = trim($text, "\r\n");
$text = '<div class="codeblock">' . highlight_string("<?php\n{$text}\n?>", 1) . '</div>';
$text = str_replace("\n", '', $text);
return codefilter_fix_spaces($text);
}
function codefilter_process_php_inline($matches) {
$text = str_replace('<br />', '', $matches[0]);
$text = highlight_string(decode_entities($text), 1);
$text = str_replace(array(
'<code>',
'</code>',
"\n",
), array(
'',
'',
'',
), $text);
return $text;
}
function codefilter_process_code($matches) {
$text = str_replace(' ', "\n", $matches[1]);
$multiline = strpos($text, "\n") !== FALSE;
$text = preg_replace("/^\n/", '', preg_replace('@</?(br|p)\\s*/?>@', '', str_replace('\\"', '"', $text)));
$text = trim($text, "\n");
$text = nl2br($text);
$text = preg_replace_callback('/<\\?php.+?\\?>/s', 'codefilter_process_php_inline', $text);
$text = '<code>' . codefilter_fix_spaces(str_replace(' ', ' ', $text)) . '</code>';
if ($multiline) {
$text = '<div class="codeblock">' . $text . '</div>';
}
return str_replace("\n", '', $text);
}
function codefilter_fix_spaces($text) {
$text = preg_replace('@ (?! )@', ' ', $text);
$text = preg_replace('@<br /> ([^ ])@', '<br /> $1', $text);
return $text;
}
function codefilter_escape($text, $type = 'code') {
$text = check_plain(str_replace('\\"', '"', $text));
$text = str_replace(array(
"\r",
"\n",
), array(
'',
' ',
), $text);
$text = "[codefilter_{$type}]{$text}[/codefilter_{$type}]";
return $text;
}
function _codefilter_escape_code_tag($matches) {
return codefilter_escape($matches[1], 'code');
}
function _codefilter_escape_php_tag($matches) {
return codefilter_escape($matches[2], 'php');
}
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':
$text = preg_replace_callback('@<code>(.+?)</code>@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;
}
}