sharing about .NET and technology RSS 2.0
 Sunday, February 12, 2006

Here I describe how you can add syntax highlighting (C#, SQL, Javascript, etc.) in MediaWiki. In MediaWiki you can add syntax highlighting through GeSHiHighlight but I found it interesting for combining the world of PHP and .NET. For syntax highlighting in .NET I used the CodeHighlighter ASP.NET Control from Actipro Software.

The intersection between PHP and .NET for communication are web services. Therefore I created a .NET webservice that provides the method Parse with the parameters code and languagekey (= C#, SQL, XMl, etc.)  which returns a HTML string.

using System;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Web.Services.Protocols;
using System.Configuration;
using ActiproSoftware.CodeHighlighter;
using ActiproSoftware.SyntaxEditor;

[WebService(Namespace = "http://tempuri.org/SyntaxHighlighting")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SyntaxHighlighting : System.Web.Services.WebService
{
[WebMethod]
public string Parse(string code, string languageKey)
{
   if (string.IsNullOrEmpty(code))
      return string.Empty;

   SyntaxLanguage language = GetSyntaxLanguage(languageKey);
   if (language == null)
      throw new ApplicationException("LANGKEYNOTEXIST");

   CodeHighlighterEngine engine = new CodeHighlighterEngine();
   engine.OutliningEnabled = false;
   return engine.GenerateHtmlInline(string.Empty, code, language);
}

CodeHighlighterConfiguration GetCodeHighlighterConfig()
{
   CodeHighlighterConfiguration config = (CodeHighlighterConfiguration)HttpContext.Current.Cache["CodeHighlighterConfig"];
   if (config == null)
   {
      config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
      HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
   }

   return config;
}

SyntaxLanguage GetSyntaxLanguage(string languageKey)
{
   if (string.IsNullOrEmpty(languageKey))
      return null;

   CodeHighlighterConfiguration config = GetCodeHighlighterConfig();
   foreach (string key in config.LanguageConfigs.Keys)
      if (key.ToLower() == languageKey.ToLower())
         return CodeHighlighter.GetLanguage(config, key);

   return null;
}
}

In MediaWiki you can extend WikiText so that for example your xml tag is recognized by the parser. This way you can extend the HTML output. In this example you would write in WikiText the following statements:

<code language="C#">
   public int
x = 5;
</code>

In the extensions folder of MediaWiki you add a file called CodeHighlighting.php with the following content:

<?php

$wgExtensionFunctions[] = "wfCodeHighlightingExtension";

function wfCodeHighlightingExtension()
{
   global $wgParser;
   $wgParser->setHook('code', 'renderCode');
}

function renderCode( $input="", $argv=array() )
{
 $result = SyntaxHighlighting($input, $argv['language']);
 return '<pre>' . trim($result) . '</pre>'; 
}

function SyntaxHighlighting($code, $languageKey)

 $location = 'http://localhost/SyntaxHighlightingWS/SyntaxHighlighting.asmx?wsdl';
 $result = $code;
 
 try
 { 
  $client = new SoapClient($location);
  $arr = array("code" => $code, "languageKey" => $languageKey);
  $result = $client->Parse($arr)->ParseResult; 
 }
 catch(SoapFault $exception)
 {  
  if (strpos($exception->faultstring, "LANGKEYNOTEXIST") === false)
  {
   throw $exception;
  }
 }   
 return $result; 
}
?>

Through the setHook method you can extend WikiText. Here the method renderCode is called when a XML element named 'code' is inside your WikiText. Calling a web service in PHP 5 is easy with the SoapClient object. If the language we pass does not exist, we simply return the original string.

One more thing must be done, is to add an include in the LocalSettings.php in your MediaWiki folder.

include("extensions/CodeHighlighting.php");

Finally:

Sunday, February 12, 2006 11:04:22 PM (Romance Standard Time, UTC+01:00)  #    Comments [0] -

Tracked by:
"The power of a markup language" (delarou) [Trackback]
http://www.delarou.name/PermaLink,guid,bc5847f7-7c24-4ca4-9d24-1aacc7527d9f.aspx [Pingback]
"MediaWiki with Syntax Higlighting - a sequel" (Real Developer Network) [Trackback]
http://www.realdn.net/msblog/PermaLink,guid,eab0a281-effd-4e4d-8ebc-270655e386ce... [Pingback]
"zyban side affects" (zyban side affects) [Trackback]
"www.best-vaporizers.com" (www.best-vaporizers.com) [Trackback]
"www.jntah.com" (www.jntah.com) [Trackback]
"downlineincome.com" (downlineincome.com) [Trackback]
"www.pokerplayersusa.com" (www.pokerplayersusa.com) [Trackback]
"www.mommyco.com" (www.mommyco.com) [Trackback]
"www.cannabisvaporizers.com" (www.cannabisvaporizers.com) [Trackback]
"www.delarou.net" (www.delarou.net) [Trackback]
"phenterminedietpill.fugocm.pila.pl" (phenterminedietpill.fugocm.pila.pl) [Trackback]
"www.feminizedmarijuanaseeds.com" (www.feminizedmarijuanaseeds.com) [Trackback]
"www.bewbs.com" (www.bewbs.com) [Trackback]
"www.impact210.com" (www.impact210.com) [Trackback]
"www.thecodingmaster.com" (www.thecodingmaster.com) [Trackback]
"hackgs.com" (hackgs.com) [Trackback]
"www.neptunesbeachclub.com" (www.neptunesbeachclub.com) [Trackback]
"www.conferencecalldirectory.net" (www.conferencecalldirectory.net) [Trackback]
"www.ringtone-center.com" (www.ringtone-center.com) [Trackback]
"www.herbalmarijuanavaporizer.com" (www.herbalmarijuanavaporizer.com) [Trackback]
"www.marijuanavaporizers.net" (www.marijuanavaporizers.net) [Trackback]
"www.herbvaporizers.com" (www.herbvaporizers.com) [Trackback]
"www.marijuanavapor.com" (www.marijuanavapor.com) [Trackback]
"www.vaporizerpipes.com" (www.vaporizerpipes.com) [Trackback]
"www.delarou.net" (www.delarou.net) [Trackback]
"phentermine diet pill" (phentermine diet pill) [Trackback]
"Funny Quotes" (Funny Quotes) [Trackback]
"pc memory upgrade" (pc memory upgrade) [Trackback]
"popcorn recipe ebook" (popcorn recipe ebook) [Trackback]
"generic cialis next day" (generic cialis next day) [Trackback]
"ital" (ital) [Trackback]
"foto scolaretta sex sex" (foto scolaretta sex sex) [Trackback]
"child custody" (child custody) [Trackback]
"crochet pillow edgings" (crochet pillow edgings) [Trackback]
"prom dresses" (prom dresses) [Trackback]
"invisibile ragazze inculate" (invisibile ragazze inculate) [Trackback]
"piu caldo fuoriclasse papa" (piu caldo fuoriclasse papa) [Trackback]
"museo santa giulia brescia" (museo santa giulia brescia) [Trackback]
"rubber stamping sites" (rubber stamping sites) [Trackback]
"dirty disney toons" (dirty disney toons) [Trackback]
"nokia 1110" (nokia 1110) [Trackback]
"Antivirus Software Reviews" (Antivirus Software Reviews) [Trackback]
"construction project checklist" (construction project checklist) [Trackback]
"chicago gay dating" (chicago gay dating) [Trackback]
"car seat covers dog" (car seat covers dog) [Trackback]
"cinema roma" (cinema roma) [Trackback]
"what is my computer hz" (what is my computer hz) [Trackback]
"babel" (babel) [Trackback]
"owner builder homes" (owner builder homes) [Trackback]
"SEIKO KINETIC SKH640" (SEIKO KINETIC SKH640) [Trackback]
"babysitter wanted fairbanks alaska" (babysitter wanted fairbanks alaska) [Trackback]
"foto spogliatoio" (foto spogliatoio) [Trackback]
"lilith" (lilith) [Trackback]
"si te vas mp3" (si te vas mp3) [Trackback]
"girls ugly" (girls ugly) [Trackback]
Navigation
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Christoph De Baene
Sign In
Statistics
Total Posts: 151
This Year: 22
This Month: 1
This Week: 0
Comments: 147
All Content © 2008, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene (delarou)