You are here

public static function CodeFilter::processCode in Code Filter 8

Processes chunks of escaped code into HTML.

2 calls to CodeFilter::processCode()
CodeFilter::processCodeCallback in src/Plugin/Filter/CodeFilter.php
Callback to replace content of the <code> elements.
CodeFilterTest::testCodefilterProcessCode in src/Tests/CodeFilterTest.php
Test codefilter_process_code().

File

src/Plugin/Filter/CodeFilter.php, line 151

Class

CodeFilter
Text filter for highlighting PHP source code.

Namespace

Drupal\codefilter\Plugin\Filter

Code

public static function processCode($text) {

  // Undo linebreak escaping.
  $text = str_replace('&#10;', "\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', [
    get_class(),
    'processPHPInline',
  ], $text);
  $text = '<code>' . self::fixSpaces(str_replace(' ', '&nbsp;', $text)) . '</code>';
  $text = $multiline ? '<div class="codeblock">' . $text . '</div>' : $text;

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