Extension:EmbedHighlightedCodeFromFile

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
EmbedHighlightedCodeFromFile

Release status: experimental

Implementation Tag
Description embed source code from a file on a page, auto syntax highlighted
Last Version 0.1 (2008/06/06)
MediaWiki 1.11
License No license specified
Download see below
Example http://florian-konnertz.de/wiki/ArrayView.php

Contents

[edit] What can this extension do?

embed source code from a file on a page, auto syntax highlighted. It's copied partially from Extension:Embed Document and requires geshi, so I just install Extension:SyntaxHighlight_GeSHi


[edit] Usage

use on your page:

<embed_code>Path/To/File.xyz</embed_code>

in one line. The file has to be below MW main dir. The language for highlighting is found from file suffix. (Tested only with .php)


[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/EmbedHighlightedCodeFromFile.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

Install Extension:SyntaxHighlight_GeSHi

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/EmbedHighlightedCodeFromFile.php");

[edit] Code

$wgExtensionCredits['parserhook'][] = array(
        'name' => 'EmbedHighlightedCodeFromFile',
        'author' => 'groovehunter',
        'version' => '0.1',
        'url' => 'http://www.mediawiki.org/wiki/Extension:EmbedHighlightedCodeFromFile',
        'description' => 'embed source code from a file on a page, auto syntax highlighted',
);
 
$wgExtensionFunctions[] = "register_embed_code_handler";
 
$dir = dirname(__FILE__) . '/';
$geshidir = $dir . 'SyntaxHighlight_GeSHi/geshi/';
include_once($geshidir.'geshi.php');
 
function register_embed_code_handler() {
    global $wgParser;
    $wgParser->setHook( "embed_code", "embed_code_handler" );
}
 
function embed_code_handler( $input, $argv ) {
    $allowedchars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                          'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                          'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                          'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                          '_', '/', '.');
 
    if( str_replace($allowedchars, '', $input) == '' ) {
        if ( file_exists($input) ){
            $path_parts = pathinfo($input);
            $language = $path_parts["extension"];
            $source = file_get_contents($input);
            $geshi =& new GeSHi($source, $language);
            return $geshi->parse_code();  
        }
    } else {
        return "<font color=#aa0000>Error: invalid character sequence between <code>&lt;embed_document&gt;...&lt;/embed_document&gt;</code> markers, allowed
are only<ul><li>a...z</li><li>A...Z</li><li>_</li><li>/</li><li>.</li></ul></font>";
    }
}

[edit] See also

Personal tools