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] -
.NET | ASP.NET | MediaWiki
# Friday, January 27, 2006

MediaWiki is a Wiki software licensed under GPL and written in PHP. Wiki allows users to easily add and edit content in a collaborative way. Wikipedia for example is one of the most popular Wiki's based on MediaWiki.

Updated: 29 January 2006

Below you find the necessary steps for installing MediaWiki.

  1. Download & install PHP 5.1.2 Installer. The installer creates a folder named c:\php
  2. Download PHP 5.1.2 zip package and extract all files in the folder c:\php
  3. Edit the php.ini file that resides in C:\Windows
    1. Change the extension directory: extension_dir = "c:/php/ext/
    2. uncomment mysql extension: extension = php_mysql.dll
  4. Download & install MySQL 4.1.16 (Windows (X86)).
  5. Create a virtual directory under IIS called phpmyadmin (e.g. c:/inetpub/wwwroot/phpmyadmin)
    1. Add the entry index.php in the documents tab.



  6. Download the latest phpMyAdmin and extract all files under the virtual directory of phpmyadmin.
  7. Edit the file config.default.php that resides in the phpmyadmin folder.
    1. Change the root password of MySQL.
  8. Browse to http://localhost/phpmyadmin

  9. Create a virtual directory under IIS called mediawiki (e.g. c:/inetpub/wwwroot/mediawiki)
    1. Add the entry index.php in the documents tab.
  10. Download the latest MediaWiki and extract all files under the virtual directory of mediawiki
  11. Browse to http://localhost/mediawiki & Configure.
    1. Add the line: '$wgEnableUploads = true' in the LocalSettings.php to enable uploads

Troubleshooting:

After installation you will notice that some pages give a warning, namely:

   Undefined index: REQUEST_URI in C:\Inetpub\wwwroot\mediawiki\includes\WebRequest

This is a known issue and can be fixed.

Friday, January 27, 2006 12:54:50 AM (Romance Standard Time, UTC+01:00) -  # -  Comments [2] -
MediaWiki
Navigation
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

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

© Copyright 2010
Christoph De Baene
Sign In
Statistics
Total Posts: 176
This Year: 2
This Month: 0
This Week: 0
Comments: 249
All Content © 2010, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene