Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »
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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using ScrewTurn.Wiki.PluginFramework;
  6. using System.Web;
  7. using System.Net;
  8. using System.IO;
  9.  
  10. namespace Aspiring.ScrewTurnWiki.Geshi
  11. {
  12. public class GeshiFormatProvider : IFormatterProvider {
  13. private IHost host;
  14. private string config;
  15. private ComponentInformation info = new ComponentInformation("Geshi Code Formatting Plugin 1.0", "Aspiring Technology", "http://hookedonlinq.com/");
  16.  
  17. public bool PerformPhase1 {
  18. get { return true; }
  19. }
  20.  
  21. public bool PerformPhase2 {
  22. get { return false; }
  23. }
  24.  
  25. public bool PerformPhase3 {
  26. get { return false; }
  27. }
  28.  
  29. public string Format(string raw, ContextInformation context, FormattingPhase phase) {
  30. // change these to suit your system
  31. const string geshiUrl = @"http://-yourwiki.com-/geshi/getgeshi.php";
  32. const int tailLength = 7;
  33. const int headLength = 5;
  34. string language = "csharp";
  35. string style = "";
  36. string lineNumbers = "0";
  37. string head = @"\<" + "code" + @".*?\>";
  38. string tail = @"\<\/" + "code" + @"\>";
  39. Regex regex = new Regex(head + ".+?" + tail, RegexOptions.IgnoreCase | RegexOptions.Singleline);
  40.  
  41. Match match = regex.Match(raw);
  42. while (match.Success) {
  43. // remove the tail
  44. string code = match.Value.Substring(0, match.Value.Length - tailLength);
  45.  
  46. // remove the header and extract the arguments
  47. int headEnd = code.IndexOf('>');
  48. string[] args = code.Substring(headLength, headEnd - headLength).Trim().Split(' ');
  49. foreach (string s in args) {
  50. if (s != string.Empty) {
  51. string[] thisArg = s.Split('=');
  52. if (thisArg.Length == 2) {
  53. switch (thisArg[0].ToLower()) {
  54. case "lang" :
  55. case "language" :language = thisArg[1].Trim(); break;
  56. case "numbers" : lineNumbers = thisArg[1].Trim(); break;
  57. case "style" : style = thisArg[1].Trim(); break;
  58. }
  59. }
  60. }
  61. }
  62. code = code.Substring(headEnd+1);
  63. // format the code
  64. raw = raw.Replace(match.Value, GetGeshiCode(context, geshiUrl, code, language, lineNumbers, style));
  65. match = regex.Match(raw);
  66. }
  67.  
  68. return raw;
  69. }
  70.  
  71. public void Init(IHost host, string config) {
  72. this.host = host;
  73. this.config = config;
  74. }
  75.  
  76. public void Shutdown() { }
  77.  
  78. public ComponentInformation Information {
  79. get { return info; }
  80. }
  81.  
  82. public string GetGeshiCode(ContextInformation context, string geshiUrl, string source, string language, string lineNumbers, string style)
  83. {
  84. string result = source;
  85.  
  86. if (string.IsNullOrEmpty(geshiUrl)) return result;
  87. if (string.IsNullOrEmpty(source)) return string.Empty;
  88.  
  89. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geshiUrl);
  90. try {
  91. byte[] postDataBytes = Encoding.UTF8.GetBytes("source=" + HttpUtility.UrlEncode(source) +
  92. "&language=" + HttpUtility.UrlEncode(language) +
  93. "&numbers=" + HttpUtility.UrlEncode(lineNumbers) +
  94. "&style=" + HttpUtility.UrlEncode(style));
  95. request.Method = "POST";
  96. request.ContentType = "application/x-www-form-urlencoded";
  97. request.ContentLength = postDataBytes.Length;
  98. Stream requestStream = request.GetRequestStream();
  99. requestStream.Write(postDataBytes, 0 ,postDataBytes.Length);
  100. requestStream.Close();
  101.  
  102. // get response and write to console
  103. HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
  104. try {
  105. StreamReader responseReader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
  106. result = responseReader.ReadToEnd();
  107. }
  108. finally {
  109. resp.Close();
  110. }
  111. }
  112. catch (Exception e){
  113. // If something goes wrong, we should log an error.
  114. // This will be system specific.
  115. host.LogEntry( String.Format("Failed Geshi Code Formatting for page {0} - {1}", context.Page.Name, e.Message), LogEntryType.Error, this);
  116. }
  117.  
  118. return result;
  119. }
  120. }
  121. }



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

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.