写了一个简单的语法着色类

主要功能是把c#的文本代码转换成有颜色的html代码

实在是很笨一个类,只有两种着色模式(把关键字变蓝,把字符串变褐",但还能用...反正挺简单,把源代码也发出来,下面的源代码就是用这个类着色过的

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace d_BlogCodeHelper
{
    
    public class ColorCodes
    {
        public string Font
        {
            get { return font; }
            set { font = value; }
        }
        public string FontSize
        {
            get { return fontSize; }
            set { fontSize = value; }
        }
        public string BlueColor
        {
            get { return blueColor; }
            set { blueColor = value; }
        }
        public string BrownColor
        {
            get { return brownColor; }
            set { brownColor = value; }
        }
        public string GreenColor
        {
            get { return greenColor; }
            set { greenColor = value; }
        }

        public string BrownAndBlueWords(string oldtext)
        {
            StringBuilder text = new StringBuilder(oldtext);
            text.Replace(" "" ");
            text.Replace("<""<");
            text.Replace(">"">");
            text.Replace("/n""<br/>");
            text.Insert(0, "<font face='" + Font + "'><font size='" + FontSize + "'>");
            text.Append("</font>");
            foreach (string nowWord in blueWords)
            {
                text.Replace(nowWord, "<font color='#" + blueColor + "'>" + nowWord + "</font>");
            }

            Regex findString = new Regex("/"(?.)*?/"");
            MatchCollection allStrings = findString.Matches(text.ToString());
            foreach (Match oneString in allStrings)
            {
                text.Replace(oneString.Value, "<font color='#" + brownColor + "'>" + oneString.Value + "</font>");
            }
            return text.ToString();
        }

        public string BlueWords(string oldtext)
        {
            StringBuilder text = new StringBuilder(oldtext);
            text.Replace(" "" ");
            text.Replace("<""<");
            text.Replace(">"">");
            text.Replace("/n""<br/>");
            text.Insert(0, "<font face='" + Font + "'><font size='"+FontSize+"'>");
            text.Append("</font>");
            foreach (string nowWord in blueWords)
            {
                text.Replace(nowWord, "<font color='#"+blueColor+"'>" + nowWord + "</font>");
            }
            return text.ToString();
           
        }


        private string font = "Tahoma";
        private string fontSize="1";
        private string blueColor="0000ff";
        private string greenColor="008800";
        private string brownColor = "a31515"

//下面一个储存所有C#关键字的数组,但是对于在字符串里出现的关键字,这个类处理的还不够好

        private string[] blueWords ={     }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值