You are here

function codefilter_process_code in Code Filter 6

Same name and namespace in other branches
  1. 5 codefilter.module \codefilter_process_code()
  2. 7 codefilter.module \codefilter_process_code()

Processes chunks of escaped code into HTML.

1 string reference to 'codefilter_process_code'
codefilter_filter in ./codefilter.module
Implementation of hook_filter()

File

./codefilter.module, line 57

Code

function codefilter_process_code($matches) {

  // Undo linebreak escaping
  $text = str_replace('
', "\n", $matches[1]);

  // 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('/&lt;\\?php.+?\\?&gt;/s', 'codefilter_process_php_inline', $text);
  $text = '<code>' . codefilter_fix_spaces(str_replace(' ', '&nbsp;', $text)) . '</code>';
  if ($multiline) {
    $text = '<div class="codeblock">' . $text . '</div>';
  }

  // Remove newlines to avoid clashing with the linebreak filter
  return str_replace("\n", '', $text);
}