You are here

public function GeshiFilterFilter::prepareCallback in GeSHi Filter for syntax highlighting 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/GeshiFilterFilter.php \Drupal\geshifilter\Plugin\Filter\GeshiFilterFilter::prepareCallback()

Callback_geshifilter_prepare for preparing input text.

Replaces the code tags brackets with geshifilter specific ones to prevent possible messing up by other filters, e.g. '[python]foo[/python]' to '[geshifilter-python]foo[/geshifilter-python]'. Replaces newlines with "
" to prevent issues with the line break filter Escapes the tricky characters like angle brackets with SafeMarkup::checkPlain() to prevent messing up by other filters like the HTML filter.

Parameters

array $match: An array with the pieces from matched string.

  • 0: complete matched string.
  • 1: opening bracket ('<' or '[').
  • 2: tag.
  • 3: and.
  • 4: attributes.
  • 5: closing bracket.
  • 6: source code.
  • 7: closing tag.

Return value

string Return escaped code block.

File

src/Plugin/Filter/GeshiFilterFilter.php, line 896

Class

GeshiFilterFilter
Provides a base filter for Geshi Filter.

Namespace

Drupal\geshifilter\Plugin\Filter

Code

public function prepareCallback(array $match) {
  $tag_name = $match[2];
  $tag_attributes = $match[3];
  $content = $match[6];

  // Get the default highlighting mode.
  $lang = $this->config
    ->get('default_highlighting');
  if ($lang == GeshiFilter::DEFAULT_DONOTHING) {

    // If the default highlighting mode is GeshiFilter::DEFAULT_DONOTHING
    // and there is no language set (with language tag or language attribute),
    // we should not do any escaping in this prepare phase,
    // so that other filters can do their thing.
    $enabled_languages = GeshiFilter::getEnabledLanguages();

    // Usage of language tag?
    list($generic_code_tags, $language_tags, $tag_to_lang) = $this
      ->getTags();
    if (isset($tag_to_lang[$tag_name]) && isset($enabled_languages[$tag_to_lang[$tag_name]])) {
      $lang = $tag_to_lang[$tag_name];
    }
    else {

      // Get additional settings from the tag attributes.
      $settings = $this
        ->parseAttributes($tag_attributes);
      if ($settings['language'] && isset($enabled_languages[$settings['language']])) {
        $lang = $settings['language'];
      }
    }

    // If no language was set: prevent escaping and return original string.
    if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
      return $match[0];
    }
  }
  if ($this
    ->decodeEntities()) {
    $content = $this
      ->unencode($content);
  }

  // Return escaped code block.
  return '[geshifilter-' . $tag_name . $tag_attributes . ']' . str_replace([
    "\r",
    "\n",
  ], [
    '',
    '&#10;',
  ], Html::escape($content)) . '[/geshifilter-' . $tag_name . ']';
}