You are here

public function GeshiFilterFilter::parseAttributes in GeSHi Filter for syntax highlighting 8

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

Helper function for parsing the attributes of GeSHi code tags.

Get the settings for language, line numbers, etc.

Parameters

string $attributes: String with the attributes.

Return value

array An array of settings with fields 'language', 'line_numbering', 'linenumbers_start' and 'title'.

2 calls to GeshiFilterFilter::parseAttributes()
GeshiFilterFilter::prepareCallback in src/Plugin/Filter/GeshiFilterFilter.php
Callback_geshifilter_prepare for preparing input text.
GeshiFilterFilter::replaceCallback in src/Plugin/Filter/GeshiFilterFilter.php
Callback for preg_replace_callback.

File

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

Class

GeshiFilterFilter
Provides a base filter for Geshi Filter.

Namespace

Drupal\geshifilter\Plugin\Filter

Code

public function parseAttributes($attributes) {

  // Initial values.
  $lang = NULL;
  $line_numbering = NULL;
  $linenumbers_start = NULL;
  $title = NULL;
  $special_lines = [];
  $class_to_lang = NULL;

  // Get the possible tags and languages.
  list($generic_code_tags, $language_tags, $tag_to_lang) = $this
    ->getTags();
  $language_attributes = GeshiFilter::whitespaceExplode(GeshiFilter::ATTRIBUTES_LANGUAGE);
  $attributes_preg_string = implode('|', array_merge($language_attributes, [
    GeshiFilter::ATTRIBUTE_LINE_NUMBERING,
    GeshiFilter::ATTRIBUTE_LINE_NUMBERING_START,
    GeshiFilter::ATTRIBUTE_FANCY_N,
    GeshiFilter::ATTRIBUTE_TITLE,
    GeshiFilter::ATTRIBUTE_SPECIAL_LINES,
  ]));
  $enabled_languages = GeshiFilter::getEnabledLanguages();

  // Parse $attributes to an array $attribute_matches with:
  // $attribute_matches[0][xx] fully matched string, e.g. 'language="python"'
  // $attribute_matches[1][xx] param name, e.g. 'language'
  // $attribute_matches[2][xx] param value, e.g. 'python'.
  preg_match_all('#(' . $attributes_preg_string . ')="?([^"]*)"?#', $attributes, $attribute_matches);
  foreach ($attribute_matches[1] as $a_key => $att_name) {

    // Get attribute value.
    $att_value = $attribute_matches[2][$a_key];

    // Check for the language attributes.
    $class_to_lang = str_replace('language-', '', $att_value);
    if (in_array($att_name, $language_attributes)) {

      // Try first to map the attribute value to geshi language code.
      if (in_array($att_value, $language_tags)) {
        $att_value = $tag_to_lang[$att_value];
      }

      // Set language if extracted language is an enabled language.
      if (array_key_exists($att_value, $enabled_languages)) {
        $lang = $att_value;
      }
      elseif ($att_name == 'class' && array_key_exists($class_to_lang, $enabled_languages)) {
        $lang = $class_to_lang;
      }
    }
    elseif ($att_name == GeshiFilter::ATTRIBUTE_LINE_NUMBERING) {
      switch (strtolower($att_value)) {
        case "off":
          $line_numbering = 0;
          break;
        case "normal":
          $line_numbering = 1;
          break;
        case "fancy":
          $line_numbering = 5;
          break;
      }
    }
    elseif ($att_name == GeshiFilter::ATTRIBUTE_FANCY_N) {
      $att_value = (int) $att_value;
      if ($att_value >= 2) {
        $line_numbering = $att_value;
      }
    }
    elseif ($att_name == GeshiFilter::ATTRIBUTE_LINE_NUMBERING_START) {
      if ($line_numbering < 1) {
        $line_numbering = 1;
      }
      $linenumbers_start = (int) $att_value;
    }
    elseif ($att_name == GeshiFilter::ATTRIBUTE_TITLE) {
      $title = $att_value;
    }
    elseif ($att_name == GeshiFilter::ATTRIBUTE_SPECIAL_LINES) {
      $special_lines = explode(',', $att_value);
    }
  }

  // Return parsed results.
  return [
    'language' => $lang,
    'line_numbering' => $line_numbering,
    'linenumbers_start' => $linenumbers_start,
    'title' => $title,
    'special_lines' => $special_lines,
  ];
}