/** * Helper function for generating the CSS rules. * * @return string * String with the CSS rules. */ public static function generateLanguagesCssRules() { $output = ''; $geshi_library = libraries_load('geshi'); if ($geshi_library['loaded']) { $languages = GeshiFilter::getAvailableLanguages(); foreach ($languages as $langcode => $language_full_name) { // Create GeSHi object. $geshi = GeshiFilterProcess::geshiFactory('', $langcode); GeshiFilterProcess::overrideGeshiDefaults($geshi, $langcode); // Add CSS rules for current language. $output .= $geshi->get_stylesheet(FALSE) . "\n"; // Release GeSHi object. unset($geshi); } } else { drupal_set_message(t('Error while generating CSS rules: could not load GeSHi library.'), 'error'); } return $output; }
/** * {@inheritdoc} */ public static function overrideGeshiDefaults(\Geshi &$geshi, $langcode) { $config = \Drupal::config('geshifilter.settings'); parent::overrideGeshiDefaults($geshi, $langcode); $geshi->enable_classes(TRUE); $geshi->set_header_type((int) $config->get('code_container', GESHI_HEADER_PRE)); }
/** * Assert function for testing if GeSHi highlighting works. * * @param string $body * The body text of the node. * @param array $check_list * List of items that should be in rendered output (assertRaw). * An item is something like array($source_code, $lang, $line_numbering, * $linenumbers_start, $inline_mode). If $lang is set, GeSHifilter syntax * highlighting is applied to $sourcecode. If $lang is false, $sourcecode is * directly looked for. * @param string $description * Description of the assertion. * @param bool $invert * If assertNoRaw should be used instead of assertRaw. */ protected function assertGeshiFilterHighlighting($body, array $check_list, $description, $invert = FALSE) { // Create a node. $node = array('title' => 'Test for GeShi Filter', 'body' => array(array('value' => $body . "\n" . $this->randomMachineName(100), 'format' => 'geshifilter_text_format')), 'type' => 'geshifilter_content_type'); $this->drupalCreateNode($node); $this->drupalGet('node/' . $this->node); $this->node++; // $format = entity_load('filter_format', 'geshifilter_text_format'); // $filter = $format->filters('geshifilter'); // $format->settings['format'];. foreach ($check_list as $fragment) { list($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode) = $fragment; if ($lang) { // Apply syntax highlighting. $source_code = GeshiFilterProcess::geshiProcess($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode); } if ($invert) { $this->assertNoRaw($source_code, $description); } else { $this->assertRaw($source_code, $description); } } }
/** * Callback for preg_replace_callback. * * Old: _geshifilter_replace_callback($match, $format). * * @param array $match * Elements from array: * - 0: complete matched string. * - 1: tag name. * - 2: tag attributes. * - 3: tag content. * * @return string * Return the string processed by geshi library. */ protected function replaceCallback(array $match) { $complete_match = $match[0]; $tag_name = $match[1]; $tag_attributes = $match[2]; $source_code = $match[3]; // Undo linebreak and escaping from preparation phase. $source_code = Html::decodeEntities($source_code); // Initialize to default settings. $lang = $this->config->get('default_highlighting'); $line_numbering = $this->config->get('default_line_numbering'); $linenumbers_start = 1; $title = NULL; // Determine language based on tag name if possible. list($generic_code_tags, $language_tags, $tag_to_lang) = $this->getTags(); if (in_array(GeshiFilter::BRACKETS_PHPBLOCK, array_filter($this->tagStyles()))) { $language_tags[] = 'questionmarkphp'; $tag_to_lang['questionmarkphp'] = 'php'; } if (isset($tag_to_lang[$tag_name])) { $lang = $tag_to_lang[$tag_name]; } // Get additional settings from the tag attributes. $settings = $this->parseAttributes($tag_attributes); if (isset($settings['language'])) { $lang = $settings['language']; } if (isset($settings['line_numbering'])) { $line_numbering = $settings['line_numbering']; } if (isset($settings['linenumbers_start'])) { $linenumbers_start = $settings['linenumbers_start']; } if (isset($settings['title'])) { $title = $settings['title']; } if ($lang == GeshiFilter::DEFAULT_DONOTHING) { // Do nothing, and return the original. return $complete_match; } if ($lang == GeshiFilter::DEFAULT_PLAINTEXT) { // Use plain text 'highlighting'. $lang = 'text'; } $inline_mode = strpos($source_code, "\n") === FALSE; // Process and return. return GeshiFilterProcess::processSourceCode($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode, $title); }