欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net的cms 核心代码篇

程序员文章站 2024-03-06 10:10:31
第一篇,我简略描述了一下我的cms标签所表示的含义。ancms(c#版)第一篇绑定数据 第二篇,我将展示了标签背后真正运行的代码。好像开源有点多余,核心代码就下面这些。 复...
第一篇,我简略描述了一下我的cms标签所表示的含义。ancms(c#版)第一篇绑定数据

第二篇,我将展示了标签背后真正运行的代码。

好像开源有点多余,核心代码就下面这些。
复制代码 代码如下:

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;
//}

public static string fnsplitcontentregex = @"\$split\[.*?\]\[.*?\]";
public static string fnsplitregex = @"<!--{split}-->[\s\s]*?<!--{/split}-->";
public static string fnsplit(string content)
{
foreach (string s in method.regexstr(fnsplitregex, content))
{
var list_split = method.regexstr(fnsplitcontentregex, content);
string splitmethod = "";
if (list_split != null)
{
splitmethod = list_split[0].tostring();
}
if (splitmethod != "")
{
string source = "", separator = "";
var list_split_value = splitmethod.split(']');
if (list_split_value.length > 0)
{
source = list_split_value[0].replace("$split[", "");
separator = list_split_value[1].replace("[", "");
if (source != "")
{
string str = "";
if (tv.isinteger(source) == "")
{
str = "<%if(1==1){var arr=a[" + source + "];foreach(var s in arr.split('"+separator+"')){%>";
}
else
{
str = "<%if(1==1){var arr=\"" + source + "\";foreach(var s in arr.split('" + separator + "')){%>";
}
str += s.replace(splitmethod, "<%=s%>").replace("<!--{split}-->", "").replace("<!--{/split}-->", "");
str += "<%}}%>";
content = content.replace(s, str);
}
}

}
}
return content;
}





/// <summary>
/// 将html方法转换成aspx的方法
/// </summary>
/// <param name="methodname"></param>
/// <param name="content"></param>
/// <returns></returns>
public static aspxfiled htmlfiledtoaspxfiled(string methodname, string content)
{
aspxfiled af = new aspxfiled();
hashtable ht = methodfiled(content);
foreach (dictionaryentry h in ht)
{
if (h.key.tostring() == "filed")
{
af.filed = h.value.tostring();
}
if (h.key.tostring() == "num")
{
if (tv.isinteger(h.value.tostring()) == "")
{
af.showcounts = convert.toint32(h.value.tostring());
if (af.showcounts < 0)
{
af.showcounts = 1;
}
}
else
{
af.showcounts = 10;
}
}
if (h.key.tostring() == "sort")
{
af.sort = h.value.tostring();
}
if (h.key.tostring() == "page")
{
if (h.value.tostring() == "true")
{
af.current = "an_current";
}
else
{
af.current = "1";
}
}
if (h.key.tostring() == "category")
{
af.categoryid = h.value.tostring();
}
if (h.key.tostring() == "keyword")
{
af.keyword = h.value.tostring();
}
if (h.key.tostring() == "id")
{
af.id = h.value.tostring();
}
}
return af;
}

public static string aspxmethoddataset(string methodname,string content,string datatablename)
{
aspxfiled af = htmlfiledtoaspxfiled(methodname, content);
string str = "";
if (methodname.contains("pager"))
{
if (methodname == "clistpager ")
{
str = "myhashtable.add(\"" + datatablename + "\", " + convertmethodnamehtmltoaspx(methodname) + "(\"" + af.categoryid + "\"));";
}
else
{
str = "myhashtable.add(\"" + datatablename + "\", " + convertmethodnamehtmltoaspx(methodname) + "(\"" + af.id + "\",\"" + af.categoryid + "\",\"" + af.keyword + "\"));";
}
}
else
{
if (methodname == "clist ")
{
str = "myhashtable.add(\"" + datatablename + "\", " + convertmethodnamehtmltoaspx(methodname) + "(\"" + af.filed + "\", " + af.current + ", " + af.showcounts + ", \"" + af.sort + "\", \"" + af.categoryid + "\"));";
}
else
{
str = "myhashtable.add(\"" + datatablename + "\", " + convertmethodnamehtmltoaspx(methodname) + "(\"" + af.filed + "\", " + af.current + ", " + af.showcounts + ", \"" + af.sort + "\", \"" + af.id + "\", \"" + af.categoryid + "\", \"" + af.keyword + "\"));";
}
}
return str;
}
public static string aspxmethoddatarow(string datatablename,string content)
{
return "<%if(1==1){var myrows = myrows(\"" + datatablename + "\"); if (myrows != null){foreach (var a in myrows){ %>" + content + "<%}}else{%><p style='margin:10px;'>没有相关信息</p><%}}%>";
}

public static string serverfunction(string content)
{
return "<script runat=\"server\">protected override void onload(eventargs e){" + content + "}</script>";
}
public static string serverfunction(string content, string fnname)
{
return "<script runat=\"server\">public void " + fnname + "(){" + content + "}</script>";
}

}

public class aspxfiled
{
private string _filed;
public string filed
{
get{ return _filed; }
set
{
if (value.contains("textfile.id,"))
{
_filed = value;
}
else
{
_filed = "textfile.id," + value;
}
}
}
private string _id = "";
public string id
{
get { return _id; }
set { _id = value; }
}
private string _categoryid = "";
public string categoryid
{
get { return _categoryid; }
set { _categoryid = value; }
}
private string _keyword = "";
public string keyword
{
get { return _keyword; }
set { _keyword = value; }
}

private string _sort;
public string sort
{
get { return _sort; }
set { _sort = value; }
}
private int _showcounts = 0;
public int showcounts
{
get{return _showcounts;}
set { _showcounts = value; }
}
private string _current = "1";
public string current
{
get { return _current; }
set { _current = value;}
}
}
/// <summary>
/// html和aspx的方法名称对应替换
/// </summary>
public class converthtmltoaspx
{
public string datatablename(int num,string name)
{
return method.md5(datetime.now.tolongdatestring() + new random().next(1000) + num + new random().next(1000) + name);
}
public string toaspx(string html,string ascxname)
{
html = alltoaspx(html);
list<string> list_allmethodcontent = htmltoaspxmethod.allmethodcontent(html);
string load = "";
int i = 0;
foreach (string allmethodcontent in list_allmethodcontent)
{
i++;
string allmethod = htmltoaspxmethod.allmethod(allmethodcontent)[0];//<!--{an:list}-->
string methodname = htmltoaspxmethod.methodname(allmethod);//方法名称<!--{an:list
hashtable methodfiled = htmltoaspxmethod.methodfiled(allmethod);//filed=[title] sort=[time desc]
string content = allmethodcontent.replace(allmethod, "").replace("<!--{/an}-->", "");
content = htmltoaspxmethod.valueone(content);
content = htmltoaspxmethod.valueonetostringformat(content);
string datatablename = datatablename(i,ascxname);
load += htmltoaspxmethod.aspxmethoddataset(methodname, allmethod, datatablename);
content = htmltoaspxmethod.aspxmethoddatarow(datatablename, content);
html = html.replace(allmethodcontent, content);
}
if (ascxname == "")
{
string[] js=htmltoaspxmethod.jsinclude(html);
html = js[1];
load += js[0];
load = htmltoaspxmethod.serverfunction(load);
}
else
{
load = htmltoaspxmethod.serverfunction(load, ascxname);
}
html = htmltoaspxmethod.hrefhtml(html);//链接转换
return load + html;
}
private string alltoaspx(string html)
{
html = html.replace("$categoryname", "<%=an_categoryname%>");
//html = html.replace("$title$", "<%=an_title%>").//页面标题
// replace("$keywords$", "<%=an_keywords%>").//页面关键词
// replace("$description$", "<%=an_description%>").//页面描述
// replace("$domain$", "<%=an_domain%>").//网站域名
// replace("$categoryname$", "<%=an_categoryname%>").//分类名称
// replace("$contact$", "\"+an_contact+\"").//联系我们
// replace("$id$", "an_id").//明细id
// replace("$category$", "an_categoryid").//分类id
// replace("$tongji$", "<%=an_tongji%>").
// replace("$keyword$", "\"+an_keyword+\"");//搜索的关键词
html = htmltoaspxmethod.urlroute(html);
html = htmltoaspxmethod.paging(html);
html = htmltoaspxmethod.fnsplit(html);

return html;
}

}
}

呵呵,会不会太肤浅了。

主要就是通过上面这个代码,将html模版通过替换后变成aspx来运行。

我要去完善我的cms啦。