function handle_geshi($content, $language) { $g = new GeSHi($content, $language); $g->enable_classes(); $g->set_header_type(GESHI_HEADER_DIV); $g->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5); #$g->set_overall_style('color:black'); $g->set_overall_id('source'); $g->set_code_style('color:black;', "'Courier New', Courier, monospace"); $g->set_line_style('color:#838383;', '', true); return $g->parse_code(); }
public function format_code($text, $post = null) { if (isset($post)) { $code = $text; if (preg_match("/\\[gist: ([0-9]+)\\]/i", $code, $matches)) { return '<script src="http://gist.github.com/' . $matches[1] . '.js"></script>'; } $post->code_unformatted = $post->code; $languages = getLanguages(); $geshi = new GeSHi($code, $languages[$post->language]); if ($geshi->error() !== false) { return "<pre id='geshi_code'>" . $geshi->error() . "</pre>"; } $geshi->set_overall_id('geshi_code'); $return = $geshi->parse_code(); return $return; } else { return "<pre id='geshi_code'>" . $text . "</pre>"; } }
function onParseContentBlock($page, $name, $text, $shortcut) { $output = NULL; if (!empty($name) && !$shortcut) { list($language, $lineNumber, $class, $id) = $this->getHighlightInfo($name); if (!empty($language)) { $geshi = new GeSHi(trim($text), $language); $geshi->set_language_path($this->yellow->config->get("pluginDir") . "/highlight/"); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $geshi->set_overall_class($class); $geshi->set_overall_id($id); $geshi->enable_line_numbers($lineNumber ? GESHI_NORMAL_LINE_NUMBERS : GESHI_NO_LINE_NUMBERS); $geshi->start_line_numbers_at($lineNumber); $geshi->enable_classes(true); $geshi->enable_keyword_links(false); $output = $geshi->parse_code(); $output = preg_replace("#<pre(.*?)>(.+?)</pre>#s", "<pre\$1><code>\$2</code></pre>", $output); } } return $output; }
/** * Loads data for this template */ protected function LoadData() { $commit = $this->GetProject()->GetCommit($this->params['hashbase']); $this->tpl->assign('commit', $commit); $tree = $commit->GetTree(); $this->tpl->assign('tree', $commit->GetTree()); if (!isset($this->params['hash']) && isset($this->params['file'])) { $this->params['hash'] = $tree->PathToHash($this->params['file']); if (empty($this->params['hash'])) { throw new GitPHP_FileNotFoundException($this->params['file']); } } $blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']); if (!empty($this->params['file'])) { $blob->SetPath($this->params['file']); } $blob->SetCommit($commit); $this->tpl->assign('blob', $blob); if ($this->Plain()) { return; } $head = $this->GetProject()->GetHeadCommit(); $this->tpl->assign('head', $head); if ($this->config->GetValue('filemimetype')) { $mimeReader = new GitPHP_FileMimeTypeReader($blob, $this->GetMimeStrategy()); $mimetype = $mimeReader->GetMimeType(true); if ($mimetype == 'image') { $this->tpl->assign('datatag', true); $this->tpl->assign('mime', $mimeReader->GetMimeType()); $this->tpl->assign('data', base64_encode($blob->GetData())); return; } } if ($this->config->GetValue('geshi')) { include_once GITPHP_GESHIDIR . "geshi.php"; if (class_exists('GeSHi')) { $geshi = new GeSHi("", 'php'); if ($geshi) { $lang = GitPHP_Util::GeshiFilenameToLanguage($blob->GetName()); if (empty($lang)) { $lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetName(), '.'), 1)); } if (!empty($lang)) { $geshi->enable_classes(); $geshi->enable_strict_mode(GESHI_MAYBE); $geshi->set_source($blob->GetData()); $geshi->set_language($lang); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->set_overall_id('blobData'); $this->tpl->assign('geshiout', $geshi->parse_code()); $this->tpl->assign('geshicss', $geshi->get_stylesheet()); $this->tpl->assign('geshi', true); return; } } } } $this->tpl->assign('bloblines', $blob->GetData(true)); }
/** * 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; }
/** * Function called by the pre_typography extension hook before the text will be parsed by EE * @param string $str text that will be parsed * @param object $typo Typography object * @param array $prefs Preferences sent to $TYPE->parse_type * @return string text where the code has been stripped and code positions marked with an MD5-ID * @access public * @global $EXT Extension-Object to support multiple calls to the same extension hook * @global $OUT could be used to display errors - it isn't at the moment * @todo Display error using $OUT */ function pre_typography($str, $typo, $prefs) { // we don't need the DB, nor IN, nor DSP // should probably use OUT to display user_error messages global $EXT, $OUT; // here we're doing the actual work if ($EXT->last_call !== FALSE) { // A different extension has run before us $str = $EXT->last_call; } $cache_dir = dirname(__FILE__) . '/' . $this->settings['cache_dir']; $rllen = strlen($this->rlimit); $pos = array(); preg_match_all($this->llimit, $str, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $key => $match) { $pos[$match[1]] = array(); $pos[$match[1]]['match'] = $match[0]; // lang (called type internally for historical reasons) if (!empty($matches[1][$key][0])) { // strip slashes for filesystem security and quotes because the value might be quoted $pos[$match[1]]['type'] = str_replace(array('/', '"', "'"), '', substr($matches[1][$key][0], 5)); } else { $pos[$match[1]]['type'] = NULL; } // strict if (!empty($matches[2][$key][0])) { switch (str_replace(array('"', "'"), '', strtolower(substr($matches[2][$key][0], 7)))) { case 'true': case '1': $pos[$match[1]]['strict'] = TRUE; break; case 'false': case '0': $pos[$match[1]]['strict'] = FALSE; break; default: $pos[$match[1]]['strict'] = NULL; break; } } else { $pos[$match[1]]['strict'] = NULL; } // line $pos[$match[1]]['line'] = !empty($matches[3][$key][0]) ? str_replace(array('"', "'"), '', substr($matches[3][$key][0], 5)) : NULL; // start $pos[$match[1]]['start'] = !empty($matches[4][$key][0]) ? str_replace(array('"', "'"), '', substr($matches[4][$key][0], 6)) : NULL; // keyword_links if (!empty($matches[5][$key][0])) { switch (str_replace(array("'", '"'), '', strtolower(substr($matches[5][$key][0], 14)))) { case 'true': case '1': $pos[$match[1]]['keyword_links'] = TRUE; break; case 'false': case '0': $pos[$match[1]]['keyword_links'] = FALSE; break; default: $pos[$match[1]]['keyword_links'] = NULL; break; } } else { $pos[$match[1]]['keyword_links'] = NULL; } // overall_class $pos[$match[1]]['overall_class'] = !empty($matches[6][$key][0]) ? str_replace(array("'", '"'), '', substr($matches[6][$key][0], 14)) : NULL; // overall_id $pos[$match[1]]['overall_id'] = !empty($matches[7][$key][0]) ? str_replace(array("'", '"'), '', substr($matches[7][$key][0], 11)) : NULL; } // clean variables used in the loop unset($matches, $key, $match); // krsort the array so we can use substr stuff and won't mess future replacements krsort($pos); // Check for the cache dir if (file_exists($cache_dir) && is_dir($cache_dir)) { $cache_dir = realpath($cache_dir) . '/'; if (!is_writable($cache_dir)) { // try to chmod it @chmod($cache_dir, 0777); if (!is_writable($cache_dir)) { // still not writable? display a warning print '<b>Warning</b>: Your <i>' . $this->name . '</i> cache directory <b>' . $cache_dir . '</b> is not writable! This will cause severe performance problems, so I suggest you chmod that dir.'; } } } else { if (!mkdir($cache_dir, 0777)) { print '<b>Warning</b>: Your <i>' . $this->name . '</i> cache directory <b>' . $cache_dir . '</b> could not be created! This will cause severe performance problems, so I suggest you create and chmod that dir.'; } else { // create an index.html so the contents will not be listed. @touch($cache_dir . 'index.html'); } } if (mt_rand(0, 10) == 10) { // on every 10th visit do the garbage collection $cur = time(); $d = dir($cache_dir); while (($f = $d->read()) !== FALSE) { if ($f != 'index.html' && $f[0] != '.') { if ($cur - filemtime($cache_dir . $f) > $this->settings['cache_cutoff']) { // File is older than cutoff, delete it. @unlink($cache_dir . $f); } } } } // loop through the code snippets $i = 0; foreach ($pos as $code_pos => $match) { $error = FALSE; if (($code_end_pos = strpos($str, $this->rlimit, (int) $code_pos + strlen($match['match']))) !== FALSE) { // we have a matching end tag. // make sure cache is regenerated when changing options, too! $md5 = md5(($not_geshified = substr($str, $code_pos + strlen($match['match']), $code_end_pos - $code_pos - strlen($match['match']))) . print_r($match, TRUE) . print_r($this->settings, TRUE)); // check whether we already have this in a cache file if (is_file($cache_dir . $md5) && is_readable($cache_dir . $md5)) { if (is_callable('file_get_contents')) { $geshified = file_get_contents($cache_dir . $md5); // this is for the garbage collection touch($cache_dir . $md5); } else { // screw PHP4! $f = fopen($cache_dir . $md5, 'r'); $geshified = fread($f, filesize($cache_dir . $md5)); fclose($f); touch($cache_dir . $md5); } } else { // no cache so do the GeSHi thing if ($this->settings['geshi_version'] == '1.1') { // use GeSHi 1.1 include_once dirname(__FILE__) . '/geshi-1.1/class.geshi.php'; // highlight code according to type setting, default to setting $geshi = new GeSHi($not_geshified, $match['type'] !== NULL ? $match['type'] : $this->settings['default_type']); $str_error = $geshi->error(); if (empty($str_error)) { // neither line numbers, nor strict mode is supported in GeSHi 1.1 yet. // in fact it does pretty much nothing // there used to be a rather large block of code here, testing wether methods were callable and call them if // that's the case. // parse the code $geshified = $geshi->parseCode(); } else { $error = TRUE; } } else { // use GeSHi 1.0 include_once dirname(__FILE__) . '/geshi-1.0/geshi.php'; // highlight code according to type setting, default to php $geshi = new GeSHi($not_geshified, $match['type'] !== NULL ? $match['type'] : $this->settings['default_type']); $str_error = $geshi->error(); if (empty($str_error)) { // enable line numbers $number_style = !empty($match['line']) ? $match['line'] : $this->settings['default_line']; switch (strtolower(preg_replace('/\\d+/', '', $number_style))) { case 'normal': $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); break; case 'fancy': $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, (int) preg_replace('/[^\\d]*/', '', $number_style)); break; case 'none': $geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS); break; } // set first line number if ($match['start']) { $geshi->start_line_numbers_at($match['start']); } // set strict mode if ($match['strict']) { $geshi->enable_strict_mode(TRUE); } // enable or disable keyword links $geshi->enable_keyword_links((bool) ($match['keyword_links'] !== NULL) ? $match['keyword_links'] : $this->settings['keyword_links']); // set overall class name if ($match['overall_class'] != NULL) { $geshi->set_overall_class($match['overall_class']); } // set overall id if ($match['overall_id'] != NULL) { $geshi->set_overall_id($match['overall_id']); } // set header type switch ($this->settings['geshi_header_type']) { case 'div': $geshi->set_header_type(GESHI_HEADER_DIV); break; case 'pre': $geshi->set_header_type(GESHI_HEADER_PRE); break; case 'pre-valid': $geshi->set_header_type(GESHI_HEADER_PRE_VALID); break; case 'pre-table': $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); break; case 'none': $geshi->set_header_type(GESHI_HEADER_NONE); break; } // set encoding (for legacy reasons afaik) $geshi->set_encoding($this->settings['geshi_encoding']); // parse the source code $geshified = $geshi->parse_code(); } else { $error = TRUE; } } if (!file_exists($cache_dir . $md5) && is_writable($cache_dir) || file_exists($cache_dir . $md5) && is_writable($cache_dir . $md5)) { if (!$error) { // we can write to the cache file if (is_callable('file_put_contents')) { file_put_contents($cache_dir . $md5, $geshified); @chmod($cache_dir . $md5, 0777); } else { // when will you guys finally drop PHP4 support? $f = fopen($cache_dir . $md5, 'w'); fwrite($f, $geshified); fclose($f); @chmod($cache_dir . $md5, 0777); } } } else { // We could ignore that, but for performance reasons better warn the user. print '<b>Warning</b>: Your <i>' . $this->name . '</i> cache directory <b>' . $cache_dir . '</b> is not writable! This will cause severe performance problems, so I suggest you chmod that dir.'; } } // save replacement to cache and mark location with an identifier for later replacement if (!isset($_SESSION['cache']['ext.geshify'])) { $_SESSION['cache']['ext.geshify'] = array(); } if (!$error) { $_SESSION['cache']['ext.geshify'][$md5] = $geshified; $str = substr($str, 0, $code_pos) . $md5 . substr($str, $code_end_pos + $rllen); } } // unset used variables, so we don't get messed up unset($code_pos, $code_end_pos, $md5, $geshified, $not_geshified, $geshi, $match, $ident, $error); } return $str; }
/** * Get a geshi object for this source. This function is for internal use. * @return GeSHI! The geshi object associated with the source. */ protected function getGeSHI() { if (!isset($this->geshi)) { // truncante long lines and limit the number of lines $text = ""; $linesTruncated = array(); $lines = explode("\n", $this->plainSourceCode); $nbOfLines = count($lines); $n = 0; while ($n < $nbOfLines && $n < MAX_NB_OF_LINES) { $line = $lines[$n]; if (strlen($line) > MAX_LINE_LENGTH) { $msg = "line #" . ($n + 1) . " has been truncated to " . MAX_LINE_LENGTH . " characters (out of " . strlen($line) . " characters)\n"; $lines[$n] = substr($lines[$n], 0, MAX_LINE_LENGTH) . "... TRUNCATED"; trigger_error($msg); } $text .= $line . "\n"; $n++; } if (count($linesTruncated)) { $text = "WARNING: Some long lines have been truncated." . "The file might not display correcly\n" . $text; } $text = implode("\n", $lines); if ($nbOfLines > MAX_NB_OF_LINES) { $msg = "\nFILE truncated to " . MAX_NB_OF_LINES . " lines (out of " . $nbOfLines . " lines)\n"; $text .= $msg; trigger_error($msg); } $geshi = new GeSHi(); $geshi->set_source($text); $geshi->set_language($this->geshiLanguage); $geshi->set_overall_id($this->sourceId); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->enable_classes(); $geshi->enable_ids(true); $this->geshi = $geshi; } return $this->geshi; }
/** * LoadData * * Loads data for this template * * @access protected */ protected function LoadData() { $commit = $this->project->GetCommit($this->params['hashbase']); $this->tpl->assign('commit', $commit); if (!isset($this->params['hash']) && isset($this->params['file'])) { $this->params['hash'] = $commit->PathToHash($this->params['file']); } $blob = $this->project->GetBlob($this->params['hash']); if (!empty($this->params['file'])) { $blob->SetPath($this->params['file']); } $blob->SetCommit($commit); $this->tpl->assign('blob', $blob); if (isset($this->params['plain']) && $this->params['plain']) { return; } $head = $this->project->GetHeadCommit(); $this->tpl->assign('head', $head); $this->tpl->assign('tree', $commit->GetTree()); if (GitPHP_Config::GetInstance()->GetValue('filemimetype', true)) { $mime = $blob->FileMime(); if ($mime) { $mimetype = strtok($mime, '/'); if ($mimetype == 'image') { $this->tpl->assign('datatag', true); $this->tpl->assign('mime', $mime); $this->tpl->assign('data', base64_encode($blob->GetData())); return; } } } if (GitPHP_Config::GetInstance()->GetValue('geshi', true)) { include_once GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('geshiroot', 'lib/geshi/')) . "geshi.php"; if (class_exists('GeSHi')) { $geshi = new GeSHi("", 'php'); if ($geshi) { $lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetName(), '.'), 1)); if (!empty($lang)) { $geshi->enable_classes(); $geshi->enable_strict_mode(GESHI_MAYBE); $geshi->set_source($blob->GetData()); $geshi->set_language($lang); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->set_overall_id('blobData'); $this->tpl->assign('geshiout', $geshi->parse_code()); $this->tpl->assign('geshicss', $geshi->get_stylesheet()); $this->tpl->assign('geshi', true); return; } } } } $this->tpl->assign('bloblines', $blob->GetData(true)); }