static function render_code($source, $line_numbers = false, $lang = 'cpp-qt', $class = null) { require_once self::$geshi_file; $geshi = new GeSHi($source, $lang); $geshi->add_keyword_group(42, 'font-weight:bold', false, Yii::app()->db->createCommand()->select('name')->from('class')->queryColumn()); $geshi->set_url_for_keyword_group(42, CHtml::normalizeUrl(array('class/view')) . '/{FNAME}'); if ($line_numbers) { $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); } return $geshi->parse_code(); }
function highlight_syntax($code, $langid) { $value = get_record('problemstatement_programming_language', 'id', $langid); if ($value) { $syntax = $value->geshi; } else { $syntax = ''; } /* switch ($langid) { case '0': $syntax='cpp'; break; case '1': $syntax='delphi'; break; case '2': $syntax='java'; break; case '3': $syntax='python'; break; case '4': $syntax='csharp'; break; }*/ $geshi = new GeSHi($code, $syntax); $geshi->set_header_type(GESHI_HEADER_DIV); // $geshi->enable_classes(true); $geshi->set_overall_style('font-family: monospace;'); $linenumbers = 1; if ($linenumbers) { $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5); $geshi->set_line_style('color:#222;', 'color:#888;'); $geshi->set_overall_style('font-size: 14px;font-family: monospace;', true); } $urls = FALSE; $indentsize = FALSE; $inline = FALSE; if (!$urls) { for ($i = 0; $i < 5; $i++) { $geshi->set_url_for_keyword_group($i, ''); } } if ($indentsize) { $geshi->set_tab_width($indentsize); } $parsed = $geshi->parse_code(); if ($inline) { $parsed = preg_replace('/^<div/', '<span', $parsed); $parsed = preg_replace('/<\\/div>$/', '</span>', $parsed); } //return $geshi->parse_code().$syntax; $lang = get_record('problemstatement_programming_language', 'id', $langid); if (!$lang) { $lang = ''; } $comment = get_string("programwritten", "problemstatement") . $lang->language_name; //get_string("lang_".$langid, "problemstatement"); return $parsed . $comment; }
protected function highlightSourceFile($filename) { if ($this->geshipath) { require_once $this->geshipath . '/geshi.php'; $source = file_get_contents($filename); $geshi = new GeSHi($source, 'php', $this->geshilanguagespath); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->enable_strict_mode(true); $geshi->enable_classes(true); $geshi->set_url_for_keyword_group(3, ''); $html = $geshi->parse_code(); $lines = split("<li>|</li>", $html); // skip first and last line array_pop($lines); array_shift($lines); $lines = array_filter($lines); $lines = array_map(array($this, 'stripDiv'), $lines); return $lines; } else { $lines = file($filename); for ($i = 0; $i < count($lines); $i++) { $line = $lines[$i]; $line = rtrim($line); if (function_exists('mb_convert_encoding')) { $lines[$i] = mb_convert_encoding($line, 'UTF-8'); } else { $lines[$i] = utf8_encode($line); } } return $lines; } }
$geshi->set_methods_style(1, "color: black;", false); //for multi-line comments /**/ $geshi->set_comments_style('MULTI','color: rgb(63,127,95); font-style: code;', false); //for 'import' keyword // $geshi->set_comments_style(2,'color: rgb(127,0,85); font-weight: bold;', false); //for string constants $geshi->set_strings_style('color: rgb(42,0,255);', true); //for links (standard classes, etc.) $geshi->set_link_styles(GESHI_LINK, 'color: #000060;'); $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;'); //for keywords $geshi->set_keyword_group_style(1,'color: rgb(127,0,85); font-weight: bold;', false); $geshi->set_keyword_group_style(2,'color: rgb(127,0,85); font-weight: bold;', false); $geshi->set_keyword_group_style(4,'color: rgb(127,0,85); font-weight: bold;', false); //new keyword group for 'package' // $geshi->add_keyword_group(5, 'color: rgb(127,0,85); font-weight: bold;', true, array('package', 'import')); $geshi->set_header_content('SVNKit API examlpe: '.$fileName); $geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;'); $geshi->set_symbols_highlighting(false); $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;'); $geshi->set_url_for_keyword_group(3,"http://java.sun.com/j2se/1.4.2/docs/api/"); $code = $geshi->parse_code(); echo "<html><body><h1 >".$fileName."</h1>".$code."<div style=\"max-width: 800px; width: 800px;\"><center><small style=\"color: #669999; font-size: 100%; margin: 0px; padding: 0px; margin-top: 0.5em; font-weight: normal;\"><span style=\"font-size: 80%;\">(c) 2004-2007 TMate Software. All rights reserved.</span></small></center></div></body></html>"; ?>