[Home]WikiPatches/SourceHighlight2

UseModWiki | WikiPatches | RecentChanges | Preferences

Source Highlight Patch 2

See also: /SourceHighlight

Description

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

The patch


--- 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/\&lt;pre\&gt;((.|\n)*?)\&lt;\/pre\&gt;/&StorePre($1, "pre")/ige;
     $pageText =~
+        s/\&lt;code(?:\s+(\w+))?\&gt;((.|\n)*?)\&lt;\/code\&gt;/&StoreCode($2, $1)/ige;
+    $pageText =~
         s/\&lt;code\&gt;((.|\n)*?)\&lt;\/code\&gt;/&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/\&lt;nowiki\&gt;((.|\n)*?)\&lt;\/nowiki\&gt;/&StoreRaw($1)/ige;
     # The <pre> tag wraps the stored text with the HTML <pre> tag
     s/\&lt;pre\&gt;((.|\n)*?)\&lt;\/pre\&gt;/&StorePre($1, "pre")/ige;
+    s/\&lt;code(?:\s+(\w+))?\&gt;((.|\n)*?)\&lt;\/code\&gt;/&StoreCode($2, $1)/ige;
     s/\&lt;code\&gt;((.|\n)*?)\&lt;\/code\&gt;/&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/&lt;/</g;
+  $text =~ s/&gt;/>/g;
+  $text =~ s/&amp;/&/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) = @_;


UngarPeter


 Discussion:
 Q::
 Hi, Ungar
 This Patch seems work fine with GNU sourcehighlight too...
 --JuanmaMP

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited June 11, 2007 2:22 am by JuanmaMP (diff)
Search: