diff -urN SyntaxHighlightingPlugin/data/Plugins/SyntaxHighlightingPlugin.txt SyntaxHighlightingPlugin.2005-04-14/data/Plugins/SyntaxHighlightingPlugin.txt --- SyntaxHighlightingPlugin/data/Plugins/SyntaxHighlightingPlugin.txt 2003-01-22 11:49:22.000000000 +0100 +++ SyntaxHighlightingPlugin.2005-04-14/data/Plugins/SyntaxHighlightingPlugin.txt 2006-05-03 22:54:15.000000000 +0200 @@ -1,5 +1,5 @@ -%META:TOPICINFO{author="tisser_n" date="1043231880" format="1.0" version="1.4"}% -%META:TOPICPARENT{name="TWikiPreferences"}% +%META:TOPICINFO{author="BenHouston" date="1108408251" format="1.0" version="1.1"}% +%META:TOPICPARENT{name="TWikiPreferences"}% ---++ Syntax Highlighting TWiki Plugin The Syntax Highlighting Twiki Plugin is used to emphasize the rendering of your twiki text according to several languages. @@ -58,46 +58,53 @@ * vhdl * vrml * wmlscript + * xml (kind of - emulated as html) * zsh ---+++ Syntax Rules To enable syntax highlighting on some text, just type : -%begin [numbered] language% + +%SYNTAX "language" [numbered]% some text belonging to the language -%end% where language is one of the list above, and numbered an optional keyword triggering the line numbering feature. +%ENDSYNTAX% + +where language is one of the list above, and numbered an optional keyword triggering the line numbering feature. -Notice that the =%begin language%= and =%end%= must be left alone on one single line. +Notice that the =%SYNTAX%= and =%ENDSYNTAX%= tags must be left alone on one single line. ----+++ Example The following text : -%begin c++% + +%SYNTAX{"c++"}% #include int main() { std::cout << "Hello, world." << std::endl; } -%end% +%ENDSYNTAX% + gives : -%begin c++% +%SYNTAX{"c++"}% #include int main() { std::cout << "Hello, world." << std::endl; } -%end% +%ENDSYNTAX% _If you do not see a nice colorized piece of code above this, you should check if the plugin is well installed._ You can also output numbered lines starting at 10 with this text: -%begin numbered:10 sh% + +%SYNTAX{"sh" numbered:10}% #!/bin/sh languages=`enscript --help-highlight | grep 'Name:' | cut -d ' ' -f 2` for l in $languages; do @@ -105,10 +112,11 @@ * $l EOF done -%end% +%ENDSYNTAX% + which outputs: -%begin numbered:10 sh% +%SYNTAX{"sh" numbered:10}% #!/bin/sh languages=`enscript --help-highlight | grep 'Name:' | cut -d ' ' -f 2` for l in $languages; do @@ -116,12 +124,12 @@ * $l EOF done -%end% +%ENDSYNTAX% ---+++ %TOPIC% Settings Plugin settings are stored as preferences variables. To reference -a plugin setting write ==%<plugin>_<setting>%==, i.e. ==%DEFAULTPLUGIN_SHORTDESCRIPTION%== +a plugin setting write ==%<plugin>_<setting>%==, i.e. ==%SYNTAXHIGHLIGHTINGPLUGIN_SHORTDESCRIPTION%== * One line description, is shown in the %TWIKIWEB%.TextFormattingRules topic: * Set SHORTDESCRIPTION = This Plugin is used to emphasize the rendering of your twiki text according to several languages. @@ -129,6 +137,13 @@ * Debug plugin: (See output in =data/debug.txt=) * Set DEBUG = 0 + * Container: Allows you to determine the block container to hold the output from this plugin. Add in any valid xhtml, and include __OUTPUT__ ([underscore][underscore]OUTPUT[underscore][underscore]) to tell the plugin where to throw the formateed output. For example, if you want your code blocks output into a div with a class of =twikiSyntaxHighlightingBlock=, you could use a CONTAINER value of <div class="twikiSyntaxHighlightingBlock">__OUTPUT__</div> + * Set CONTAINER = + + * Enscript: This plugin defaults to settings that assume enscript is on your path and that it is version 1.63. If you have a different version (especially earlier), change these settings. + * Set ENSCRIPT_PATH = + * Set ENSCRIPT_VERSION = + ---+++ Plugin Installation Instructions * Make sure you have GNU enscript 1.6.3 or above installed. @@ -144,16 +159,16 @@ ---++++ Enscript oddities -%begin numbered sh% +%SYNTAX{"sh"}% # youpi -%end% +%ENDSYNTAX% gives -%begin numbered sh% +%SYNTAX{"sh"}% # youpi -%end% +%ENDSYNTAX% Because =enscript= strangely adds an extra line (for closing its emphasizing markers) between the matched <pre> </pre> to its html output. @@ -161,17 +176,19 @@ ---++++ WikiWords on last line -%begin sh% + +%SYNTAX{"sh"}% UnmatchedWikiword :) MatchedWikiWord :( -%end% +%ENDSYNTAX% + gives -%begin sh% +%SYNTAX{"sh"}% UnmatchedWikiword :) MatchedWikiWord :( -%end% +%ENDSYNTAX% Adding a newline at the end of input stops TWiki in his greedy matching, but this workaround is not an acceptable solution. Feel free to find another one .... ---+++ Plugin Info @@ -187,4 +204,5 @@ __Related Topics:__ %TWIKIWEB%.TWikiPreferences, %TWIKIWEB%.TWikiPlugins, EmptyPlugin --- Main.NicolasTisserand - 12 Jul 2002 +-- Main.NicolasTisserand - 12 Jul 2002 + diff -urN SyntaxHighlightingPlugin/lib/TWiki/Plugins/SyntaxHighlightingPlugin.pm SyntaxHighlightingPlugin.2005-04-14/lib/TWiki/Plugins/SyntaxHighlightingPlugin.pm --- SyntaxHighlightingPlugin/lib/TWiki/Plugins/SyntaxHighlightingPlugin.pm 2003-01-22 11:40:02.000000000 +0100 +++ SyntaxHighlightingPlugin.2005-04-14/lib/TWiki/Plugins/SyntaxHighlightingPlugin.pm 2006-05-03 22:51:34.000000000 +0200 @@ -24,14 +24,14 @@ # # It uses enscript as syntax highlighter. # -# Use it in your twiki text by writing %begin language% ... %end% +# Use it in your twiki text by writing %SYNTAX{"language"}% ... %ENDSYNTAX% # with language = ada asm awk bash c changelog c++ csh delphi diff diffs diffu elisp fortran fortran_pp haskell html idl inf java javascript # ksh m4 mail makefile maple matlab modula_2 nested nroff objc outline pascal perl postscript python rfc scheme sh skill sql states synopsys -# tcl tcsh tex vba verilog vhdl vrml wmlscript zsh +# tcl tcsh tex vba verilog vhdl vrml wmlscript xml zsh package TWiki::Plugins::SyntaxHighlightingPlugin; -use IPC::Open2; +use IPC::Run qw(run); use vars qw( $web $topic $user $installWeb $VERSION $debug @@ -39,24 +39,6 @@ $VERSION = '1.000'; -sub pipeThru -{ - my $out; - - $pid = open2( \*Reader, \*Writer, $_[0]); - - print Writer $_[1]; - close(Writer); - - while () - { - $out .= $_; - } - close (Reader); - - return $out; -} - sub highlight { my %langs = ( @@ -113,23 +95,55 @@ "vhdl" => "vhdl", "vrml" => "vrml", "wmlscript" => "wmlscript", + "xml" => "html", "zsh" => "zsh" ); return " Syntax Highlighting: error: $_[0]: undefined language " unless exists $langs{lc($_[0])}; my ($lang, $text, $nb_option, $nb_start) = @_; + $lang = '' if ! defined $lang; + $text = '' if ! defined $text; + $nb_option = '' if ! defined $nb_option; + $nb_start = '' if ! defined $nb_start; + + # Get plugin debug flag + my $enscriptPath = &TWiki::Func::getPreferencesValue( "SYNTAXHIGHLIGHTINGPLUGIN_ENSCRIPT_PATH" ) || "enscript"; + my $enscriptVersion = &TWiki::Func::getPreferencesValue( "SYNTAXHIGHLIGHTINGPLUGIN_ENSCRIPT_VERSION" ) || 1.63; + + my $flag = ($enscriptVersion >= 1.63) ? "--highlight" : "--pretty-print"; + my @cmd = ($enscriptPath, qw(--color --language=html --output - --silent), "$flag=$langs{lc($lang)}"); - my $highlighted = pipeThru("enscript --color --language=html --highlight=$langs{lc($lang)} -o - -q", $text); + my $highlighted = ''; + eval { + run \@cmd, \$text, \$highlighted; + }; + + if ($@) { + return " Syntax Highlighting: internal error: " . $@ . ""; + } + if ($highlighted =~ s/.*\\n(.*?)\n?\<\/PRE\>.*/$1/os) { - if ($nb_option eq " numbered") + if ($nb_option =~ /numbered/i) { my $line = ($nb_start eq "") ? 1 : $nb_start; $highlighted =~ s/(^.*)/sprintf("%5d<\/font><\/b>\t%s", $line++, $1)/mgeo } - return "
".$highlighted."
<\/td><\/tr><\/table>"; + + my $container = &TWiki::Func::getPreferencesValue( "SYNTAXHIGHLIGHTINGPLUGIN_CONTAINER" ) || ''; + if ($container) + { + $container =~ s/<//g; + $container =~ s/__OUTPUT__/
$highlighted<\/pre>/i;
+           return $container; 
+        }
+	else 
+        {
+           return "
".$highlighted."
<\/td><\/tr><\/table>"; + } } else { @@ -163,8 +177,8 @@ { &TWiki::Func::writeDebug( "- SyntaxHighlightingPlugin::startRenderingHandler( $_[1] )" ) if $debug; - # matches %begin [numbered][:n] language% ... %end% - $_[0] =~ s/^%begin( numbered)?(?:\:(\d+))? ([^%]*?)%\n(.*?)^%end%$/highlight($3, $4, $1, $2)/megos; + # matches %SYNTAX{"lang" numbered}% ... %ENDSYNTAX% + $_[0] =~ s/^%SYNTAX\{\s*\"([^"]*)\"\s*\"?(numbered)?\"?\s*(?:\:\s*(\d+))?\s*\}%\s*\n(.*?)^%ENDSYNTAX%\s*$/highlight($1, $4, $2, $3)/megosi; } # =========================