Extension:Enscript

From MediaWiki.org

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

Release status: unknown

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

Contents

[edit] Description

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

[edit] Download and Installation Instructions

  • 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 right 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");

[edit] Usage

<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;

[edit] License

This Piece of code is Under Gnu Public License.

[edit] The extension itself

<?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;
}

?>

Personal tools