See also: /SourceHighlight
This patch uses the Text::Highlight perl module. With this patch you can enclose source code snippets between special tags, and have them show up properly highlighted on the page. The html output has the class codefragment. Also, you need to add classes for the highlighted text to your wiki's CSS (see: $StyleSheet). Here's a sample:
PRE.codefragment { border: 1px solid #CCCCCC; background-color: #f5f5f5; margin: 4px 8px 4xp 2px; padding: 4px 6px; } .comment { color: #080; } .string { color: #008; } .number { color: #488; } .key1 { font-weight: bold; color: #00f; } .key2 { color: #c00; } .key3, .key4, .key5, .key6, .key7, .key8 { font-weight: bold; }
The tags are <code language> and </code>. The language can be anything that your version of Text::Highlight supports, e.g. CPP, Perl, Java, etc.
The $SourceHighlight? config variable must be set to 1.
NOTE: Text::Highlight is very fast, but doesn't produce a very high quality output. For better output, use the /SourceHighlight patch instead
--- wiki.pl 2003-09-11 14:21:02.000000000 +0200 +++ SourceHighlight.pl 2005-10-21 11:38:22.000000000 +0200 @@ -53,7 +53,9 @@ @IsbnNames @IsbnPre @IsbnPost $EmailFile $FavIcon $RssDays $UserHeader $UserBody $StartUID $ParseParas $AuthorFooter $UseUpload $AllUpload $UploadDir $UploadUrl $LimitFileUrl $MaintTrimRc $SearchButton - $EditNameLink $UseMetaWiki @ImageSites $BracketImg ); + $EditNameLink $UseMetaWiki @ImageSites $BracketImg + $SourceHighlight + ); # Note: $NotifyDefault is kept because it was a config variable in 0.90 # Other global variables: use vars qw(%Page %Section %Text %InterSite %SaveUrl %SaveNumUrl @@ -89,6 +91,7 @@ $NotFoundPg = ""; # Page for not-found links ("" for blank pg) $EmailFrom = "Wiki"; # Text for "From: " field of email notes. $SendMail = "/usr/sbin/sendmail"; # Full path to sendmail executable +$SourceHighlight = 1; # 1 = enable syntax highlight <code CPP>...</code> $FooterNote = ""; # HTML for bottom of every page $EditNote = ""; # HTML notice above buttons on edit page $MaxPost = 1024 * 210; # Maximum 210K posts (about 200K for pages) @@ -1580,6 +1583,8 @@ $pageText =~ s/\<pre\>((.|\n)*?)\<\/pre\>/&StorePre($1, "pre")/ige; $pageText =~ + s/\<code(?:\s+(\w+))?\>((.|\n)*?)\<\/code\>/&StoreCode($2, $1)/ige; + $pageText =~ s/\<code\>((.|\n)*?)\<\/code\>/&StorePre($1, "code")/ige; $pageText =~ s/((.|\n)+?\n)\s*\n/&ParseParagraph($1)/geo; $pageText =~ s/(.*)<\/p>(.+)$/$1.&ParseParagraph($2)/seo; @@ -1607,6 +1612,7 @@ s/\<nowiki\>((.|\n)*?)\<\/nowiki\>/&StoreRaw($1)/ige; # The <pre> tag wraps the stored text with the HTML <pre> tag s/\<pre\>((.|\n)*?)\<\/pre\>/&StorePre($1, "pre")/ige; + s/\<code(?:\s+(\w+))?\>((.|\n)*?)\<\/code\>/&StoreCode($2, $1)/ige; s/\<code\>((.|\n)*?)\<\/code\>/&StorePre($1, "code")/ige; if ($EarlyRules ne '') { $_ = &EvalLocalRules($EarlyRules, $_, !$useImage); @@ -1894,6 +1900,62 @@ return &StoreRaw("<$tag>" . $html . "</$tag>"); } +sub StoreCode { + my ($text, $lang) = @_; + + unless ($SourceHighlight and defined($lang)) { + return &StorePre($text, 'CODE'); + } + + $lang = 'CPP' if $lang =~ /^cpp$/i; + + $text =~ s/</</g; + $text =~ s/>/>/g; + $text =~ s/&/&/g; + use Text::Highlight 'preload'; + my $th = new Text::Highlight(wrapper => "<pre class=codefragment>%s</pre>\n"); + + return &StoreRaw( $th->highlight(ucfirst($lang), $text) ); +} + sub StoreHref { my ($anchor, $text) = @_;
Discussion: Q:: Hi, Ungar This Patch seems work fine with GNU sourcehighlight too... --JuanmaMP