You are here

function codefilter_prism_process_code in Code Filter 7

Processes chunks of escaped code into HTML.

Parameters

string $text: Code to convert to HTML.

string $attributes: Attributes to add to the enclosing HTML tag (eg class="myclass").

Return value

string HTML containing the text suitable for consumption by prism.

1 call to codefilter_prism_process_code()
_codefilter_prism_process_code_callback in modules/codefilter_prism/codefilter_prism.module
Callback to replace content of the <code> elements.

File

modules/codefilter_prism/codefilter_prism.module, line 110
Text filter for highlighting PHP source code.

Code

function codefilter_prism_process_code($text, $attributes = '') {

  // Trim first leading line break.
  $text = preg_replace('@^[ \\t]*(\\n|&#10;)@', '', $text);

  // Add language class if one doesn't exist.
  $class_added = FALSE;
  if (strpos($attributes, 'class="') !== FALSE) {
    if (strpos($attributes, 'language-') === FALSE) {
      $attributes = str_replace('class="', 'class="language-php ', $attributes);
      $class_added = TRUE;
    }
  }
  else {
    $attributes .= ' class="language-php"';
    $class_added = TRUE;
  }

  // If the class was already added, the filter has been applied twice, such as
  // with WYSIWYG processing, undo entity encoding.
  if (!$class_added) {
    $text = decode_entities($text);
  }
  $text = codefilter_fix_spaces($text);
  $text = '<code' . $attributes . '>' . $text . '</code>';

  // If the class was newly added, and the code is “block”-level with newlines
  // or encoded newlines, add a wrapping <pre>.
  if ($class_added && (strpos($text, "\n") !== FALSE || strpos($text, '&#10;'))) {
    $text = '<pre class="codeblock">' . $text . '</pre>';
  }
  return $text;
}