You are here

function codefilter_process_code in Code Filter 5

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

Processes chunks of escaped code into HTML.

File

./codefilter.module, line 63

Code

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('/&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);
}