You are here

function _syntaxhighlighter_do_filter_prepare in Syntax Highlighter 6.2

Same name and namespace in other branches
  1. 6 syntaxhighlighter.module \_syntaxhighlighter_do_filter_prepare()
  2. 7.2 syntaxhighlighter.module \_syntaxhighlighter_do_filter_prepare()
  3. 7 syntaxhighlighter.module \_syntaxhighlighter_do_filter_prepare()

Escape the content text in preparation for filtering:

  • change all syntaxhighlighter <pre> tag pairs to {-_sYnTaXhIgHlIgHtEr_-} {/-_sYnTaXhIgHlIgHtEr_-} pair (so other filters would not mess with them

Precondition: all the open/close tags much match because search is done on pair by pair basis. If match is not even, do nothing.

All HTML tags and entities inside the SyntaxHighlighter must be properly escape. For example, if you show HTML code, change

  • '<' to '&lt;': e.g. <pre> -> &lt;pre>, <html> -> &lt;html>
  • neutralize & in entity: e.g.: &gt; -> &amp;gt;

Parameters

string $text: the content text to be filtered

Return value

the escape content text

1 call to _syntaxhighlighter_do_filter_prepare()
syntaxhighlighter_filter in ./syntaxhighlighter.module
Implements hook_filter()

File

./syntaxhighlighter.module, line 300
Syntax highlight code using the Syntaxhighlighter javascript library. See http://alexgorbatchev.com/wiki/SyntaxHighlighter

Code

function _syntaxhighlighter_do_filter_prepare($text) {
  $tag_name = variable_get('syntaxhighlighter_tagname', 'pre');
  $pattern = "#<{$tag_name}\\s*([^>]*)>|</\\s*{$tag_name}>#";
  preg_match_all($pattern, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  $output = '';
  $at = 0;
  $n = count($matches);

  // do nothing if open/close tag match is not even
  if ($n % 2) {
    return $text;
  }
  for ($i = 0; $i < $n;) {
    $open_tag = $matches[$i++];
    $close_tag = $matches[$i++];
    if (strpos($open_tag[1][0], 'brush:')) {
      $output .= substr($text, $at, $open_tag[0][1] - $at);
      $begin = $open_tag[0][1] + strlen($open_tag[0][0]);
      $length = $close_tag[0][1] - $begin;
      $output .= '{' . SYNTAXHIGHLGHTER_TAG_STRING . ' ' . $open_tag[1][0] . '}' . substr($text, $begin, $length) . '{/' . SYNTAXHIGHLGHTER_TAG_STRING . '}';
      $at = $close_tag[0][1] + strlen($close_tag[0][0]);
    }
  }
  $output .= substr($text, $at);
  return $output;
}