I have implemented a Plug-in for Screwturn wiki that allows code highlighting using the
Geshi syntax highlighting PHP module.
This is very much beta at the moment, but you might find it useful.
Email me with suggestions and bug reports.
Features
- supports over 60 languages
- supports line numbering
- supports highlighting important lines
- hyperlinks to keywords (need more definition in csharp, i'm working on it)
You use it in the following way:
<code
>public static void TestMethod()
</code
>produces
public static void TestMethod()
You can use the following arguments within the opening tag:
lang=actionscript, ada, apache, applescript, asm, asp, autoit, bash, blitzbasic, bnf, c, caddcl, cadlisp, cfdg, cfm, cpp-qt, cpp,
csharp, css, c_mac, d, delphi, diff, div, dos, eiffel, fortran, freebasic, gml, groovy, html4strict, idl, ini, inno, io, java, java5, javascript, latex, lisp, lua, matlab, mirc, mpasm, mysql, nsis, objc, ocaml-brief, ocaml, oobas, oracle8, pascal, perl, php-brief, php, plsql, python, qbasic, reg, robots, ruby, sas, scheme, sdlbasic, smalltalk, smarty, sql, tcl, text, thinbasic, tsql, vb, vbnet, vhdl, visualfoxpro, winbatch, xml
numbers=
0 = no line numbers, 1 = line numbers, 2 = fancy line numbers (every 5th line)
And within the code you can use the following tags to emphasise a line:
<BEGIN GeSHi
>important code here
<END GeSHi
>Steps:
1. Download, unzip and put the Geshi code onto your web server somewhere.
Geshi website
2. Create a text file called
getgeshi.php with the following code
<?php
include('geshi.php');
if ( get_magic_quotes_gpc() ) $_POST['source'] = stripslashes($_POST['source']);
$source = $_POST['source'];
$language = $_POST['language'];
$numbers = $_POST['numbers'];
$style = $_POST['style'];
$path = 'geshi/';
$geshi = new GeSHi($source, $language, $path);
##$geshi->set_overall_style('background-color: ' + backcolor, true); ##ffffee;', true); //to match my wiki
$geshi->set_overall_style($style, true);
$geshi->enable_line_numbers($numbers);
echo $geshi->parse_code();
#reset everything
$geshi->set_overall_style("", true);
$geshi->enable_line_numbers(0);
?>3. Download the
ScrewTurn source-code, and make sure you can compile it on your local machine.
4. Create a new project in the solution, add references to System.Web and the ScrewTurn.Wiki.PluginFramework project. Copy the following source code into a class file and compile.
Update the URL in line 33 to point to the php file you save in step 2.using System.
Collections.
Generic;
using System.
Text.
RegularExpressions;
using ScrewTurn.Wiki.PluginFramework;
namespace Aspiring.ScrewTurnWiki.Geshi
{
public class GeshiFormatProvider : IFormatterProvider {
private IHost host;
private string config;
private ComponentInformation info =
new ComponentInformation
("Geshi Code Formatting Plugin 1.0",
"Aspiring Technology",
"http://hookedonlinq.com/");
public bool PerformPhase1 {
get { return true; }
}
public bool PerformPhase2 {
get { return false; }
}
public bool PerformPhase3 {
get { return false; }
}
public string Format(string raw, ContextInformation context, FormattingPhase phase) {
// change these to suit your system
const string geshiUrl = @"http://-yourwiki.com-/geshi/getgeshi.php";
const int tailLength = 7;
const int headLength = 5;
string language = "csharp";
string style = "";
string lineNumbers = "0";
string head = @"\<" + "code" + @".*?\>";
string tail = @"\<\/" + "code" + @"\>";
Regex regex =
new Regex
(head +
".+?" + tail, RegexOptions.
IgnoreCase | RegexOptions.
Singleline);
Match match = regex.Match(raw);
while (match.Success) {
// remove the tail
string code = match.Value.Substring(0, match.Value.Length - tailLength);
// remove the header and extract the arguments
int headEnd = code.IndexOf('>');
string[] args = code.Substring(headLength, headEnd - headLength).Trim().Split(' ');
foreach (string s in args) {
if (s != string.Empty) {
string[] thisArg = s.Split('=');
if (thisArg.Length == 2) {
switch (thisArg[0].ToLower()) {
case "lang" :
case "language" :language = thisArg[1].Trim(); break;
case "numbers" : lineNumbers = thisArg[1].Trim(); break;
case "style" : style = thisArg[1].Trim(); break;
}
}
}
}
code = code.Substring(headEnd+1);
// format the code
raw = raw.Replace(match.Value, GetGeshiCode(context, geshiUrl, code, language, lineNumbers, style));
match = regex.Match(raw);
}
return raw;
}
public void Init(IHost host, string config) {
this.host = host;
this.config = config;
}
public void Shutdown() { }
public ComponentInformation Information {
get { return info; }
}
public string GetGeshiCode(ContextInformation context, string geshiUrl, string source, string language, string lineNumbers, string style)
{
string result = source;
if (string.IsNullOrEmpty(geshiUrl)) return result;
if (string.IsNullOrEmpty(source)) return string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geshiUrl);
try {
byte[] postDataBytes = Encoding.UTF8.GetBytes("source=" + HttpUtility.UrlEncode(source) +
"&language=" + HttpUtility.UrlEncode(language) +
"&numbers=" + HttpUtility.UrlEncode(lineNumbers) +
"&style=" + HttpUtility.UrlEncode(style));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postDataBytes, 0 ,postDataBytes.Length);
requestStream.Close();
// get response and write to console
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
try {
StreamReader responseReader =
new StreamReader
(resp.
GetResponseStream(), Encoding.
UTF8);
result = responseReader.ReadToEnd();
}
finally {
resp.Close();
}
}
catch (Exception e){
// If something goes wrong, we should log an error.
// This will be system specific.
host.LogEntry( String.Format("Failed Geshi Code Formatting for page {0} - {1}", context.Page.Name, e.Message), LogEntryType.Error, this);
}
return result;
}
}
}
5. Upload the compiled DLL plug-in using the admistration area on your ScrewTurn wiki.
You can download the
Pre-compiled DLL and config file. you will need to edit the path to getgeshi.php in the Geshi.dll.config file