You are here

function _geshifilter_prepare in GeSHi Filter for syntax highlighting 6

Same name and namespace in other branches
  1. 5.2 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 107

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.
  // Replace the code container tag brackets
  // and prepare the container content (newline and angle bracket protection).
  // @todo: make sure that these replacements can be done in series.
  $tag_styles = array_filter(_geshifilter_tag_styles($format));
  if (in_array(GESHIFILTER_BRACKETS_ANGLE, $tag_styles)) {

    // Prepare <foo>..</foo> blocks.
    $pattern = '#(<)(' . $tags_string . ')((\\s+[^>]*)*)(>)(.*?)(</\\2\\s*>|$)#s';
    $text = preg_replace_callback($pattern, create_function('$match', "return _geshifilter_prepare_callback(\$match, {$format});"), $text);
  }
  if (in_array(GESHIFILTER_BRACKETS_SQUARE, $tag_styles)) {

    // Prepare [foo]..[/foo] blocks.
    $pattern = '#((?<!\\[)\\[)(' . $tags_string . ')((\\s+[^\\]]*)*)(\\])(.*?)((?<!\\[)\\[/\\2\\s*\\]|$)#s';
    $text = preg_replace_callback($pattern, create_function('$match', "return _geshifilter_prepare_callback(\$match, {$format});"), $text);
  }
  if (in_array(GESHIFILTER_BRACKETS_DOUBLESQUARE, $tag_styles)) {

    // Prepare [[foo]]..[[/foo]] blocks.
    $pattern = '#(\\[\\[)(' . $tags_string . ')((\\s+[^\\]]*)*)(\\]\\])(.*?)(\\[\\[/\\2\\s*\\]\\]|$)#s';
    $text = preg_replace_callback($pattern, create_function('$match', "return _geshifilter_prepare_callback(\$match, {$format});"), $text);
  }
  if (in_array(GESHIFILTER_BRACKETS_PHPBLOCK, $tag_styles)) {

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