代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
Convert Code To HTML
作者:
dezai
/ 发布于
2014/6/20
/
560
using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Web; namespace CodeHTML { public class TranslateCode { private class ProgrammingLanguage { public const string VB = "vb"; public const string CSharp = "c#"; public const string JSharp = "js"; } private bool _ShowFileName; private int _FontSize; private string _Language; private const string TAG_FNTRED = "<font color=\"red\">"; private const string TAG_FNTBLUE = "<font color=\"blue\">"; private const string TAG_FNTGRN = "<font color=\"green\">"; private const string TAG_FNTMRN = "<font color=\"maroon\">"; private const string TAG_EFONT = "</font>"; public bool ShowFileName { get { return _ShowFileName; } set { _ShowFileName = value; } } public int FontSize { get { return _FontSize; } set { _FontSize = value; } } public string Language { get { return _Language; } set { _Language = value; } } public TranslateCode() { _ShowFileName = false; _FontSize = 3; _Language = ""; } private void SetLanguageFromFileName(string FilePath) { if (FilePath.Split(new char[] { Convert.ToChar(".") }).Length > 0) { string str2 = FilePath.Substring(FilePath.LastIndexOf(Convert.ToChar(".")) + 1).ToLower().Trim(); if (StringType.StrCmp(str2, "vb", false) == 0) { _Language = "vb"; } else if (StringType.StrCmp(str2, "c#", false) == 0) { _Language = "c#"; } else if (StringType.StrCmp(str2, "js", false) == 0) { _Language = "js"; } } } public string RenderFile(string FilePath) { SetLanguageFromFileName(FilePath); return Render(File.OpenText(FilePath)); } public void RenderFile(string FilePath, string OutputFilePath) { SetLanguageFromFileName(FilePath); StreamWriter streamWriter = new StreamWriter(OutputFilePath); streamWriter.Write(Render(File.OpenText(FilePath))); streamWriter.Flush(); streamWriter.Close(); } public string Render(StreamReader InputTextReader) { return Render(InputTextReader.ReadToEnd()); } public string Render(string InputString) { StringBuilder stringBuilder = new StringBuilder(); StringWriter stringWriter = new StringWriter(stringBuilder); string[] strs1 = InputString.Split(new char[] { 13 }); stringWriter.WriteLine(String.Concat("<font size=\"", StringType.FromInteger(_FontSize), "\" \\>")); string str4 = _Language.Trim().ToLower(); if (StringType.StrCmp(str4, "c#", false) == 0) { stringWriter.WriteLine("<pre>"); string[] strs5 = strs1; for (int i2 = 0; i2 < (int)strs5.Length; i2++) { string str2 = strs5[i2]; stringWriter.Write(FixCSLine(str2)); } stringWriter.WriteLine("</pre>"); } else if (StringType.StrCmp(str4, "js", false) == 0) { stringWriter.WriteLine("<pre>"); string[] strs4 = strs1; for (int k = 0; k < (int)strs4.Length; k++) { string str2 = strs4[k]; stringWriter.Write(FixJSLine(str2)); } stringWriter.WriteLine("</pre>"); } else if (StringType.StrCmp(str4, "vb", false) == 0) { stringWriter.WriteLine("<pre>"); string[] strs3 = strs1; for (int j = 0; j < (int)strs3.Length; j++) { string str2 = strs3[j]; stringWriter.Write(FixVBLine(str2)); } stringWriter.WriteLine("</pre>"); } else { bool flag2 = false; bool flag1 = false; stringWriter.WriteLine("<pre>"); string[] strs2 = strs1; for (int i1 = 0; i1 < (int)strs2.Length; i1++) { string str2 = strs2[i1]; _Language = GetLanguageFromLine(str2, _Language); if (IsScriptBlockTagStart(str2)) { stringWriter.Write(FixASPXLine(str2)); flag2 = true; } else if (IsScriptBlockTagEnd(str2)) { stringWriter.Write(FixASPXLine(str2)); flag2 = false; } else if ((IsMultiLineTagStart(str2) & flag1 == false) != 0) { stringWriter.Write(String.Concat("<font color=blue><b>", HttpUtility.HtmlEncode(str2))); flag1 = true; } else if ((IsMultiLineTagEnd(str2) & flag1) != 0) { stringWriter.Write(String.Concat(HttpUtility.HtmlEncode(str2), "</b></font>")); flag1 = false; } else if (flag1) { stringWriter.Write(HttpUtility.HtmlEncode(str2)); } else if (flag2) { string str3 = _Language.Trim().ToLower(); if (StringType.StrCmp(str3, "c#", false) == 0) { stringWriter.Write(FixCSLine(str2)); } else if (StringType.StrCmp(str3, "js", false) == 0) { stringWriter.Write(FixJSLine(str2)); } else if (StringType.StrCmp(str3, "vb", false) == 0) { stringWriter.Write(FixVBLine(str2)); } else { stringWriter.Write(FixVBLine(str2)); } } else { stringWriter.Write(FixASPXLine(str2)); } } stringWriter.WriteLine("</pre>"); } stringWriter.WriteLine("</font>"); stringWriter.Flush(); return stringBuilder.ToString(); } private string GetLanguageFromLine(string sInputLine, string DefaultLanguage) { string str1; string str2 = DefaultLanguage; if (sInputLine.Length == 0) { str1 = str2; } else { Match match = Regex.Match(sInputLine, "(?i)<%@\\s*Page\\s*.*Language\\s*=\\s*\"(?<lang>[^\"]+)\""); if (match.Success) { str2 = match.Groups["lang"].ToString(); } match = Regex.Match(sInputLine, "(?i)(?=.*runat\\s*=\\s*\"?server\"?)<script.*language\\s*=\\s*\"(?<lang>[^\"]+)\".*>"); if (match.Success) { str2 = match.Groups["lang"].ToString(); } match = Regex.Match(sInputLine, "(?i)<%@\\s*WebService\\s*.*Language\\s*=\\s*\"?(?<lang>[^\"]+)\"?"); if (match.Success) { str2 = match.Groups["lang"].ToString(); } if (StringType.StrCmp(str2, "CS", false) == 0) { str2 = "c#"; } str1 = str2; } return str1; } private string FixCSLine(string sInputLine) { string str1; string str3 = sInputLine; if (sInputLine.Length == 0) { str1 = sInputLine; } else { str3 = Regex.Replace(sInputLine, "(?i)(\\t)", " "); str3 = HttpUtility.HtmlEncode(str3); string[] strs1 = new string[]{"private", "protected", "public", "namespace", "class", "break", "for", "if", "else", "while", "switch", "case", "using", "return", "null", "void", "int", "bool", "string", "float", "this", "new", "true", "false", "const", "static", "base", "foreach", "in", "try", "catch", "get", "set", "char", "default"}; string str2 = String.Concat("(?<keyword>", String.Join("|", strs1), ")"); str3 = Regex.Replace(str3, String.Concat("\\b", str2, "\\b(?<!//.*)"), "<font color=\"blue\">${keyword}</font>"); str3 = Regex.Replace(str3, "(?<comment>//.*$)", "<font color=\"green\">${comment}</font>"); str1 = str3; } return str1; } private string FixJSLine(string sInputLine) { string str1; string str3 = sInputLine; if (sInputLine.Length == 0) { str1 = sInputLine; } else { str3 = Regex.Replace(sInputLine, "(?i)(\\t)", " "); str3 = HttpUtility.HtmlEncode(str3); string[] strs1 = new string[]{"private", "protected", "public", "namespace", "class", "var", "for", "if", "else", "while", "switch", "case", "using", "get", "return", "null", "void", "int", "string", "float", "this", "set", "new", "true", "false", "const", "static", "package", "function", "internal", "extends", "super", "import", "default", "break", "try", "catch", "finally"}; string str2 = String.Concat("(?<keyword>", String.Join("|", strs1), ")"); str3 = Regex.Replace(str3, String.Concat("\\b", str2, "\\b(?<!//.*)"), "<font color=\"blue\">${keyword}</font>"); str3 = Regex.Replace(str3, "(?<comment>//.*$)", "<font color=\"green\">${comment}</font>"); str1 = str3; } return str1; } private string FixVBLine(string sInputLine) { string str2; string str3 = sInputLine; if (sInputLine.Length == 0) { str2 = sInputLine; } else { str3 = Regex.Replace(sInputLine, "(?i)(\\t)", " "); str3 = HttpUtility.HtmlEncode(str3); string[] strs1 = new string[]{"AddressOf", "Delegate", "Optional", "ByVal", "ByRef", "Decimal", "Boolean", "Option", "Compare", "Binary", "Text", "On", "Off", "Explicit", "Strict", "Private", "Protected", "Public", "End Namespace", "Namespace", "End Class", "Exit", "Class", "Goto", "Try", "Catch", "End Try", "For", "End If", "If", "Else", "ElseIf", "Next", "While", "And", "Do", "Loop", "Dim", "As", "End Select", "Select", "Case", "Or", "Imports", "Then", "Integer", "Long", "String", "Overloads", "True", "Overrides", "End Property", "End Sub", "End Function", "Sub", "Me", "Function", "End Get", "End Set", "Get", "Friend", "Inherits", "Implements", "Return", "Not", "New", "Shared", "Nothing", "Finally", "False", "Me", "My", "MyBase", "End Enum", "Enum"}; string str1 = String.Concat("(?<keyword>", String.Join("|", strs1), ")"); str3 = Regex.Replace(str3, String.Concat("(?i)\\b", str1, "\\b(?<!\\'.*)"), "<font color=\"blue\">${keyword}</font>"); str3 = Regex.Replace(str3, "(?<comment>\\'(?![^\\']*\").*$)", "<font color=\"green\">${comment}</font>"); str2 = str3; } return str2; } private string FixASPXLine(string sInputLine) { string str1; string str2 = sInputLine; if (sInputLine.Length == 0) { str1 = sInputLine; } else { str2 = Regex.Replace(str2, "(?i)(\\t)", " "); str2 = HttpUtility.HtmlEncode(str2); string str4 = "(?i)(?<a>(^.*))(?<b>(<!--))(?<c>(.*))(?<d>(-->))(?<e>(.*))"; string str3 = "${a}<font color=\"green\">${b}${c}${d}</font>${e}"; if (Regex.IsMatch(str2, str4)) { str2 = Regex.Replace(str2, str4, str3); } str4 = "(?i)(?<a>(<%@))(?<b>(.*))(?<c>(%>))"; str3 = "<font color=blue><b>${a}${b}${c}</b></font>"; if (Regex.IsMatch(str2, str4)) { str2 = Regex.Replace(str2, str4, str3); } str4 = "(?i)(?<a>(<%#))(?<b>(.*))(?<c>(%>))"; str3 = "${a}<font color=red><b>${b}</b></font>${c}"; if (Regex.IsMatch(str2, str4)) { str2 = Regex.Replace(str2, str4, str3); } str4 = "(?i)(?<a>(<)(?!%)(?!/?asp:)(?!/?template)(?!/?property)(?!/?ibuyspy:)(/|!)?)(?<b>[^;\\s&]+)(?<c>(\\s|>|\\Z))"; str3 = "${a}<font color=\"maroon\">${b}</font>${c}"; if (Regex.IsMatch(str2, str4)) { str2 = Regex.Replace(str2, str4, str3); } str4 = "(?i)(?<a></?)(?<b>(asp:|template|property|IBuySpy:).*)(?<c>>)?"; str3 = "${a}<font color=\"blue\"><b>${b}</b></font>${c}"; if (Regex.IsMatch(str2, str4)) { str2 = Regex.Replace(str2, str4, str3); } str4 = "(?i)(?<a>(<)(/|!|%)?)"; str3 = "<font color=\"blue\">${a}</font>"; if (Regex.IsMatch(str2, str4)) { str2 = Regex.Replace(str2, str4, str3); } str4 = "(?i)(?<a>(/|%)?(>))"; str3 = "<font color=\"blue\">${a}</font>"; if (Regex.IsMatch(str2, str4)) { str2 = Regex.Replace(str2, str4, str3); } str1 = str2; } return str1; } private bool IsScriptBlockTagStart(string sInputLine) { bool flag1 = false; if (Regex.IsMatch(sInputLine, "<script.*runat=\"?server\"?.*>")) { flag1 = true; } else if (Regex.IsMatch(sInputLine, "(?i)<%@\\s*WebService")) { flag1 = true; } bool flag2 = flag1; return flag2; } private bool IsScriptBlockTagEnd(string sInputLine) { bool flag1 = false; if (Regex.IsMatch(sInputLine, "</script.*>")) { flag1 = true; } bool flag2 = flag1; return flag2; } private bool IsMultiLineTagStart(string sInputLine) { bool flag1 = false; string str2 = "(?i)(?!.*>)(?<a></?)(?<b>(asp:|template|property|IBuySpy:).*)"; if (Regex.IsMatch(HttpUtility.HtmlEncode(sInputLine), str2)) { flag1 = true; } bool flag2 = flag1; return flag2; } private bool IsMultiLineTagEnd(string sInputLine) { bool flag1 = false; string str2 = "(?i)>"; if (Regex.IsMatch(HttpUtility.HtmlEncode(sInputLine), str2)) { flag1 = true; } bool flag2 = flag1; return flag2; } } }
试试其它关键字
Convert
Code
To
HTML
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3