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_menu($may_cache) {
if (!$may_cache) {
drupal_add_css(drupal_get_path('module', 'codefilter') . '/codefilter.css');
}
}
function codefilter_process_php($text) {
$text = preg_replace('@</?(br|p)\\s*/?>@', '', str_replace('\\"', '"', $text));
$text = decode_entities($text);
$text = trim($text, "\r\n");
$text = '<div class="codeblock">' . highlight_string("<?php\n{$text}\n?>", 1) . '</div>';
$text = str_replace(array(
'<font color="',
'</font>',
), array(
'<span style="color: ',
'</span>',
), $text);
$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(
'<font color="',
'</font>',
), array(
'<span style="color: ',
'</span>',
), $text);
$text = str_replace(array(
'<code>',
'</code>',
"\n",
), array(
'',
'',
'',
), $text);
return $text;
}
function codefilter_process_code($text) {
$text = str_replace(' ', "\n", $text);
$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) {
return preg_replace('@ (?! )@', ' ', $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_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('@<code>(.+?)</code>@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;
}
}