Extension:Enscript

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Enscript

Release status: unknown

Implementation Tag
Description Syntax highlight code using GNU Enscript
Author(s) Yedidia Klein
Last version 0.2
MediaWiki Tested on 1.8.2.
License GPL
Download Enscript.php
Parameters

lang

Check usage and version matrix

Contents

Description [edit]

This Extension is made for syntax highlighting in color your code in various languages - using GNU Enscript.

Download and Installation Instructions [edit]

  • Make sure enscript is installed on your system.
  • The extension can be downloaded from here : Save as Enscript.php or copied from end of this doc.
  • Save it in your extensions Directory (with write permissions)
  • Edit Enscript.php and set your enscript binary path (line 32) - good chance that the default are OK for you.
$file['enscript'] = "/usr/bin/enscript";
  • Add this line to your LocalSettings.php:
require_once("extensions/Enscript.php");

Usage [edit]

<enscript lang=perl>
#!/usr/bin/perl

print "Hello world";
$c = $a + $b;
</enscript>

will look like:

#!/usr/bin/perl
print "Hello world";
$c = $a + $b;

License [edit]

This Piece of code is Under Gnu Public License.

The extension itself [edit]

<?php
###############################################
# Enscript MediaWiki Extension
# By Yedidia Klein
# Version 0.2
# Feb 07
###############################################
$EnscriptVersion = '0.2';

#----------------------------------------------------------------------------
#    Extension initialization
#----------------------------------------------------------------------------

global $wgExtensionCredits;
$wgExtensionCredits['parserhook'][] = array(
    'name'=>'Enscript',
    'version'=>$EnscriptVersion,
    'author'=>'Yedidia Klein',
    'url'=>'http://www.mediawiki.org/wiki/Extension:enscript',
    'description' => 'Show code highlighted w/ GNU Enscript'
    );

$wgExtensionFunctions[] = "wfEnscript";

function wfEnscript() {
    global $wgParser;

    $wgParser->setHook( "Enscript", "renderEnscript" );
}

function renderEnscript( $input , $argv, &$parser ) {
  $file['enscript'] = "/usr/bin/enscript";
  $file['enscript-options'] = " -q --language=html --color -E".$argv['lang']." -o -";

  $output="";

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w")  // stdout is a pipe that the child will write to
);
$cwd = '/tmp';    
  $ph=proc_open($file['enscript']." ".$file['enscript-options'],$descriptorspec,$pipes,$cwd);
  fwrite($pipes[0],$input);
  fclose($pipes[0]);

  $output=stream_get_contents($pipes[1]);
  fclose($pipes[1]);

  $return_value = proc_close($ph);

  $output = str_replace("<H1>(stdin)</H1>","",$output);
  return $output;
}

?>