function onExtra($name) { $output = NULL; if ($name == "header") { if (!$this->yellow->config->get("highlightStylesheetDefault")) { $locationStylesheet = $this->yellow->config->get("serverBase") . $this->yellow->config->get("pluginLocation") . "highlight.css"; $fileNameStylesheet = $this->yellow->config->get("pluginDir") . "highlight.css"; if (is_file($fileNameStylesheet)) { $output = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"{$locationStylesheet}\" />\n"; } } else { $geshi = new GeSHi(); $geshi->set_language_path($this->yellow->config->get("pluginDir") . "/highlight/"); foreach ($geshi->get_supported_languages() as $language) { if ($language == "geshi") { continue; } $geshi->set_language($language); $output .= $geshi->get_stylesheet(false); } $output = "<style type=\"text/css\">\n{$output}</style>"; } } return $output; }
/** * Method highlights single code block. If the language file is overriden * in sfGeshiPlugin/lib/custom directory, it uses it. Otherwise, it uses * default GeSHi language file from its lib. * * @param String $code - code block. * @param String $language - language name of the code block. */ public static function parse_single($code, $language) { $geshi_object = new GeSHi($code, $language); $alternative_directory = GESHI_LANG_ROOT . '../../custom/'; $alternative_file = $alternative_directory . strtolower($language) . '.php'; if (file_exists($alternative_file)) { $geshi_object->set_language_path($alternative_directory); } return $geshi_object->parse_code(); }
/** * Initialise a GeSHi object to format some code, performing * common setup for all our uses of it * * @param string $text * @param string $lang * @return GeSHi */ public static function prepare($text, $lang) { global $wgTitle, $wgOut; self::initialise(); $geshi = new GeSHi($text, $lang); if ($geshi->error() == GESHI_ERROR_NO_SUCH_LANG) { return null; } $geshi->set_encoding('UTF-8'); $geshi->enable_classes(); $geshi->set_overall_class("source-{$lang}"); $geshi->enable_keyword_links(false); // Wikia change start if ($wgTitle instanceof Title && EditPageLayoutHelper::isCodeSyntaxHighlightingEnabled($wgTitle)) { $theme = 'solarized-light'; if (SassUtil::isThemeDark()) { $theme = 'solarized-dark'; } $geshi->set_language_path(GESHI_ROOT . $theme . DIRECTORY_SEPARATOR); $geshi->set_overall_id('theme-' . $theme); $wgOut->addStyle(AssetsManager::getInstance()->getSassCommonURL('extensions/SyntaxHighlight_GeSHi/styles/solarized.scss')); } // Wikia change end return $geshi; }
/** * do_geshi() - Performs GeSHi transforms on XHTML blobs * * @param $blob str - The blob to transform * @param $open str - Opening expression to match * @param $close str - Closing expression to match * @param $lang str - Language file to use * **********************************************************/ function do_geshi($blob, $open = '<pre>', $close = '</pre>', $lang = 'c') { $out = FALSE; $regexp = '|' . $open . '(.*?)' . $close . '|si'; echo $regexp . "\n\n"; while (preg_match($regexp, $blob, $matches)) { $geshi = new GeSHi($matches[1], $lang); $geshi->set_language_path($geshi_lang); $blob_new = $geshi->parse_code(); // Strip annoying final <br /> $blob_new = preg_replace('/\\n <\\/pre>/', '</pre>', $blob_new); // Fix annoying GeSHI-injected attributes $blob_new = preg_replace('/<pre.*>/i', '<pre>', $blob_new); $blob = preg_replace($regexp, $blob_new, $blob, 1); unset($geshi); } return $out; }
/** * Helper function for generating a GeSHi object. * * @param string $source_code * The source code to process. * @param string $language * The language to generate a GeSHi object for. * * @return \GeSHi * Return a Geshi class object. */ public static function geshiFactory($source_code, $language) { $available_languages = GeshiFilter::getAvailableLanguages(); $geshi = new \GeSHi($source_code, $language); $geshi->set_language_path($available_languages[$language]['language_path']); return $geshi; }
$regexp = '|<pre><code>(.*?)<\\/code><\\/pre>|si'; while (preg_match($regexp, $page_body, $matches) > 0) { // Replace 1st match with token $page_body = preg_replace($regexp, $tmp_token, $page_body, 1); $block_new = $matches[1]; // Un-encode ampersand entities $block_new = decode_markdown($block_new); // Replace token with revised string $page_body = preg_replace("|{$tmp_token}|", '<div class="codeblock">' . $block_new . '</div>', $page_body); } // Run GeSHi over code blocks $regexp = '|<div class="codeblock">(.*?)<\\/div>|si'; $language = 'c'; while (preg_match($regexp, $page_body, $matches)) { $geshi = new GeSHi($matches[1], $language); $geshi->set_language_path($geshi_path); $block_new = $geshi->parse_code(); // Strip annoying final newline $block_new = preg_replace('|\\n <\\/pre>|', '</pre>', $block_new); // Remove style attribute (TODO: Research this in GeSHi) $block_new = preg_replace('| style="font-family:monospace;"|', '', $block_new); $page_body = preg_replace($regexp, $block_new, $page_body, 1); unset($geshi); // Clean up } unset($block_new); // Clean up // Apply typographic flourishes $page_body = SmartyPants($page_body); /** * Generate Doxygen Body
/** * List of available languages. * * @return array * An array mapping language code to array with the language path and * full language name. */ public static function getAvailableLanguages() { // Try to get it from cache (database actually). $cache = \Drupal::cache(); $available_languages = $cache->get('geshifilter_available_languages_cache'); if (!$available_languages) { // Not in cache: build the array of available_languages. $geshi_library = libraries_load('geshi'); $available_languages = array(); if ($geshi_library['loaded']) { $dirs = array($geshi_library['library path'] . '/geshi', drupal_get_path('module', 'geshifilter') . '/geshi-extra'); foreach ($dirs as $dir) { foreach (file_scan_directory($dir, '/.[pP][hH][pP]$/i') as $filename => $fileinfo) { // Short name. $name = $fileinfo->name; // Get full name. $geshi = new \GeSHi('', $name); $geshi->set_language_path($dir); $fullname = $geshi->get_language_name(); unset($geshi); // Store. $available_languages[$name] = array('language_path' => $dir, 'fullname' => $fullname); } } ksort($available_languages); // Save array to database. $cache->set('geshifilter_available_languages_cache', $available_languages); } } else { $available_languages = $available_languages->data; } return $available_languages; }