You are here

function _geshifilter_parse_attributes in GeSHi Filter for syntax highlighting 5.2

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

Helper function for parsing the attributes of GeSHi code tags to get the settings for language and line numbers.

Parameters

$attributes string with the attributes:

$format the concerning input format:

Return value

array of settings with fields 'language', 'line_numbering', 'linenumbers_start'

2 calls to _geshifilter_parse_attributes()
_geshifilter_prepare_callback in ./geshifilter.pages.inc
_geshifilter_prepare callback 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_replace_callback in ./geshifilter.pages.inc
preg_replace_callback callback

File

./geshifilter.pages.inc, line 13

Code

function _geshifilter_parse_attributes($attributes, $format) {

  // Initial values.
  $lang = NULL;
  $line_numbering = NULL;
  $linenumbers_start = NULL;

  // Get the possible tags and languages.
  list($generic_code_tags, $language_tags, $tag_to_lang) = _geshifilter_get_tags($format);
  $language_attributes = _geshifilter_whitespace_explode(GESHIFILTER_ATTRIBUTES_LANGUAGE);
  $attributes_preg_string = implode('|', array_merge($language_attributes, array(
    GESHIFILTER_ATTRIBUTE_LINE_NUMBERING,
    GESHIFILTER_ATTRIBUTE_LINE_NUMBERING_START,
    GESHIFILTER_ATTRIBUTE_FANCY_N,
  )));
  $enabled_languages = _geshifilter_get_enabled_languages();

  // 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 . ')="?([^\\s"]*)"?#', $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.
    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 == 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;
    }
  }

  // Return parsed results.
  return array(
    'language' => $lang,
    'line_numbering' => $line_numbering,
    'linenumbers_start' => $linenumbers_start,
  );
}