asp.net的cms 核心代码篇
编程学习 2021-07-04 22:40www.dzhlxh.cn编程入门
第一篇:我简略描述了一下我的cms标签所表示的含义;anCMS(c#版)第一篇绑定数据;第二篇:我将展示了标签背后真正运行的代码,本文将介绍asp.net的cms 核心代码篇
第一篇,我简略描述了一下我的cms标签所表示的含义。
第二篇,我将展示了标签背后真正运行的代码。
好像开源有点多余,核心代码就狼蚁网站SEO优化这些。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace an.helper
{
/// <summary>
/// 查找html页面中可以转换成aspx页面的方法 的正则表达式
/// </summary>
public class HtmlToAspxMethod
{
private static T_Validate tv=new T_Validate();
public static string ConvertMethodNameHtmlToAspx(string methodName)
{
string str="";
switch(methodName)
{
case "list ": str = "TextFile"; break; //文档
case "listpager ": str = "TextFilePager"; break; //文档分页
case "alist ": str = "Article"; break;//文章列表
case "alistpager ": str = "ArticlePager"; break;//文章分页
case "plist ": str = "Products"; break;//产品列表
case "plistpager ": str = "ProductsPager"; break;//产品分页
case "olist ": str = "One"; break;//单页列表
case "olistpager ": str = "OnePager"; break;//单页分页
case "clist ": str = "Category"; break; //分类列表
case "clistpager ": str = "CategoryPager"; break; //分类列表
case "llist ": str = "Link"; break;//连接列表
case "llistpager ": str = "LinkPager"; break;//连接分页
case "ilist ": str = "Images"; break;//图片集列表
case "ilistpager ": str = "ImagesPager"; break;//图片集分页
default: break;
}
return str;
}
public static string MethodListName = "list |listpager |alist |alistpager |plist |plistpager |olist |olistpager |clist |clistpager |llist |llistpager |ilist |ilistpager ";
public static string MethodFiledName = "filed|num|category|keyword|sort|page|id";
/// <summary>
/// 所有方法的入口,第一次运行时遍历该方法。
/// </summary>
public static string AllMethodContentRegex = "<!--{an:(" + MethodListName + ").*?}-->[\\s\\S]*?<!--{/an}-->";
public static List<string> AllMethodContent(string content)
{
return Method.RegexStr(AllMethodContentRegex, content);
}
/// <summary>
/// 获取所有的方法
/// </summary>
public static string AllMethodRegex = "<!--{an:(" + MethodListName + ")[^}]*}-->";
public static List<string> AllMethod(string content)
{
return Method.RegexStr(AllMethodRegex, content);
}
/// <summary>
/// 获取方法的函数名称
/// </summary>
public static string MethodNameRegex = "<!--{an:(" + MethodListName + ")";
public static string MethodName(string content)
{
string str = "";
var c=Method.RegexStr(MethodNameRegex, content);
if (c.Count>0)
{
str = c[0].ToString();
}
return str.Replace("<!--{an:", "");
}
/// <summary>
/// 获取方法的字段和值
/// </summary>
public static string MethodFiledRegex = "(" + MethodFiledName + ")=\\[[^]]+]";
public static Hashtable MethodFiled(string content)
{
Hashtable ht = new Hashtable();
foreach (string s in Method.RegexStr(MethodFiledRegex, content))
{
string[] arr = s.Split('=');
ht.Add(arr[0], arr[1].Replace("[", "").Replace("]", ""));
}
return ht;
}
public static string JsIncludeRegex = "<script.*rel=\"share\"[^>]*></script>";
public static string[] JsInclude(string html)
{
string js = "";
List<string> list_include = Method.RegexStr(JsIncludeRegex, html);
foreach (string inc in list_include)
{
List<string> includeList = Method.RegexStr(@"\w+(?=\.js)", inc);
if (includeList != null)
{
js += includeList[0] + "(); ";
html = html.Replace(inc, "<!--#include file=\"Share/" +includeList[0] + ".ascx\"--> ");
}
}
return new string[] { js, html };
}
/// <summary>
/// 占位符
/// </summary>
public static string ValueOneRegex = @"\${\d+}";
public static string ValueOne(string content)
{
foreach (string s in Method.RegexStr(ValueOneRegex, content))
{
string num = s.Replace("${", "").Replace("}", "");
content = content.Replace(s, "<%=a[" + num + "]%>");
}
return content;
}
public static string ValueOneToStringFormartRegex = @"\${\d+\|.*?}";
public static string ValueOneToStringFormat(string content)
{
foreach (string s in Method.RegexStr(ValueOneToStringFormartRegex, content))
{
string str = s.Replace("${", "").Replace("}", "");
string[] arr = str.Split('|');
string num = arr[0].ToString();
string formart = arr[1].ToString();
content = content.Replace(s, "<%=TimeFormart(a[" + num + "],\"" + formart + "\")%>");
}
return content;
}
public static string UrlRouteRegex = @"\$href\[\w+\]\[.*?\]";
public static string UrlRoute(string content)
{
foreach (string s in Method.RegexStr(UrlRouteRegex, content))
{
//$href[index][{"aaa","bbb"},{"ccc","ddd"}]
//<%=UrlRoute("",new string[,]{{"aaa","bbb"},{"ccc","ddd"}});%>
string urlRoute = s.Replace("$href[", "<%=UrlRoute(\"").
Replace("][", "\",new string[,]{ ").
Replace("#0", "a[0].ToString()").
Replace("#1", "a[1].ToString()").
Replace("#2", "a[2].ToString()").
Replace("#3", "a[3].ToString()").
Replace("#4", "a[4].ToString()").
Replace("#5", "a[5].ToString()").
Replace("#6", "a[6].ToString()").
Replace("#7", "a[7].ToString()").
Replace("#8", "a[8].ToString()").
Replace("#9", "a[9].ToString()");
urlRoute = urlRoute.Substring(0,urlRoute.Length - 1);
urlRoute = urlRoute + "})%>";
content = content.Replace(s, urlRoute);
}
return content;
}
public static string PagingRegex = @"\$pager\[\w+\]\[.*?\]\[\d+\]";
public static string Paging(string content)
{
//$pager[list][Category=$category$][15]
//<%=Paging("list",new{Category=$category$}, int showCounts, object totleCounts)%>
foreach (string s in Method.RegexStr(PagingRegex, content))
{
int numSite=s.LastIndexOf('[');
string pager = s.Substring(0, numSite);
string num = s.Substring(numSite).Replace("[", "").Replace("]", "");
pager = pager.Replace("$pager[", "<%=Paging(\"").
Replace("][", "\",new string[,]{");
pager = pager.Substring(0, pager.Length - 1) + "}," + num + ",a[0])%>";
content = content.Replace(s, pager);
}
return content;
}
public static string LinkHrefRegex = "(?<=<link.*href=\")[^\"]*";
public static string ScriptSrcRegex = "(?<=<script.*src=\")[^\"]*";
public static string ImgSrcRegex = "<img.*rel=\"share\"[^>]*/>";
public static string TemplatePath = "<%=An_DoMain%>/Templates/";
public static string HrefHtml(string html)
{
List<string> list_link = Method.RegexStr(LinkHrefRegex, html);
foreach (string link in list_link.Distinct())
{
html = html.Replace(link, TemplatePath + link);
}
List<string> list_script = Method.RegexStr(ScriptSrcRegex, html);
foreach (string script in list_script.Distinct())
{
html = html.Replace(script, TemplatePath + script);
}
List<string> list_img = Method.RegexStr(ImgSrcRegex, html);
foreach (string img in list_img.Distinct())
{
html = html.Replace(img, img.Replace("src=\"", "src=\"" + TemplatePath));
}
return html;
}
//public static string ForRegex = "<!--{for}-->[\\s\\S]*?<!--{/for}-->";
//public static string For(string arr, string content)
//{
// foreach (string s in Method.RegexStr(ForRegex, content))
// {
// string str = "";
// str = "<%if(1==1){var arr=" + arr + ";foreach(var s in arr){%>";
// str = str + s.Replace("<!--{for}-->", "").Replace("<!--{/for}-->", "") + "<%}}%>";
// content=content.Replace(s,str);
// }
// return content;
/
第二篇,我将展示了标签背后真正运行的代码。
好像开源有点多余,核心代码就狼蚁网站SEO优化这些。
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace an.helper
{
/// <summary>
/// 查找html页面中可以转换成aspx页面的方法 的正则表达式
/// </summary>
public class HtmlToAspxMethod
{
private static T_Validate tv=new T_Validate();
public static string ConvertMethodNameHtmlToAspx(string methodName)
{
string str="";
switch(methodName)
{
case "list ": str = "TextFile"; break; //文档
case "listpager ": str = "TextFilePager"; break; //文档分页
case "alist ": str = "Article"; break;//文章列表
case "alistpager ": str = "ArticlePager"; break;//文章分页
case "plist ": str = "Products"; break;//产品列表
case "plistpager ": str = "ProductsPager"; break;//产品分页
case "olist ": str = "One"; break;//单页列表
case "olistpager ": str = "OnePager"; break;//单页分页
case "clist ": str = "Category"; break; //分类列表
case "clistpager ": str = "CategoryPager"; break; //分类列表
case "llist ": str = "Link"; break;//连接列表
case "llistpager ": str = "LinkPager"; break;//连接分页
case "ilist ": str = "Images"; break;//图片集列表
case "ilistpager ": str = "ImagesPager"; break;//图片集分页
default: break;
}
return str;
}
public static string MethodListName = "list |listpager |alist |alistpager |plist |plistpager |olist |olistpager |clist |clistpager |llist |llistpager |ilist |ilistpager ";
public static string MethodFiledName = "filed|num|category|keyword|sort|page|id";
/// <summary>
/// 所有方法的入口,第一次运行时遍历该方法。
/// </summary>
public static string AllMethodContentRegex = "<!--{an:(" + MethodListName + ").*?}-->[\\s\\S]*?<!--{/an}-->";
public static List<string> AllMethodContent(string content)
{
return Method.RegexStr(AllMethodContentRegex, content);
}
/// <summary>
/// 获取所有的方法
/// </summary>
public static string AllMethodRegex = "<!--{an:(" + MethodListName + ")[^}]*}-->";
public static List<string> AllMethod(string content)
{
return Method.RegexStr(AllMethodRegex, content);
}
/// <summary>
/// 获取方法的函数名称
/// </summary>
public static string MethodNameRegex = "<!--{an:(" + MethodListName + ")";
public static string MethodName(string content)
{
string str = "";
var c=Method.RegexStr(MethodNameRegex, content);
if (c.Count>0)
{
str = c[0].ToString();
}
return str.Replace("<!--{an:", "");
}
/// <summary>
/// 获取方法的字段和值
/// </summary>
public static string MethodFiledRegex = "(" + MethodFiledName + ")=\\[[^]]+]";
public static Hashtable MethodFiled(string content)
{
Hashtable ht = new Hashtable();
foreach (string s in Method.RegexStr(MethodFiledRegex, content))
{
string[] arr = s.Split('=');
ht.Add(arr[0], arr[1].Replace("[", "").Replace("]", ""));
}
return ht;
}
public static string JsIncludeRegex = "<script.*rel=\"share\"[^>]*></script>";
public static string[] JsInclude(string html)
{
string js = "";
List<string> list_include = Method.RegexStr(JsIncludeRegex, html);
foreach (string inc in list_include)
{
List<string> includeList = Method.RegexStr(@"\w+(?=\.js)", inc);
if (includeList != null)
{
js += includeList[0] + "(); ";
html = html.Replace(inc, "<!--#include file=\"Share/" +includeList[0] + ".ascx\"--> ");
}
}
return new string[] { js, html };
}
/// <summary>
/// 占位符
/// </summary>
public static string ValueOneRegex = @"\${\d+}";
public static string ValueOne(string content)
{
foreach (string s in Method.RegexStr(ValueOneRegex, content))
{
string num = s.Replace("${", "").Replace("}", "");
content = content.Replace(s, "<%=a[" + num + "]%>");
}
return content;
}
public static string ValueOneToStringFormartRegex = @"\${\d+\|.*?}";
public static string ValueOneToStringFormat(string content)
{
foreach (string s in Method.RegexStr(ValueOneToStringFormartRegex, content))
{
string str = s.Replace("${", "").Replace("}", "");
string[] arr = str.Split('|');
string num = arr[0].ToString();
string formart = arr[1].ToString();
content = content.Replace(s, "<%=TimeFormart(a[" + num + "],\"" + formart + "\")%>");
}
return content;
}
public static string UrlRouteRegex = @"\$href\[\w+\]\[.*?\]";
public static string UrlRoute(string content)
{
foreach (string s in Method.RegexStr(UrlRouteRegex, content))
{
//$href[index][{"aaa","bbb"},{"ccc","ddd"}]
//<%=UrlRoute("",new string[,]{{"aaa","bbb"},{"ccc","ddd"}});%>
string urlRoute = s.Replace("$href[", "<%=UrlRoute(\"").
Replace("][", "\",new string[,]{ ").
Replace("#0", "a[0].ToString()").
Replace("#1", "a[1].ToString()").
Replace("#2", "a[2].ToString()").
Replace("#3", "a[3].ToString()").
Replace("#4", "a[4].ToString()").
Replace("#5", "a[5].ToString()").
Replace("#6", "a[6].ToString()").
Replace("#7", "a[7].ToString()").
Replace("#8", "a[8].ToString()").
Replace("#9", "a[9].ToString()");
urlRoute = urlRoute.Substring(0,urlRoute.Length - 1);
urlRoute = urlRoute + "})%>";
content = content.Replace(s, urlRoute);
}
return content;
}
public static string PagingRegex = @"\$pager\[\w+\]\[.*?\]\[\d+\]";
public static string Paging(string content)
{
//$pager[list][Category=$category$][15]
//<%=Paging("list",new{Category=$category$}, int showCounts, object totleCounts)%>
foreach (string s in Method.RegexStr(PagingRegex, content))
{
int numSite=s.LastIndexOf('[');
string pager = s.Substring(0, numSite);
string num = s.Substring(numSite).Replace("[", "").Replace("]", "");
pager = pager.Replace("$pager[", "<%=Paging(\"").
Replace("][", "\",new string[,]{");
pager = pager.Substring(0, pager.Length - 1) + "}," + num + ",a[0])%>";
content = content.Replace(s, pager);
}
return content;
}
public static string LinkHrefRegex = "(?<=<link.*href=\")[^\"]*";
public static string ScriptSrcRegex = "(?<=<script.*src=\")[^\"]*";
public static string ImgSrcRegex = "<img.*rel=\"share\"[^>]*/>";
public static string TemplatePath = "<%=An_DoMain%>/Templates/";
public static string HrefHtml(string html)
{
List<string> list_link = Method.RegexStr(LinkHrefRegex, html);
foreach (string link in list_link.Distinct())
{
html = html.Replace(link, TemplatePath + link);
}
List<string> list_script = Method.RegexStr(ScriptSrcRegex, html);
foreach (string script in list_script.Distinct())
{
html = html.Replace(script, TemplatePath + script);
}
List<string> list_img = Method.RegexStr(ImgSrcRegex, html);
foreach (string img in list_img.Distinct())
{
html = html.Replace(img, img.Replace("src=\"", "src=\"" + TemplatePath));
}
return html;
}
//public static string ForRegex = "<!--{for}-->[\\s\\S]*?<!--{/for}-->";
//public static string For(string arr, string content)
//{
// foreach (string s in Method.RegexStr(ForRegex, content))
// {
// string str = "";
// str = "<%if(1==1){var arr=" + arr + ";foreach(var s in arr){%>";
// str = str + s.Replace("<!--{for}-->", "").Replace("<!--{/for}-->", "") + "<%}}%>";
// content=content.Replace(s,str);
// }
// return content;
/