Extension:Code
From MediaWiki.org
Code Release status: beta |
|
---|---|
Implementation | Tag |
Description | A syntax highligher using GeSHi |
Author(s) | Paul Grinberg |
Last version | v0.9 (July 18, 2011) |
MediaWiki | 1.17.0+ (and probably much earlier) |
License | GPL |
Download | No link Read changelog |
Check usage (experimental) |
Extension:Code is a syntax highligher using GeSHi.
Contents |
[edit] Installation Instructions
[edit] Step 1
Copy the following code into your extensions directory under Code/Code.php file.
<?php if( !defined( 'MEDIAWIKI' ) ) { echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); die( -1 ); } $wgExtensionCredits['validextensionclass'][] = array( 'path' => __FILE__, 'name' => 'Code', 'version' => '0.9', 'author' => 'Paul Grinberg', 'url' => 'http://www.mediawiki.org/wiki/Extension:Code', 'descriptionmsg' => 'Syntax Highlighting using GeSHi', 'description' => 'Adds <nowiki><Code></nowiki> tag' ); $wgHooks['ParserFirstCallInit'][] = 'efCodeExtensionInit'; function efCodeExtensionInit(Parser &$parser) { $parser->setHook( "Code", "efCodeExtensionRenderCode" ); return true; } function efCodeExtensionRenderCode($input, $argv, $parser) { global $wgShowHideDivi, $wgOut; // default values $language = 'text'; $showLineNumbers = false; $showDownloadLink = false; $source = $input; $tabwidth = 4; foreach ($argv as $key => $value) { switch ($key) { case 'lang': $language = $value; break; case 'linenumbers': $showLineNumbers = true; break; case 'tabwidth': $tabwidth = $value; break; case 'download': $showDownloadLink = true; break; case 'fileurl': $html = $parser->unstrip($parser->recursiveTagParse($value),$parser->mStripState); $i = preg_match('/<a.*?>(.*?)<\/a>/', $html, $matches); $url = $matches[1]; //print("URL is '$url'"); #$source = "file_get_contents disabled! Contact your wiki admin with questions."; $source = file_get_contents($url); break; default : wfDebug( __METHOD__.": Requested '$key ==> $value'\n" ); break; } } if (!defined('GESHI_VERSION')) { include('geshi/geshi.php'); // include only once or else wiki dies } $geshi = new GeSHi($source, $language); $error = $geshi->error(); // die gracefully if errors found if ($error) { return "Code Extension Error: $error"; } $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); // always display line numbers $geshi->set_tab_width($tabwidth); $code = $geshi->parse_code(); $code_pieces = preg_split('/\<ol/', $code ); $output = ''; $ol_tag = '<ol'; if (!$showLineNumbers) { // if not asked to show line numbers, then we should hide them. This is the preferred method // because this allows for a means of a block of code in the middle of a numbered list $output .= "<style type='text/css'><!-- ol.codelinenumbers { list-style: none; margin-left: 0; padding-left: 0em;} --></style>"; $ol_tag = "<ol class='codelinenumbers'"; } $output .= $code_pieces[0]; if ($showDownloadLink) { $output .= "<a href=\"javascript:win3 = window.open('', 'code', 'width=320,height=210,scrollbars=yes');win3.document.writeln('$source');\" style=\"float:right\">Download Code</a>\n"; } $output .= $ol_tag . $code_pieces[1]; return $output; } ?>
[edit] Step 2
Add the following text to your LocalSettings.php:
require_once( "$IP/extensions/Code/Code.php" );
[edit] Usage
The extension has the following usage:
<code [options]>your code</code> where options are: lang='s' This is the language type that explains how to highlight the code. Default is 'text'. See GeSHi for full list of supported syntaxes linenumbers Turn on line numbers for each line in your code download Turn on the "Download Code" link for this block of your code tabwidth='n' How many spaces does a TAB represent fileurl='s' Instead of processing the code provided between the tags, process the code at specified URL
[edit] Revisions
- v0.8 - April 29, 2010 - initial release on MW.
- v0.9 - July 18, 2011 - Updated for MediaWiki 1.17.0.