/*************************************** INFORMATION *********************************************/
// This is suppose to be a scanned that are used on in a web-based situation
// I want to achive a syntax-highligter that replaces the text between my codeStarter-point and
// the end-point with HTML code. An example is this:
//
// ---- OTHER HTML CODE HERE ----
// [code]
// using System;
// using System.Web;
// namespace
// {
// class Scanner
// {
// public static string test = "blah";
// }
// }
// [/code]
// ---- OTHER HTML CODE HERE ----
//
//
//
// Author frw@frw.se, Filip Ekberg
/*************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
namespace scanner
{
public class CHighligt {
private string strHighligtColor = string.Empty, strName = string.Empty;
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}
public string Color
{
get
{
return strHighligtColor;
}
set
{
strHighligtColor = value;
}
}
}
class Scanner
{
static void Main(string[] args)
{
Console.WriteLine("\n\n" +replaceCode("Here comes alot of code\n<br />\n[C#]\n<br />\nusing System;\n<br />\npublic class frW\n<br />\n{\n<br />\n}\n<br />\n[/C#]\n<br />\nThere was alot of code!"));
Console.WriteLine("END");
Console.Read();
}
/// <summary>
/// This method will let us send in a big text and then re-write it and add the C# highligting
/// This will return in HTML, all line-breaks inside the code will be replaced with <br />
/// </summary>
/// <param name="strScan">The string that we want to scan through</param>
/// <returns>The fixed string with all HTML replacements</returns>
public static string replaceCode(string strScan)
{
/******************** THE ARRAY *************************/
// ArrayList arrHighlight = new ArrayList();
// arrHighligt[0] = new ArrayList();
// arrHighlight[0].add("class", "namespace");
//
// This ArrayList is used to store the words that are supose to be replaced inside the codeStarter
// and codeEnder.
//
// [0] = identyfiers
// [1] = Operators
// [2] = misc
/********************************************************/
// This will be set to true when we are inside codeStarter
CHighligt[] strArrIdent = new CHighligt[6];
strArrIdent[0] = new CHighligt();
strArrIdent[0].Name = "using";
strArrIdent[0].Color = "blue";
strArrIdent[1] = new CHighligt();
strArrIdent[1].Name = "string";
strArrIdent[1].Color = "blue";
strArrIdent[2] = new CHighligt();
strArrIdent[2].Name = "class";
strArrIdent[2].Color = "blue";
strArrIdent[3] = new CHighligt();
strArrIdent[3].Name = "int";
strArrIdent[3].Color = "blue";
strArrIdent[4] = new CHighligt();
strArrIdent[4].Name = "namespace";
strArrIdent[4].Color = "blue";
strArrIdent[5] = new CHighligt();
strArrIdent[5].Name = "new";
strArrIdent[5].Color = "blue";
string strFinalCode = string.Empty;
bool boInsideCodeStarter = false;
// Console.WriteLine("INPUT \n\n\n");
// Console.WriteLine(strScan);
// Console.WriteLine("\n\n\nERROR HANDELING \n\n\n");
strScan = Regex.Replace(strScan, @"(.*)<br(.*)/>(.*)", @"$1<br$--WHITE_SPACE--/>$3", RegexOptions.Multiline);
// Console.WriteLine(strScan);
string[] arrScanning = strScan.Split(' ');
string nextElement = string.Empty;
for (int iOuterLoop = 0; iOuterLoop < arrScanning.Length; iOuterLoop++)
{
Console.WriteLine("CURRET LINE\t= \t{0}", arrScanning[iOuterLoop]);
if (Regex.IsMatch(arrScanning[iOuterLoop], @"(\[C#\])", RegexOptions.Singleline))
{
Console.WriteLine("JUMPING IN WITH \t= \t{0}", arrScanning[iOuterLoop]);
boInsideCodeStarter = true;
}
else if (boInsideCodeStarter == true)
{
if (Regex.IsMatch(arrScanning[iOuterLoop], @"(.*)(\[\/C#\])(.*)", RegexOptions.Singleline))
{
boInsideCodeStarter = false;
}
else
{
for (int iInnerLoop = 0; iInnerLoop < strArrIdent.Length; iInnerLoop++)
{
if (Regex.IsMatch(arrScanning[iOuterLoop], @".*" + strArrIdent[iInnerLoop].Name + ".*", RegexOptions.ECMAScript))
{
strFinalCode = strFinalCode + "<font color=" + strArrIdent[iInnerLoop].Color + ">" + arrScanning[iOuterLoop] + "</font>$--WHITE_SPACE--";
}
else if (!Regex.IsMatch(arrScanning[iOuterLoop], @".*" + strArrIdent[iInnerLoop].Name + ".*", RegexOptions.ECMAScript))
{
strFinalCode = strFinalCode + arrScanning[iOuterLoop] + "$--WHITE_SPACE--";
}
}
}
}
else
{
strFinalCode = strFinalCode + arrScanning[iOuterLoop] + "$--WHITE_SPACE--";
}
}
//strFinalCode = strFinalCode.Replace("\n", "<br />");
strFinalCode = strFinalCode.Replace("$--WHITE_SPACE--", " ");
// Console.WriteLine("\n\n\nOUTPUT \n\n\n");
return strFinalCode;
}
}
}