/** * Helper function for parsing the attributes of GeSHi code tags. * * Get the settings for language, line numbers, etc. * * @param string $attributes * String with the attributes. * * @return array of settings with fields 'language', 'line_numbering', * 'linenumbers_start' and 'title'. */ public function parseAttributes($attributes) { // Initial values. $lang = NULL; $line_numbering = NULL; $linenumbers_start = NULL; $title = 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, array(GeshiFilter::ATTRIBUTE_LINE_NUMBERING, GeshiFilter::ATTRIBUTE_LINE_NUMBERING_START, GeshiFilter::ATTRIBUTE_FANCY_N, GeshiFilter::ATTRIBUTE_TITLE))); $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. 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; } elseif ($att_name == GeshiFilter::ATTRIBUTE_TITLE) { $title = $att_value; } } // Return parsed results. return array('language' => $lang, 'line_numbering' => $line_numbering, 'linenumbers_start' => $linenumbers_start, 'title' => $title); }