(file) Return to SyntaxHighlightingPlugin.pm CVS log (file) (dir) Up to [RizwankCVS] / geekymedia_web / twiki / lib / TWiki / Plugins

  1 rizwank 1.1 #
  2             # TWiki WikiClone ($wikiversion has version info)
  3             #
  4             # Copyright (C) 2000-2001 Andrea Sterbini, a.sterbini@flashnet.it
  5             # Copyright (C) 2001 Peter Thoeny, Peter@Thoeny.com
  6             #
  7             # This program is free software; you can redistribute it and/or
  8             # modify it under the terms of the GNU General Public License
  9             # as published by the Free Software Foundation; either version 2
 10             # of the License, or (at your option) any later version.
 11             #
 12             # This program is distributed in the hope that it will be useful,
 13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
 14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15             # GNU General Public License for more details, published at 
 16             # http://www.gnu.org/copyleft/gpl.html
 17             #
 18             # =========================
 19             #
 20             # This is the Syntax Highlighting TWiki plugin.
 21             # written by
 22 rizwank 1.1 # Nicolas Tisserand (tisser_n@epita.fr), Nicolas Burrus (burrus_n@epita.fr)
 23             # and Perceval Anichini (anichi_p@epita.fr)
 24             # 
 25             # It uses enscript as syntax highlighter.
 26             # 
 27             # Use it in your twiki text by writing %begin language% ... %end%
 28             # with language = ada asm awk bash c changelog c++ csh delphi diff diffs diffu elisp fortran fortran_pp haskell html idl inf java javascript
 29             # ksh m4 mail makefile maple matlab modula_2 nested nroff objc outline pascal perl postscript python rfc scheme sh skill sql states synopsys
 30             # tcl tcsh tex vba verilog vhdl vrml wmlscript zsh 
 31             
 32             package TWiki::Plugins::SyntaxHighlightingPlugin;
 33             
 34             use IPC::Open2;
 35             
 36             use vars qw(
 37             	    $web $topic $user $installWeb $VERSION $debug
 38             	    );
 39             
 40             $VERSION = '1.000';
 41             
 42             sub pipeThru
 43 rizwank 1.1 {
 44                 my $out;
 45             
 46                 $pid = open2( \*Reader, \*Writer, $_[0]);
 47             
 48                 print Writer $_[1];
 49                 close(Writer);
 50             
 51                 while (<Reader>)
 52                 {
 53             	$out .= $_;
 54                 }
 55                 close (Reader);
 56             
 57                 return $out;
 58             }
 59             
 60             sub highlight
 61             {
 62                 my %langs = (
 63             		 "ada" => "ada",
 64 rizwank 1.1 		 "asm" => "asm",
 65             		 "awk" => "awk",
 66             		 "bash" => "bash",
 67             		 "c" => "c",
 68             		 "changelog" => "changelog",
 69             		 "cpp" => "cpp",
 70             		 "c++" => "cpp",
 71             		 "csh" => "csh",
 72             		 "delphi" => "delphi",
 73             		 "diff" => "diff",
 74             		 "diffs" => "diffs",
 75             		 "diffu" => "diffu",
 76             		 "elisp" => "elisp",
 77             		 "fortran" => "fortran",
 78             		 "fortran_pp" => "fortran_pp",
 79             		 "haskell" => "haskell",
 80             		 "html" => "html",
 81             		 "idl" => "idl",
 82             		 "inf" => "inf",
 83             		 "java" => "java",
 84             		 "javascript" => "javascript",
 85 rizwank 1.1 		 "ksh" => "ksh",
 86             		 "m4" => "m4",
 87             		 "mail" => "mail",
 88             		 "makefile" => "makefile",
 89             		 "maple" => "maple",
 90             		 "matlab" => "matlab",
 91             		 "modula_2" => "modula_2",
 92             		 "nested" => "nested",
 93             		 "nroff" => "nroff",
 94             		 "objc" => "objc",
 95             		 "outline" => "outline",
 96             		 "pascal" => "pascal",
 97             		 "perl" => "perl",
 98             		 "postscript" => "postscript",
 99             		 "python" => "python",
100             		 "rfc" => "rfc",
101             		 "scheme" => "scheme",
102             		 "sh" => "sh",
103             		 "skill" => "skill",
104             		 "sql" => "sql",
105             		 "states" => "states",
106 rizwank 1.1 		 "synopsys" => "synopsys",
107             		 "tcl" => "tcl",
108             		 "tcsh" => "tcsh",
109             		 "tex" => "tex",
110             		 "tiger" => "tiger",
111             		 "vba" => "vba",
112             		 "verilog" => "verilog",
113             		 "vhdl" => "vhdl",
114             		 "vrml" => "vrml",
115             		 "wmlscript" => "wmlscript",
116             		 "zsh" => "zsh"
117             		 );
118                 
119                 return "<font color=\"red\"> Syntax Highlighting: error: $_[0]: undefined language </font>" unless exists $langs{lc($_[0])};
120                 
121                 my ($lang, $text, $nb_option, $nb_start) = @_;
122                 
123                 my $highlighted = pipeThru("/home/rizwank/bin/enscript --color --language=html --highlight=$langs{lc($lang)} -o - -q", $text);
124                 
125                 if ($highlighted =~ s/.*\<PRE\>\n(.*?)\n?\<\/PRE\>.*/$1/os)
126                 {
127 rizwank 1.1 	if ($nb_option eq " numbered")
128             	{
129             	    my $line = ($nb_start eq "") ? 1 : $nb_start;
130             	    $highlighted =~ s/(^.*)/sprintf("<b><font color=\"#000000\">%5d<\/font><\/b>\t%s", $line++, $1)/mgeo
131             	}
132             	return "<table width=\"100%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\"><tr><td bgcolor=\"#FFFFFF\"><pre>".$highlighted."</pre><\/td><\/tr><\/table>";
133                 }	
134                 else
135                 {
136             	return "<font color=\"red\"> Syntax Highlighting: internal error  </font>";
137                 }
138             }
139             
140             # =========================
141             
142             sub initPlugin
143             {
144                 ( $topic, $web, $user, $installWeb ) = @_;
145                 
146                 # check for Plugins.pm versions
147                 if( $TWiki::Plugins::VERSION < 1 ) {
148 rizwank 1.1         &TWiki::Func::writeWarning( "Version mismatch between SyntaxHighlightingPlugin and Plugins.pm" );
149                     return 0;
150                 }
151                 
152                 # Get plugin debug flag
153                 $debug = &TWiki::Func::getPreferencesFlag( "SYNTAXHIGHLIGHTINGPLUGIN_DEBUG" );
154                 
155                 # Plugin correctly initialized
156                 &TWiki::Func::writeDebug( "- TWiki::Plugins::SyntaxHighlightingPlugin::initPlugin( $web.$topic ) is OK" ) if $debug;
157                 return 1;
158             }
159             
160             # =========================
161             
162             sub startRenderingHandler
163             {
164                 &TWiki::Func::writeDebug( "- SyntaxHighlightingPlugin::startRenderingHandler( $_[1] )" ) if $debug;
165             
166                 # matches %begin [numbered][:n] language% ... %end%
167                 $_[0] =~ s/^%begin( numbered)?(?:\:(\d+))? ([^%]*?)%\n(.*?)^%end%$/highlight($3, $4, $1, $2)/megos;
168             }
169 rizwank 1.1 
170             # =========================
171             
172             1;

Rizwan Kassim
Powered by
ViewCVS 0.9.2