function source_highlighter($code) { $source = str_replace(array(">", "<", """, "&"), array(">", "<", "\"", "&"), $code[1]); if (false !== stristr($code[0], "[php]")) { $lang2geshi = "php"; } elseif (false !== stristr($code[0], "[sql]")) { $lang2geshi = "sql"; } elseif (false !== stristr($code[0], "[html]")) { $lang2geshi = "html4strict"; } else { $lang2geshi = "txt"; } $geshi = new GeSHi($source, $lang2geshi); $geshi->set_header_type(GESHI_HEADER_PRE_VALID); $geshi->set_overall_style('font: normal normal 100% monospace; color: #000066;', false); $geshi->set_line_style('color: #003030;', 'font-weight: bold; color: #006060;', true); $geshi->set_code_style('color: #000020;font-family:monospace; font-size:12px;line-height:6px;', true); //$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->enable_classes(false); $geshi->set_link_styles(GESHI_LINK, 'color: #000060;'); $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;'); $return = "<div class=\"codetop\">CODE</div><div class=\"codemain\">\n"; $return .= $geshi->parse_code(); $return .= "\n</div>\n"; return $return; }
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(); }
function source_highlighter($source, $lang2geshi) { require_once 'geshi/geshi.php'; $source = str_replace(array("'", ">", "<", """, "&"), array("'", ">", "<", "\"", "&"), $source); $lang2geshi = $lang2geshi == 'html' ? 'html4strict' : $lang2geshi; $geshi = new GeSHi($source, $lang2geshi); $geshi->set_header_type(GESHI_HEADER_PRE_VALID); $geshi->set_overall_style('font: normal normal 100% monospace; color: #000066;', false); $geshi->set_line_style('color: #003030;', 'font-weight: bold; color: #006060;', true); $geshi->set_code_style('color: #000020;font-family:monospace; font-size:12px;line-height:13px;', true); $geshi->enable_classes(false); $geshi->set_link_styles(GESHI_LINK, 'color: #000060;'); $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;'); $return = "<div class=\"codetop\">Code</div><div class=\"codemain\">\n"; $return .= $geshi->parse_code(); $return .= "\n</div>\n"; return $return; }
/** * User handler for code block * * @param TexyHandlerInvocation handler invocation * @param string block type * @param string text to highlight * @param string language * @param TexyModifier modifier * @return TexyHtml */ function blockHandler($invocation, $blocktype, $content, $lang, $modifier) { if ($blocktype !== 'block/code') { return $invocation->proceed(); } $texy = $invocation->getTexy(); global $geshiPath; if ($lang == 'html') { $lang = 'html4strict'; } $content = Texy::outdent($content); $geshi = new GeSHi($content, $lang, $geshiPath . 'geshi/'); // GeSHi could not find the language if ($geshi->error) { return $invocation->proceed(); } // do syntax-highlighting $geshi->set_encoding('UTF-8'); $geshi->set_header_type(GESHI_HEADER_PRE); $geshi->enable_classes(); $geshi->set_overall_style('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', true); $geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #003030;', 'font-weight: bold; color: #006060;', true); $geshi->set_code_style('color: #000020;', 'color: #000020;'); $geshi->set_link_styles(GESHI_LINK, 'color: #000060;'); $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;'); // save generated stylesheet $texy->styleSheet .= $geshi->get_stylesheet(); $content = $geshi->parse_code(); // check buggy GESHI, it sometimes produce not UTF-8 valid code :-(( $content = iconv('UTF-8', 'UTF-8//IGNORE', $content); // protect output is in HTML $content = $texy->protect($content, Texy::CONTENT_BLOCK); $el = TexyHtml::el(); $el->setText($content); return $el; }
exit("Access outside acceptable path."); } // Check file exists if (!file_exists($path)) { exit("File not found ({$path})."); } // Prepare GeSHi instance $geshi = new GeSHi(); $geshi->set_language('text'); $geshi->load_from_file($path); $geshi->set_header_type(GESHI_HEADER_PRE); $geshi->enable_classes(); $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 10); $geshi->set_overall_style('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', true); $geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #003030;', 'font-weight: bold; color: #006060;', true); $geshi->set_code_style('color: #000020;', 'color: #000020;'); $geshi->set_link_styles(GESHI_LINK, 'color: #000060;'); $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;'); $geshi->set_header_content('Source code viewer - ' . $path . ' - ' . $geshi->get_language_name()); $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_footer_content('Parsed in <TIME> seconds, using GeSHi <VERSION>'); $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;'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Source code viewer - <?php echo $path; ?> - <?php
/** * Prepare a CSS snippet suitable for use as a ParserOutput/OutputPage * head item * * @param GeSHi $geshi * @return string */ public static function buildHeadItem($geshi) { global $wgUseSiteCss, $wgSquidMaxage; // begin Wikia change // VOLDEV-85 // backporting core fix to monobook font size /** * GeSHi comes by default with a font-family set to monospace, which * causes the font-size to be smaller than one would expect. * We append a CSS hack to the default GeSHi styles: specifying 'monospace' * twice "resets" the browser font-size specified for monospace. * * The hack is documented in MediaWiki core under * docs/uidesign/monospace.html and in bug 33496. */ // Preserve default since we don't want to override the other style // properties set by geshi (padding, font-size, vertical-align etc.) $geshi->set_code_style('font-family: monospace, monospace;', true); // No need to preserve default (which is just "font-family: monospace;") // outputting both is unnecessary $geshi->set_overall_style('font-family: monospace, monospace;', false); // end Wikia change $lang = $geshi->language; $css = array(); $css[] = '<style type="text/css">/*<![CDATA[*/'; $css[] = ".source-{$lang} {line-height: normal;}"; $css[] = ".source-{$lang} li, .source-{$lang} pre {"; $css[] = "\tline-height: normal; border: 0px none white;"; $css[] = "}"; $css[] = $geshi->get_stylesheet(false); $css[] = '/*]]>*/'; $css[] = '</style>'; return implode("\n", $css); }
/** * 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 $wgSyntaxHighlightKeywordLinks; 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($wgSyntaxHighlightKeywordLinks); // If the source code is over 100 kB, disable higlighting of symbols. // If over 200 kB, disable highlighting of strings too. $bytes = strlen($text); if ($bytes > 102400) { $geshi->set_symbols_highlighting(false); if ($bytes > 204800) { $geshi->set_strings_highlighting(false); } } /** * GeSHi comes by default with a font-family set to monospace, which * causes the font-size to be smaller than one would expect. * We append a CSS hack to the default GeSHi styles: specifying 'monospace' * twice "resets" the browser font-size specified for monospace. * * The hack is documented in MediaWiki core under * docs/uidesign/monospace.html and in bug 33496. */ // Preserve default since we don't want to override the other style // properties set by geshi (padding, font-size, vertical-align etc.) $geshi->set_code_style('font-family: monospace, monospace;', true); // No need to preserve default (which is just "font-family: monospace;") // outputting both is unnecessary $geshi->set_overall_style('font-family: monospace, monospace;', false); return $geshi; }
/** * Prepare a CSS snippet suitable for use as a ParserOutput/OutputPage * head item * * @param GeSHi $geshi * @return string */ public static function buildHeadItem( $geshi ) { /** * Geshi comes by default with a font-family set to monospace which * ends ultimately ends up causing the font-size to be smaller than * one would expect (causing bug 26204). * We append to the default geshi style a CSS hack which is to specify * monospace twice which "reset" the browser font-size specified for monospace. * * The hack is documented in MediaWiki core under * docs/uidesign/monospace.html and in bug 33496. */ $geshi->set_code_style( 'font-family: monospace, monospace;', /** preserve defaults */ true ); $lang = $geshi->language; $css = array(); $css[] = '<style type="text/css">/*<![CDATA[*/'; $css[] = ".source-$lang {line-height: normal;}"; $css[] = ".source-$lang li, .source-$lang pre {"; $css[] = "\tline-height: normal; border: 0px none white;"; $css[] = "}"; $css[] = $geshi->get_stylesheet( false ); $css[] = '/*]]>*/'; $css[] = '</style>'; return implode( "\n", $css ); }
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5); // Set the style for the PRE around the code. The line numbers are contained within this box (not // XHTML compliant btw, but if you are liberally minded about these things then you'll appreciate // the reduced source output). $geshi->set_overall_style('font: normal normal 90% monospace; color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', false); // Set the style for line numbers. In order to get style for line numbers working, the <li> element // is being styled. This means that the code on the line will also be styled, and most of the time // you don't want this. So the set_code_style reverts styles for the line (by using a <div> on the line). // So the source output looks like this: // // <pre style="[set_overall_style styles]"><ol> // <li style="[set_line_style styles]"><div style="[set_code_style styles]>...</div></li> // ... // </ol></pre> $geshi->set_line_style('color: #003030;', 'font-weight: bold; color: #006060;', true); $geshi->set_code_style('color: #000020;', true); // Styles for hyperlinks in the code. GESHI_LINK for default styles, GESHI_HOVER for hover style etc... // note that classes must be enabled for this to work. $geshi->set_link_styles(GESHI_LINK, 'color: #000060;'); $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;'); // Use the header/footer functionality. This puts a div with content within the PRE element, so it is // affected by the styles set by set_overall_style. So if the PRE has a border then the header/footer will // appear inside it. $geshi->set_header_content('<SPEED> <TIME> GeSHi © 2004-2007, Nigel McNie, 2007-2008 Benny Baumann. View source of example.php for example of using GeSHi'); $geshi->set_header_content_style('font-family: sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;'); // You can use <TIME> and <VERSION> as placeholders $geshi->set_footer_content('Parsed in <TIME> seconds at <SPEED>, using GeSHi <VERSION>'); $geshi->set_footer_content_style('font-family: sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;'); } else { // make sure we don't preselect any language $_POST['language'] = null;
/** * Style language * * @param array $subjects */ function _styleSubjects($subjects) { // set overall style if (isset($subjects['view'])) { $this->geshi->set_overall_style($subjects['view'], $this->_invertOverwrite($subjects['view.']['overwrite'])); } if (isset($subjects['view.']['container'])) { switch ($subjects['view.']['container']) { case 'none': case 'NONE': case 'None': $this->geshi->set_header_type(GESHI_HEADER_NONE); break; case 'div': case 'Div': case 'DIV': $this->geshi->set_header_type(GESHI_HEADER_DIV); break; case 'pre': case 'Pre': case 'PRE': default: $this->geshi->set_header_type(GESHI_HEADER_PRE); break; } } if (isset($subjects['view.']['tabwidth'])) { $this->geshi->set_tab_width(intval($subjects['view.']['tabwidth'])); } // configure linenumbers if (isset($subjects['linenumbers'])) { $this->geshi->set_line_style($subjects['linenumbers'], isset($subjects['linenumbers.']['fancy']) ? $subjects['linenumbers.']['fancy'] : '', $this->_invertOverwrite($subjects['linenumbers.']['overwrite'])); } // enable / disable linenumbers if (isset($subjects['linenumbers.']['enable'])) { $this->geshi->enable_line_numbers($subjects['linenumbers.']['enable']); } // configure code style if (isset($subjects['code'])) { $this->geshi->set_code_style($subjects['code'], $this->_invertOverwrite($subjects['code.']['overwrite'])); } // configure escape if (isset($subjects['escape'])) { $this->geshi->set_escape_characters_style($subjects['escape'], $this->_invertOverwrite($subjects['escape.']['overwrite'])); } // configure symbols if (isset($subjects['symbols'])) { $this->geshi->set_symbols_style($subjects['symbols'], $this->_invertOverwrite($subjects['symbols.']['overwrite'])); } // configure strings if (isset($subjects['strings'])) { $this->geshi->set_strings_style($subjects['strings'], $this->_invertOverwrite($subjects['strings.']['overwrite'])); } // configure numbers if (isset($subjects['numbers'])) { $this->geshi->set_numbers_style($subjects['numbers'], $this->_invertOverwrite($subjects['numbers.']['overwrite'])); } // configure comment style if (isset($subjects['comments.'])) { foreach ($subjects['comments.'] as $key => $value) { if (strstr($key, '.') == false) { $this->geshi->set_comments_style($key, $value, $this->_invertOverwrite($subjects['comments.'][$key . '.']['overwrite'])); } } } // configure keywords style if (isset($subjects['keywords.'])) { foreach ($subjects['keywords.'] as $key => $value) { if (strstr($key, '.') == false) { $this->geshi->set_keyword_group_style($key, $value, $this->_invertOverwrite($subjects['keywords.'][$key . '.']['overwrite'])); } } } // enable / disable keyword links if (isset($subjects['keyword.']['links.']['enable'])) { $this->geshi->enable_keyword_links($subjects['keyword.']['links.']['enable']); } // configure keyword link styles if (isset($subjects['keyword.']['links'])) { $this->geshi->set_link_styles(GESHI_LINK, $subjects['keyword.']['links']); } if (isset($subjects['keyword.']['links.']['hover'])) { $this->geshi->set_link_styles(GESHI_HOVER, $subjects['keyword.']['links.']['hover']); } if (isset($subjects['keyword.']['links.']['active'])) { $this->geshi->set_link_styles(GESHI_ACTIVE, $subjects['keyword.']['links.']['active']); } if (isset($subjects['keyword.']['links.']['visited'])) { $this->geshi->set_link_styles(GESHI_VISITED, $subjects['keyword.']['links.']['visited']); } // configure keyword link target if (isset($subjects['keyword.']['links.']['target'])) { $this->geshi->set_link_target($subjects['keyword.']['links.']['target']); } // configure method styles if (isset($subjects['methods.'])) { foreach ($subjects['methods.'] as $key => $value) { if (strstr($key, '.') == false) { $this->geshi->set_methods_style($key, $value, $this->_invertOverwrite($subjects['methods.'][$key . '.']['overwrite'])); } } } }
<?php /* This file is part of the Pastebin package. Copyright (c) 2003-2008, Stephen Olesen All rights reserved. More information is available at http://pastebin.ca/ */ ### Generate CSS for all the GeSHi languages $langs = array('actionscript', 'ada', 'apache', 'asm', 'asp', 'asterisk-conf', 'asterisk-exten', 'bash', 'c', 'c_mac', 'caddcl', 'cadlisp', 'cpp', 'csharp', 'css', 'delphi', 'html4strict', 'java', 'javascript', 'lisp', 'lua', 'mpasm', 'nsis', 'objc', 'oobas', 'oracle8', 'pascal', 'perl', 'php-brief', 'php', 'python', 'qbasic', 'smarty', 'sql', 'vb', 'vbnet', 'visualfoxpro', 'xml'); require_once "geshi.php"; foreach ($langs as $v) { $g = new GeSHi("...", $v); $g->enable_classes(); $g->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5); $g->set_header_type(GESHI_HEADER_DIV); #$g->set_overall_id('source'); #$g->set_overall_class($v); $g->set_code_style('color:black;', "'Courier New', Courier, monospace"); $g->set_line_style('color:#838383;', '', true); $fd = fopen("css/lang/{$v}.css", "w"); fwrite($fd, $g->get_stylesheet(false)); fclose($fd); }
function wp_geshi_highlight_and_generate_css() { global $wp_geshi_codesnipmatch_arrays; global $wp_geshi_css_code; global $wp_geshi_highlighted_matches; global $wp_geshi_requested_css_files; global $wp_geshi_used_languages; // It is time to initialize the highlighting machinery. // Check for `class_exists('GeSHi')` for preventing // `Cannot redeclare class GeSHi` errors. Another plugin may already have // included its own version of GeSHi. // TODO: in this case, include GeSHi of WP-GeSHi-Highlight anyway, via // namespacing or class renaming. if (!class_exists('GeSHi')) { include_once "geshi/geshi.php"; } $wp_geshi_css_code = ""; foreach ($wp_geshi_codesnipmatch_arrays as $match_index => $match) { // Process match details. The array structure is explained in // a comment to function `wp_geshi_filter_replace_code()`. $language = strtolower(trim($match[1])); $line = trim($match[2]); $escaped = trim($match[3]); $cssfile = trim($match[4]); $code = wp_geshi_code_trim($match[5]); if ($escaped == "true") { $code = htmlspecialchars_decode($code); } // (C) Ryan McGeary // Set up GeSHi. $geshi = new GeSHi($code, $language); // Output CSS code / do *not* create inline styles. $geshi->enable_classes(); // Disable keyword links. $geshi->enable_keyword_links(false); if ($line) { $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->start_line_numbers_at($line); } // Set the output type. Reference: // http://qbnz.com/highlighter/geshi-doc.html#the-code-container $geshi->set_header_type(GESHI_HEADER_PRE_VALID); // By default, geshi sets font size to 1em and line height to 1.2em. // That does not fit many modern CSS architectures. Make this // relative and, most importantly, customizable. $geshi->set_code_style(''); // If the current language has not been processed in a previous // iteration: // - create CSS code for this language // - append this to the `$wp_geshi_css_code string`. // $geshi->get_stylesheet(false) disables the economy mode, i.e. // this will return the full CSS code for the given language. // This allows for reusing the same CSS code for multiple code // blocks of the same language. if (!in_array($language, $wp_geshi_used_languages)) { $wp_geshi_used_languages[] = $language; $wp_geshi_css_code .= $geshi->get_stylesheet(false); } $output = ""; // cssfile "none" means no wrapping divs at all. if ($cssfile != "none") { if (empty($cssfile)) { // For this code snippet the default css file is required. $cssfile = "wp-geshi-highlight"; } // Append "the css file" to the array. $wp_geshi_requested_css_files[] = $cssfile; $output .= "\n\n" . '<div class="' . $cssfile . '-wrap5">' . '<div class="' . $cssfile . '-wrap4">' . '<div class="' . $cssfile . '-wrap3">' . '<div class="' . $cssfile . '-wrap2">' . '<div class="' . $cssfile . '-wrap">' . '<div class="' . $cssfile . '">'; } // Create highlighted HTML code. $output .= $geshi->parse_code(); if ($cssfile != "none") { $output .= '</div></div></div></div></div></div>' . "\n\n"; } // Store highlighted HTML code for later usage. $wp_geshi_highlighted_matches[$match_index] = $output; } // At this point, all code snippets are parsed. Highlighted code is stored. // CSS code has been generated. Delete what is not required anymore. unset($wp_geshi_codesnipmatch_arrays); }
function wp_geshi_highlight_and_generate_css() { global $wp_geshi_codesnipmatch_arrays; global $wp_geshi_css_code; global $wp_geshi_highlighted_matches; global $wp_geshi_requested_css_files; global $wp_geshi_used_languages; // When we're here, code was found. // Time to initialize the highlighting machine... // Check for `class_exists('GeSHi')` for preventing // `Cannot redeclare class GeSHi` errors. Another plugin may already have // included its own version of GeSHi. // TODO: in this case, include GeSHi of WP-GeSHi-Highlight anyway via using // namespaces or class renaming. if (!class_exists('GeSHi')) { include_once "geshi/geshi.php"; } $wp_geshi_css_code = ""; foreach ($wp_geshi_codesnipmatch_arrays as $match_index => $match) { // Process the match details. The correspondence is explained in // function `wp_geshi_filter_replace_code()`. $language = strtolower(trim($match[1])); $line = trim($match[2]); $escaped = trim($match[3]); $cssfile = trim($match[4]); $code = wp_geshi_code_trim($match[5]); if ($escaped == "true") { $code = htmlspecialchars_decode($code); } // (C) Ryan McGeary // Set up GeSHi. $geshi = new GeSHi($code, $language); // Prepare GeSHi to output CSS code and to prohibit inline styles. $geshi->enable_classes(); // Disable keyword links. $geshi->enable_keyword_links(false); // Process the line number option given by user. if ($line) { $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->start_line_numbers_at($line); } // Set the output code type. $geshi->set_header_type(GESHI_HEADER_PRE_VALID); // By default, geshi sets font size to 1em and line height to 1.2em. // That does not fit to many modern layouts, make this relative, and // most important, make it customizable from outside. $geshi->set_code_style(''); // Append the CSS code to the CSS code string if this is the first // occurrence of the language. $geshi->get_stylesheet(false) // disables the economy mode, i.e. this will return the full CSS // code for the given language. This makes it much easier to use the // same CSS code for several code blocks of the same language. if (!in_array($language, $wp_geshi_used_languages)) { $wp_geshi_used_languages[] = $language; $wp_geshi_css_code .= $geshi->get_stylesheet(false); } $output = ""; // cssfile "none" means no wrapping divs at all. if ($cssfile != "none") { if (empty($cssfile)) { // For this code snippet the default css file is required. $cssfile = "wp-geshi-highlight"; } // Append "the css file" to the array. $wp_geshi_requested_css_files[] = $cssfile; $output .= "\n\n" . '<div class="' . $cssfile . '-wrap5">' . '<div class="' . $cssfile . '-wrap4">' . '<div class="' . $cssfile . '-wrap3">' . '<div class="' . $cssfile . '-wrap2">' . '<div class="' . $cssfile . '-wrap">' . '<div class="' . $cssfile . '">'; } $output .= $geshi->parse_code(); if ($cssfile != "none") { $output .= '</div></div></div></div></div></div>' . "\n\n"; } $wp_geshi_highlighted_matches[$match_index] = $output; } // At this point, all code snippets are parsed. Highlighted code is stored. // CSS code is generated. Delete variables that are not required anymore. unset($wp_geshi_codesnipmatch_arrays); }