Extension:SyntaxHighlight GeSHi extension
From MediaWiki.org
| WARNING: the code or configuration described here poses a major security risk.
Problem: makes it possible to read sensitive system files (like |
|
Release status: unknown |
|
|---|---|
| Implementation | Tag |
| Description | Extends the function of SyntaxHighlight GeSHi with a file/url as a file source. No more Copy-Pasting of code. |
| Author(s) | User:Sonnygauran |
| License | No license specified |
| Download | no link |
|
check usage (experimental) |
|
Contents |
[edit] Extending SyntaxHighlight_GeSHi
I've made some tweaking with the source code, i hope anybody can give any reaction to this. With these modifications, the extension can also be used to load from files, a subversion repository, and pass it through the highlighter in one swoop.
The idea came when I tried mashing the MediaWiki Include by Noah Spurrier and SyntaxHighlight GeSHi with the following code:
<source lang="php"> <include src="/programming/php/tools/geshi/geshi.php" /> </source>
Sadly, this didn't work as I would have guessed...below is the literal output of the above line:
<include src="/programming/php/tools/geshi/geshi.php" />
[edit] Usage
After doing all these, you should now be able to pull any file accessible and fetch from subversion repositories. A preview on how to achieve this can be found here. The prototype would now be:
<source lang="language" [src=source]>text</source>
- where
- language: Defines the language
- source: (Optional) Defines where the source code will come from
-
- where values are:
- text: (Default) source code from inside the source tag
- file: url from inside an accessible file
- svn: url from a subversion repository
-
- text: Plain text, full path with file, or a valid svn url.
These also include the default parameters of SyntaxHighlight_GeSHi.
To fetch a file from a directory, use
<source lang="java" src="file">/programming/java/Something.java</source>
To fetch through svn,
<source lang="java" src="svn"> http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.php </source>
This is useful for documentation. You don't have to copy-paste codes inside wikis, as well as keeping track of file changes are handled automagically by PHP.
[edit] Prerequisites
The following are required to be able to achieve this mash-up:
- MediaWiki (of course. :p)
- GeSHi
- SyntaxHighlight GeSHi
[edit] Procedure
As for my recent immersion with wiki's and my attempt on including a source code and passing it to GeSHi, I ended up modifying SyntaxHighlight_GeSHi.class.php, SyntaxHighlight_GeSHi.i18n.php, and with the idea from include.php. Here are the steps:
[edit] SyntaxHighlight_GeSHi.class.php
Basically, were
- adding an attribute src to the source tag and
- making sure the source tag knows what to do with it.
[edit] 1. Prepare the src attribute
Inside the function:
public static function parserHook( $text, $args = array(), $parser ) { // .. // snip // .. // from: // line 34: $geshi = self::prepare( $text, $lang ); // // change it to: $geshi = self::prepare( $text, $lang , $args['src']); // .. // snip // .. }
This enables us to use the src attribute for the source tag.
[edit] 2. Modify the prepare function
| original | modified |
|---|---|
private static function prepare( $text, $lang ) { 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 ); return $geshi; } |
private static function prepare($input, $lang, $source){ self::initialise(); switch ($source){ case 'file': if (file_exists($input)) $output = file_get_contents($input); if ($output === False) $output = ''; break; case 'svn': exec ("svn cat $input", $output, $return_var); if ($return_var != 0) $output = ''; else $output = join("\n", $output); break; default: $output = $input; break; } $geshi = new GeSHi( $output, $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 ); return $geshi; } |
[edit] SyntaxHighlight_GeSHi.i18n.php
Just add something to a localization temporarily. Something like:
'en' => array( // .. // snip // .. 'syntaxhighlight-err-errsource' => 'File does not exist or it cannot be accessed.', // .. // snip // .. ),
Although I have added a line for the english language section, I was not able to come up with a result as expected. I end-up with a result of GESHI_ERROR_NO_SUCH_LANG.
--Sonnygauran 21:53, 21 August 2007 (UTC)