/*************************************** 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;
namespace scanner
{
public class highlight {
private string highligtColor = string.Empty, name = string.Empty;
public string Name
{
get
{
return name;
}
set
{
name= value;
}
}
public string Color
{
get
{
return highligtColor;
}
set
{
highligtColor = value;
}
}
}
class Scanner
{
static void Main(string[] args)
{
Console.WriteLine(replaceCode("Here comes alot of code\n
\n[code]\n
\nusing System;\n
\npublic class frW\n
\n{\n
\n}\n
\n[/code]\n
\nThere was alot of code!", "[code]", "[/code]"));
Console.WriteLine("END");
Console.Read();
}
///
/// 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
///
/// The string that we want to scan through
/// The code highlighting startpoint, ie [code]
/// The code highlighting endpoint, ie [/code]
/// The fixed string with all HTML replacements
public static string replaceCode(string strScan, string codeStarter, string codeEnder)
{
/******************** 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
highlight[] strArrIdent = new highlight[2];
strArrIdent[0].Name = "using";
strArrIdent[0].Color = "blue";
strArrIdent[1].Name = "string";
strArrIdent[1].Color = "blue";
string strFinalCode = string.Empty;
bool boInsideCodeStarter = false;
string[] arrScan = strScan.Split(' ');
for (int i = 0; i < arrScan.Length; i++)
{
if (arrScan[i] == codeStarter)
{
boInsideCodeStarter = true;
}
else if (arrScan[i] == codeEnder)
{
boInsideCodeStarter = false;
}
else if (boInsideCodeStarter == true)
{
for (int a = 0; a < strArrIdent.Length; a++)
{
if (arrScan[i] == strArrIdent[i].Name)
{
strFinalCode = strFinalCode + "" + strArrIdent[i].Name + "";
strFinalCode = strFinalCode.Replace("\n", "
");
}
}
}
else
{
strFinalCode = strFinalCode + arrScan[i];
}
}
return strFinalCode;
}
}
}