You are here

function _geshifilter_prepare in GeSHi Filter for syntax highlighting 5.2

Same name and namespace in other branches
  1. 6 geshifilter.pages.inc \_geshifilter_prepare()
  2. 7 geshifilter.pages.inc \_geshifilter_prepare()

geshifilter_filter callback for preparing input text.

1 call to _geshifilter_prepare()
geshifilter_filter in ./geshifilter.module
Implementation of hook_filter().

File

./geshifilter.pages.inc, line 89

Code

function _geshifilter_prepare($format, $text) {

  // get the available tags
  list($generic_code_tags, $language_tags, $tag_to_lang) = _geshifilter_get_tags($format);
  $tags = array_merge($generic_code_tags, $language_tags);

  // escape special (regular expression) characters in tags (for tags like 'c++' and 'c#')
  $tags = preg_replace('#(\\+|\\#)#', '\\\\$1', $tags);
  $tags_string = implode('|', $tags);

  // Pattern for matching "<code>...</code>" like stuff
  // Also matches "<code>...$"  where "$" refers to end of string, not end of
  // line (because PCRE_MULTILINE (modifier 'm') is not enabled), so matching
  // still works when teaser view trims inside the source code.
  switch (_geshifilter_brackets($format)) {
    case GESHIFILTER_BRACKETS_ANGLE:
      $pattern = '#(<)(' . $tags_string . ')((\\s+[^>]*)*)(>)(.*?)(</\\2\\s*>|$)#s';
      break;
    case GESHIFILTER_BRACKETS_SQUARE:
      $pattern = '#(\\[)(' . $tags_string . ')((\\s+[^\\]]*)*)(\\])(.*?)(\\[/\\2\\s*\\]|$)#s';
      break;
    case GESHIFILTER_BRACKETS_BOTH:
      $pattern = '#([<\\[])(' . $tags_string . ')((\\s+[^>\\]]*)*)([>\\]])(.*?)(\\1/\\2\\s*\\5|$)#s';
      break;
  }

  // replace the code container tag brackets
  // and prepare the container content (newline and angle bracket protection)
  $text = preg_replace_callback($pattern, create_function('$match', "return _geshifilter_prepare_callback(\$match, {$format});"), $text);
  if (_geshifilter_php_delimeters($format)) {

    // prepare < ?php ... ? > blocks
    $text = preg_replace_callback('#[\\[<](\\?php|\\?PHP|%)(.+?)((\\?|%)[\\]>]|$)#s', '_geshifilter_prepare_php_callback', $text);
  }
  return $text;
}