C#源代码高亮着色类(C/S框架网开源)
C#源代码高亮着色类(C/S框架网开源)
C#代码高亮着色 有些功能尚未实现,比如代码中的/*注释*/无法高亮,将就使用足够啦! using System;
using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; /* * 版权:C/S框架网 www.csframework.com * * C#源代码高亮着色类 */ namespace CSFramework.BLL { /// <summary> /// C#源代码高亮着色类 /// </summary> public class CSourceRenderer { /// <summary> /// 源代码着色构造器 /// </summary> public string RenderSource(string src) { string html = src; html = ColorComment(html); //转换注释 html = ColorKeyword(html); //转换关键字 html = IndentCode(html); //空格缩进 html = ReplaceCSSTag(html); //替换CSS html = "<div class=\"codearea\">" html "</div>"; return html; } private string ReplaceCSSTag(string src) { src = src.Replace(" class='codecomment' ", " class='codecomment' ");//注释 src = src.Replace(" class='codecomment1' ", " class='codecomment1' ");//注释 src = src.Replace(" class='codekeyword' ", " class='codekeyword' ");//关键字 src = src.Replace("\r</span>", "</span>\r"); src = src.Replace("\n", "</br>"); return src; } /// <summary> /// 为//和/**/类型的注释着色 /// </summary> /// <param name="src">输入源码</param> /// <returns>格式化后的源码</returns> private string ColorBasicComment(string src) { string retCode = src; Regex r1 = new Regex(@"(^|;)([ \t]*)(//.*$)", RegexOptions.Multiline); retCode = r1.Replace(retCode, "$1$2<span class='codecomment' >$3</span>"); Regex r2 = new Regex(@"(^|[ \t] )(/\*[^\*/]*\*/[ \t\r]*$)", RegexOptions.Multiline); retCode = r2.Replace(retCode, new MatchEvaluator(this.ColorBasicComment2Evaluator)); return retCode; } /// <summary> /// 为///类型的注释着色 /// </summary> /// <param name="src">输入源码</param> /// <returns>格式化后的源码</returns> private string ColorXmlComment(string src) { string retCode = src; Regex r1 = new Regex(@"(/// *)<(.*)>)(.*)(& >", RegexOptions.Multiline); retCode = r1.Replace(retCode, new MatchEvaluator(this.ColorXmlCommentEvaluator)); Regex r2 = new Regex(@"(/// *)(.*$)", RegexOptions.Multiline); retCode = r2.Replace(retCode, "<span class='codecomment1' >$1</span>$2"); return retCode; } /// <summary> /// 为折叠显示代码构建HtmlTable框架 /// </summary> /// <param name="src">输入源码</param> /// <returns>格式化后的源码</returns> private string DrawCollapseFrameTable(string src) { System.Text.StringBuilder retCode = new System.Text.StringBuilder(); string frameHeader = ""; string frameTailer = "<br/>"; retCode.Append(frameHeader); string[] lines = src.Split('\n'); foreach (string line in lines) { string formatedLine = line.Trim(); string lineHeader = ""; string lineTailer = "<br/>"; formatedLine = lineHeader formatedLine lineTailer; retCode.Append(formatedLine); } retCode.Append(frameTailer); return retCode.ToString(); } /// <summary> /// 为一行源码中的关键字着色 /// </summary> /// <param name="codeLine">某一行源码</param> /// <param name="keywordList">关键字列表</param> /// <returns>格式化后的源码</returns> /// <summary> private string ColorKeyword(string codeLine, string[] keywordList) { string retCode = codeLine; if (!retCode.StartsWith("//")) { foreach (string keyword in KEYWORD_LIST) { Regex r = new Regex(@"(^|\s |,|\)|\(|\{|\}|\[|\]|\.|=|;)(" keyword @")(\s |,|\)|\(|\{|\}|\[|\]|\.|=|;|$)"); retCode = r.Replace(retCode, "$1<span class='codekeyword' >$2</span>$3"); } } retCode = ClearColoredKeyworsInString(retCode); return retCode.Trim(); } /// <summary> /// 清除字符串中的被着色的关键字 /// </summary> /// <param name="codeLine">某一行源码</param> /// <returns>格式化后的源码</returns> private string ClearColoredKeyworsInString(string codeLine) { System.Text.StringBuilder retCode = new System.Text.StringBuilder(); string str = codeLine.Trim(); int indexOfQuot = str.IndexOf("""); int lengthOfQuot = """.Length; if (indexOfQuot >= 0) { while (indexOfQuot >= 0) { retCode.Append(str.Substring(0, indexOfQuot lengthOfQuot)); str = str.Substring(indexOfQuot lengthOfQuot); indexOfQuot = str.IndexOf("""); if (indexOfQuot >= 0) { string inStr = str.Substring(0, indexOfQuot lengthOfQuot); inStr = inStr.Replace("\"color: blue\"", "\"\""); retCode.Append(inStr); str = str.Substring(indexOfQuot lengthOfQuot); } indexOfQuot = str.IndexOf("""); } } retCode.Append(str); return retCode.ToString(); } /// <summary> /// 关键字列表 /// </summary> public static string[] KEYWORD_LIST = { "abstract", "event", "new", "struct", "as", "explicit", "null", "switch", "base", "extern", "object", "this", "bool", "false", "operator", "throw", "break", "finally", "out", "true", "byte", "fixed", "override", "try", "case", "float", "params", "typeof", "catch", "for", "private", "uint", "char", "foreach", "protected", "ulong", "checked", "goto", "public", "unchecked", "class", "if", "readonly", "unsafe", "const", "implicit", "ref", "ushort", "continue", "in", "return", "using", "decimal", "int", "sbyte", "virtual", "default", "interface", "sealed", "volatile", "delegate", "internal", "short", "void", "do", "is", "sizeof", "while", "double", "lock", "stackalloc", "else", "long", "static", "enum", "namespace", "string", "get", "set", "#region", "#endregion", "true", "false" }; private string ColorXmlCommentEvaluator(Match m) { Regex r = new Regex("(^.*</.*$)"); if (r.Match(m.Value).Success) { return r.Replace(m.Value, "<span class='codecomment1' >$1</span>$2<span class='codecomment1' >$3</span>"); } else { return "<span class='codecomment1' >" m.Value "</span>"; } } private string ColorBasicComment2Evaluator(Match m) { System.Text.StringBuilder retCode = new System.Text.StringBuilder(); string[] lines = m.Value.Split('\n'); foreach (string line in lines) { retCode.Append("<span class='codecomment' >" line "</span>" "\n"); } return retCode.ToString(); } /// <summary> /// 为注释着色,例如:对C#,包括///型、//型和/**/的注释 /// </summary> /// <param name="src">输入源码</param> /// <returns>格式化后的源码</returns> private string ColorComment(string src) { string retCode = src; retCode = ColorBasicComment(retCode); retCode = ColorXmlComment(retCode); return retCode; } /// <summary> /// 为关键字着色 /// </summary> /// <param name="src">输入源码</param> /// <returns>格式化后的源码</returns> private string ColorKeyword(string src) { System.Text.StringBuilder retCode = new System.Text.StringBuilder(); string[] lines = src.Split('\n'); if (lines != null & lines.Length > 0) { bool isInComment = false; foreach (string line in lines) { string formatedLine = line.Trim(); if (new Regex(@"(^|[ \t] )(/\*)").Match(line).Success) { isInComment = true; } if (!isInComment) { formatedLine = ColorKeyword(line, KEYWORD_LIST); } if (new Regex(@"\*/[ \t\r]*$").Match(line).Success) { isInComment = false; } retCode.Append(formatedLine "\n"); } } return retCode.ToString(); } /// <summary> /// 代码缩进 /// </summary> /// <param name="src">输入源码</param> /// <returns>格式化后的源码</returns> private string IndentCode(string src) { System.Text.StringBuilder retCode = new System.Text.StringBuilder(); int indent = 0; string[] lines = src.Split('\n'); foreach (string line in lines) { string formatedLine = line.Trim(); if (line.Trim() == "}") { indent--; } for (int i = 0; i < indent; i ) { formatedLine = " " formatedLine; } retCode.Append(formatedLine "\n"); if (line.Trim() == "{" || line.EndsWith("{")) { indent ; } } return retCode.ToString(); } } } // 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加作者微信
参考文档:
图像处理业务逻辑CImageLibrary类(C/S框架网开源) Asp.Net使用SmtpClient发送邮件(C/S框架网开源) Asp.Net生成图片验证码(C/S框架网开源) C#多文档界面(MDI)系统框架 (原创在C/S框架网!) C#代码行数统计器(C/S框架网) C/S框架网提供MES/ERP系统基于C#语言的开发平台产品 C/S架构的Winform 代码生成器软件|C/S框架网原创作品 推荐C# Winform+DevExpress皮肤框架|C/S框架网 原创C#开发框架|C#开源框架|C/S框架网 Winform通用框架(C#.NET)之权限管理系统详细设计|C/S框架网 C#框架推荐-C/S框架网原创快速开发平台系列产品 代码生成器加载用户表和视图报错解决方案|C/S框架网 CSFramework代码生成器自动生成主从表界面-C/S框架网 Winform自动升级框架源码(C#.NET)-C/S框架网 原创:CodeHighlighter源代码格式化,代码缩进,关键词高亮着色(C#源码)
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |