You are here

public static function CodeFilter::escape in Code Filter 8

Escape code blocks.

Parameters

string $text: The string to escape.

string $type: The type of code block, either 'code' or 'php'.

Return value

string The escaped string.

2 calls to CodeFilter::escape()
CodeFilter::codeTagCallback in src/Plugin/Filter/CodeFilter.php
Callback to escape content of <code> tags.
CodeFilter::phpTagCallback in src/Plugin/Filter/CodeFilter.php
Callback to escape content of <?php ?>, [?php ?], <% %>, and [% %] tags.

File

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

Class

CodeFilter
Text filter for highlighting PHP source code.

Namespace

Drupal\codefilter\Plugin\Filter

Code

public static function escape($text, $type = 'code') {

  // Note, pay attention to odd preg_replace-with-/e behaviour on slashes.
  $text = Html::escape(str_replace('\\"', '"', $text));

  // Protect newlines from line break converter.
  $text = str_replace([
    "\r",
    "\n",
  ], [
    '',
    '&#10;',
  ], $text);

  // Add codefilter escape tags.
  $text = "[codefilter_{$type}]{$text}[/codefilter_{$type}]";
  return $text;
}