C# 格式化CodeHighlighter生成的SQL脚本高亮着色源码(CSFramework.com原创)
C# 格式化CodeHighlighter生成的SQL脚本高亮着色源码(CSFramework.com原创)
调用CodeHighlighterEngine.GenerateHtmlInline方法生成的SQL脚本高亮代码没有排版(没有代码格式化),因此自己实现C#代码排版功能,效果一般吧,总比没有排版好多了。
C# Code:
/// <summary>
/// SQL脚本代码缩进,排版,格式化
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
private static string IndentCodeSql(string SQL)
{
bool firstAs = false;
bool firstBegin = false;
bool firstAsBegin = false;
System.Text.StringBuilder retCode = new System.Text.StringBuilder();
int indent = 0;
string NoHtml;
string[] lines = SQL.Split('\n');
foreach (string line in lines)
{
NoHtml = RemoveHTML(line.Trim());
if (NoHtml == "AS" && firstAs == false) firstAs = true;
if (NoHtml == "BEGIN" && firstBegin == false) firstBegin = true;
if (NoHtml.Replace(" ", "").ToUpper() == "ASBEGIN" && firstAsBegin == false) firstAsBegin = true;
//代码段结束,减少缩进
if (line.Trim() == ")" || line.Trim().ToUpper() == "END")
{
if (indent > 0) indent--;
}
string formatedLine = line.Trim();
for (int i = 0; i < indent; i++)
{
formatedLine = " " + formatedLine;//指表符
}
//定义符合
if (NoHtml.StartsWith("@")) formatedLine = " " + formatedLine;//指表符
retCode.Append(formatedLine + "\n");
//代码段开始,增加缩进
if (line.Trim() == "(" || line.Trim().StartsWith("(") || line.Trim().EndsWith("(") || line.Trim().ToUpper() == "BEGIN" || line.Trim().ToUpper().EndsWith("BEGIN"))
{
indent++;
}
// AS BEGIN, AS, BEGIN
if ((firstAs && firstBegin) || firstAsBegin)
{
indent++;
firstAs = false; firstAsBegin = false; firstAsBegin = false;
}
}
return retCode.ToString();
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// SQL脚本代码缩进,排版,格式化
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
private static string IndentCodeSql(string SQL)
{
bool firstAs = false;
bool firstBegin = false;
bool firstAsBegin = false;
System.Text.StringBuilder retCode = new System.Text.StringBuilder();
int indent = 0;
string NoHtml;
string[] lines = SQL.Split('\n');
foreach (string line in lines)
{
NoHtml = RemoveHTML(line.Trim());
if (NoHtml == "AS" && firstAs == false) firstAs = true;
if (NoHtml == "BEGIN" && firstBegin == false) firstBegin = true;
if (NoHtml.Replace(" ", "").ToUpper() == "ASBEGIN" && firstAsBegin == false) firstAsBegin = true;
//代码段结束,减少缩进
if (line.Trim() == ")" || line.Trim().ToUpper() == "END")
{
if (indent > 0) indent--;
}
string formatedLine = line.Trim();
for (int i = 0; i < indent; i++)
{
formatedLine = " " + formatedLine;//指表符
}
//定义符合
if (NoHtml.StartsWith("@")) formatedLine = " " + formatedLine;//指表符
retCode.Append(formatedLine + "\n");
//代码段开始,增加缩进
if (line.Trim() == "(" || line.Trim().StartsWith("(") || line.Trim().EndsWith("(") || line.Trim().ToUpper() == "BEGIN" || line.Trim().ToUpper().EndsWith("BEGIN"))
{
indent++;
}
// AS BEGIN, AS, BEGIN
if ((firstAs && firstBegin) || firstAsBegin)
{
indent++;
firstAs = false; firstAsBegin = false; firstAsBegin = false;
}
}
return retCode.ToString();
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
C# Code:
/// <summary>
/// C#使用正则表达式移除所有的Html标记,返回纯文本
/// </summary>
/// <param name="HtmlString"></param>
/// <returns>返回纯文本</returns>
private static string RemoveHTML(string HtmlString)
{
//删除脚本
HtmlString =
Regex.Replace(HtmlString, @"<script[^>]*?>.*?</script>",
"", RegexOptions.IgnoreCase);
//删除HTML
HtmlString = Regex.Replace(HtmlString, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"-->", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"<!--.*", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&#(\d+);", "", RegexOptions.IgnoreCase);
HtmlString.Replace("<", "");
HtmlString.Replace(">", "");
HtmlString.Replace("\r\n", "");
HtmlString = HttpContext.Current.Server.HtmlEncode(HtmlString).Trim();
return HtmlString;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// C#使用正则表达式移除所有的Html标记,返回纯文本
/// </summary>
/// <param name="HtmlString"></param>
/// <returns>返回纯文本</returns>
private static string RemoveHTML(string HtmlString)
{
//删除脚本
HtmlString =
Regex.Replace(HtmlString, @"<script[^>]*?>.*?</script>",
"", RegexOptions.IgnoreCase);
//删除HTML
HtmlString = Regex.Replace(HtmlString, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"-->", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"<!--.*", "", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
HtmlString = Regex.Replace(HtmlString, @"&#(\d+);", "", RegexOptions.IgnoreCase);
HtmlString.Replace("<", "");
HtmlString.Replace(">", "");
HtmlString.Replace("\r\n", "");
HtmlString = HttpContext.Current.Server.HtmlEncode(HtmlString).Trim();
return HtmlString;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
SQL脚本格式化效果:
SQL Code:
GO
/****** Object: StoredProcedure [dbo].[usp_lic_GetFunctionListByUser] Script Date: 03/02/2020 19:32:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_lic_GetFunctionListByUser]
@LicenseID VARCHAR(20)=''
AS
BEGIN
/**************************************************
功能:获取定制权限(特殊权限)
SELECT * FROM sys_ProductFunctionUser
SELECT * FROM sys_ProductFunctionAll
usp_lic_GetFunctionListByUser '0001'
***************************************************/
--返回客户基本资料
SELECT LicenseID,UserName,ContactPerson,Tel,Mobile FROM dbo.sys_ProductUser WHERE LicenseID=@LicenseID
--返回客户软件功能资料
SELECT * FROM dbo.sys_ProductFunctionUser WHERE LicenseID=@LicenseID ORDER BY ModuleID,FunctionID
END;
//来源:C/S框架网(www.csframework.com) QQ:23404761
/****** Object: StoredProcedure [dbo].[usp_lic_GetFunctionListByUser] Script Date: 03/02/2020 19:32:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_lic_GetFunctionListByUser]
@LicenseID VARCHAR(20)=''
AS
BEGIN
/**************************************************
功能:获取定制权限(特殊权限)
SELECT * FROM sys_ProductFunctionUser
SELECT * FROM sys_ProductFunctionAll
usp_lic_GetFunctionListByUser '0001'
***************************************************/
--返回客户基本资料
SELECT LicenseID,UserName,ContactPerson,Tel,Mobile FROM dbo.sys_ProductUser WHERE LicenseID=@LicenseID
--返回客户软件功能资料
SELECT * FROM dbo.sys_ProductFunctionUser WHERE LicenseID=@LicenseID ORDER BY ModuleID,FunctionID
END;
//来源:C/S框架网(www.csframework.com) QQ:23404761
SQL脚本格式化效果:
SQL Code:
USE [CSFramework_WebAPI]
GO
/****** Object: StoredProcedure [dbo].[usp_sys_Logout] Script Date: 03/02/2020 19:33:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_sys_Logout]
@UserID VARCHAR(50)
AS
BEGIN
/*************************************************
SELECT * FROM dbo.sys_LoginHistory
usp_sys_Logout '8888888xxx8'
usp_sys_Logout '8888888888'
**************************************************/
IF EXISTS(SELECT TOP 1 * FROM dbo.Api_User WHERE Account=@UserID)
BEGIN
INSERT INTO dbo.sys_LoginHistory
( Account ,
LoginType ,
LoginTime ,
DeviceType ,
DeviceMID ,
ClientVersion
)
VALUES ( @UserID, -- Account - varchar(30)
'O' , -- LoginType - varchar(1)
GETDATE() , -- LoginTime - datetime
'' , -- DeviceType - varchar(10)
'' , -- DeviceMID - varchar(50)
'' -- ClientVersion - varchar(20)
)
SELECT '' AS MSG --返回空表示成功
END ELSE
SELECT '账号不存在!' AS MSG
END;
//来源:C/S框架网(www.csframework.com) QQ:23404761
GO
/****** Object: StoredProcedure [dbo].[usp_sys_Logout] Script Date: 03/02/2020 19:33:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_sys_Logout]
@UserID VARCHAR(50)
AS
BEGIN
/*************************************************
SELECT * FROM dbo.sys_LoginHistory
usp_sys_Logout '8888888xxx8'
usp_sys_Logout '8888888888'
**************************************************/
IF EXISTS(SELECT TOP 1 * FROM dbo.Api_User WHERE Account=@UserID)
BEGIN
INSERT INTO dbo.sys_LoginHistory
( Account ,
LoginType ,
LoginTime ,
DeviceType ,
DeviceMID ,
ClientVersion
)
VALUES ( @UserID, -- Account - varchar(30)
'O' , -- LoginType - varchar(1)
GETDATE() , -- LoginTime - datetime
'' , -- DeviceType - varchar(10)
'' , -- DeviceMID - varchar(50)
'' -- ClientVersion - varchar(20)
)
SELECT '' AS MSG --返回空表示成功
END ELSE
SELECT '账号不存在!' AS MSG
END;
//来源:C/S框架网(www.csframework.com) QQ:23404761
参考文章:CodeHighlighter源代码格式化,代码缩进,关键词高亮着色(C#源码)
扫一扫加微信:
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网