All pastes #32686 Raw Edit

Unnamed

public text v1 · immutable
#32686 ·published 2005-12-06 18:40 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
{
    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.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
            string[,] strArrIdent = new string[,] {
                {"blue", "using"},
                {"blue", "namepsace"}
            };

            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 (boInsideCodeStarter == true)
                {
                    for (int a = 0; a < strArrIdent.Length; a++)
                    {
                        if (arrScan[i] == strArrIdent[i][1])
                        {
                            strFinalCode = strFinalCode + "<font color=" + strArrIdent[i][0] + ">" + arrScan + "</font>";
                            strFinalCode = strFinalCode.Replace("\n", "<br />");
                        }
                    }
                }
                else
                {
                    strFinalCode = strFinalCode + arrScan[i];
                }
            }


            return strScan;
        }   
    }
}