All pastes #32693 Raw Edit

Miscellany

public text v1 · immutable
#32693 ·published 2005-12-06 19:10 UTC
rendered paste body
/*************************************** 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<br />\n[code]\n<br />\nusing System;\n<br />\npublic class frW\n<br />\n{\n<br />\n}\n<br />\n[/code]\n<br />\nThere was alot of code!", "[code]", "[/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>
        /// <param name="codeEnder">The code highlighting startpoint, ie [code]</param>
        /// <param name="codeStarter">The code highlighting endpoint, ie [/code]</param>
        /// <returns>The fixed string with all HTML replacements</returns>
        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 + "<font color=" + strArrIdent[i].Color + ">" + strArrIdent[i].Name + "</font>";
                            strFinalCode = strFinalCode.Replace("\n", "<br />");
                        }
                    }
                }
                else
                {
                    strFinalCode = strFinalCode + arrScan[i];
                }
            }


            return strFinalCode;
        }   
    }
}