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

  1 using System;2 using System.Collections;3 using System.Collections.Generic;4 using System.Linq;5 using System.Text;6 7 namespace an.helper8 {9     /// <summary>10     /// 查找html页面中可以转换成aspx页面的方法 的正则表达式11     /// </summary>12     public class HtmlToAspxMethod13     {14         private static T_Validate tv=new T_Validate();15         public static string ConvertMethodNameHtmlToAspx(string methodName)16         {17             string str="";18             switch(methodName)19             {20                 case "list ": str = "TextFile"; break;  //文档21                 case "listpager ": str = "TextFilePager"; break; //文档分页22                 case "alist ": str = "Article"; break;//文章列表23                 case "alistpager ": str = "ArticlePager"; break;//文章分页24                 case "plist ": str = "Products"; break;//产品列表25                 case "plistpager ": str = "ProductsPager"; break;//产品分页26                 case "olist ": str = "One"; break;//单页列表27                 case "olistpager ": str = "OnePager"; break;//单页分页28                 case "clist ": str = "Category"; break; //分类列表29                 case "clistpager ": str = "CategoryPager"; break; //分类列表30                 case "llist ": str = "Link"; break;//连接列表31                 case "llistpager ": str = "LinkPager"; break;//连接分页32                 case "ilist ": str = "Images"; break;//图片集列表33                 case "ilistpager ": str = "ImagesPager"; break;//图片集分页34                 default: break;35             }36             return str;37         }38         public static string MethodListName = "list |listpager |alist |alistpager |plist |plistpager |olist |olistpager |clist |clistpager |llist |llistpager |ilist |ilistpager ";39         public static string MethodFiledName = "filed|num|category|keyword|sort|page|id";40         /// <summary>41         /// 所有方法的入口,第一次运行时遍历该方法。42         /// </summary>43         public static string AllMethodContentRegex = "<!--{an:(" + MethodListName + ").*?}-->[\\s\\S]*?<!--{/an}-->";44         public static List<string> AllMethodContent(string content)45         {46             return Method.RegexStr(AllMethodContentRegex, content);47         }48         /// <summary>49         /// 获取所有的方法50         /// </summary>51         public static string AllMethodRegex = "<!--{an:(" + MethodListName + ")[^}]*}-->";52         public static List<string> AllMethod(string content)53         {54             return Method.RegexStr(AllMethodRegex, content);55         }56 57         /// <summary>58         /// 获取方法的函数名称59         /// </summary>60         public static string MethodNameRegex = "<!--{an:(" + MethodListName + ")";61         public static string MethodName(string content)62         {63             string str = "";64             var c=Method.RegexStr(MethodNameRegex, content);65             if (c.Count>0)66             {67                 str = c[0].ToString();68             }69             return str.Replace("<!--{an:", "");70         }71         /// <summary>72         /// 获取方法的字段和值73         /// </summary>        74         public static string MethodFiledRegex = "(" + MethodFiledName + ")=\\[[^]]+]";75         public static Hashtable MethodFiled(string content)76         {           77             Hashtable ht = new Hashtable();78             foreach (string s in Method.RegexStr(MethodFiledRegex, content))79             {80                 string[] arr = s.Split('=');81                 ht.Add(arr[0], arr[1].Replace("[", "").Replace("]", ""));82             }83             return ht;84         }85         public static string JsIncludeRegex = "<script.*rel=\"share\"[^>]*></script>";86         public static string[] JsInclude(string html)87         {88             string js = "";            89             List<string> list_include = Method.RegexStr(JsIncludeRegex, html);90             foreach (string inc in list_include)91             {92                 List<string> includeList = Method.RegexStr(@"\w+(?=\.js)", inc);93                 if (includeList != null)94                 {95                     js += includeList[0] + "(); ";                    96                     html = html.Replace(inc, "<!--#include file=\"Share/" +includeList[0]  + ".ascx\"--> ");97                 }98             }99             return new string[] { js, html };
100         }
101         /// <summary>
102         /// 占位符
103         /// </summary>
104         public static string ValueOneRegex = @"\${\d+}";
105         public static string ValueOne(string content)
106         {
107             foreach (string s in Method.RegexStr(ValueOneRegex, content))
108             {
109                 string num = s.Replace("${", "").Replace("}", "");
110                 content = content.Replace(s, "<%=a[" + num + "]%>");
111             }
112             return content;
113         }
114
115         public static string ValueOneToStringFormartRegex = @"\${\d+\|.*?}";
116         public static string ValueOneToStringFormat(string content)
117         {
118             foreach (string s in Method.RegexStr(ValueOneToStringFormartRegex, content))
119             {
120                 string str = s.Replace("${", "").Replace("}", "");
121                 string[] arr = str.Split('|');
122                 string num = arr[0].ToString();
123                 string formart = arr[1].ToString();
124                 content = content.Replace(s, "<%=TimeFormart(a[" + num + "],\"" + formart + "\")%>");
125             }
126             return content;
127         }
128
129         public static string UrlRouteRegex = @"\$href\[\w+\]\[.*?\]";
130         public static string UrlRoute(string content)
131         {
132             foreach (string s in Method.RegexStr(UrlRouteRegex, content))
133             {
134                 //$href[index][{"aaa","bbb"},{"ccc","ddd"}]
135                 //<%=UrlRoute("",new string[,]{{"aaa","bbb"},{"ccc","ddd"}});%>
136                 string urlRoute = s.Replace("$href[", "<%=UrlRoute(\"").
137                                     Replace("][", "\",new string[,]{ ").
138                                     Replace("#0", "a[0].ToString()").
139                                     Replace("#1", "a[1].ToString()").
140                                     Replace("#2", "a[2].ToString()").
141                                     Replace("#3", "a[3].ToString()").
142                                     Replace("#4", "a[4].ToString()").
143                                     Replace("#5", "a[5].ToString()").
144                                     Replace("#6", "a[6].ToString()").
145                                     Replace("#7", "a[7].ToString()").
146                                     Replace("#8", "a[8].ToString()").
147                                     Replace("#9", "a[9].ToString()");
148                 urlRoute = urlRoute.Substring(0,urlRoute.Length - 1);
149                 urlRoute = urlRoute + "})%>";
150                 content = content.Replace(s, urlRoute);
151             }
152             return content;
153         }
154         public static string PagingRegex = @"\$pager\[\w+\]\[.*?\]\[\d+\]";
155         public static string Paging(string content)
156         {
157             //$pager[list][Category=$category$][15]
158             //<%=Paging("list",new{Category=$category$}, int showCounts, object totleCounts)%>
159             foreach (string s in Method.RegexStr(PagingRegex, content))
160             {
161                 int numSite=s.LastIndexOf('[');
162                 string pager = s.Substring(0, numSite);
163                 string num = s.Substring(numSite).Replace("[", "").Replace("]", "");
164
165                 pager = pager.Replace("$pager[", "<%=Paging(\"").
166                               Replace("][", "\",new string[,]{");
167                 pager = pager.Substring(0, pager.Length - 1) + "}," + num + ",a[0])%>";
168                 content = content.Replace(s, pager);
169             }
170             return content;
171         }
172
173
174         public static string LinkHrefRegex = "(?<=<link.*href=\")[^\"]*";
175         public static string ScriptSrcRegex = "(?<=<script.*src=\")[^\"]*";
176         public static string ImgSrcRegex = "<img.*rel=\"share\"[^>]*/>";
177         public static string TemplatePath = "<%=An_DoMain%>/Templates/";
178
179         public static string HrefHtml(string html)
180         {
181             List<string> list_link = Method.RegexStr(LinkHrefRegex, html);
182             foreach (string link in list_link.Distinct())
183             {
184                 html = html.Replace(link, TemplatePath + link);
185             }
186             List<string> list_script = Method.RegexStr(ScriptSrcRegex, html);
187             foreach (string script in list_script.Distinct())
188             {
189                 html = html.Replace(script, TemplatePath + script);
190             }
191             List<string> list_img = Method.RegexStr(ImgSrcRegex, html);
192             foreach (string img in list_img.Distinct())
193             {
194                 html = html.Replace(img, img.Replace("src=\"", "src=\"" + TemplatePath));
195             }
196             return html;
197         }
198
199
200         //public static string ForRegex = "<!--{for}-->[\\s\\S]*?<!--{/for}-->";
201         //public static string For(string arr, string content)
202         //{
203         //    foreach (string s in Method.RegexStr(ForRegex, content))
204         //    {
205         //        string str = "";
206         //        str = "<%if(1==1){var arr=" + arr + ";foreach(var s in arr){%>";
207         //        str = str + s.Replace("<!--{for}-->", "").Replace("<!--{/for}-->", "") + "<%}}%>";
208         //        content=content.Replace(s,str);
209         //    }
210         //    return content;
211         //}
212
213         public static string FnSplitContentRegex = @"\$split\[.*?\]\[.*?\]";
214         public static string FnSplitRegex = @"<!--{split}-->[\s\S]*?<!--{/split}-->";
215         public static string FnSplit(string content)
216         {
217             foreach (string s in Method.RegexStr(FnSplitRegex, content))
218             {
219                 var list_split = Method.RegexStr(FnSplitContentRegex, content);
220                 string splitMethod = "";
221                 if (list_split != null)
222                 {
223                     splitMethod = list_split[0].ToString();
224                 }
225                 if (splitMethod != "")
226                 {
227                     string source = "", separator = "";
228                     var list_split_value = splitMethod.Split(']');
229                     if (list_split_value.Length > 0)
230                     {
231                         source = list_split_value[0].Replace("$split[", "");
232                         separator = list_split_value[1].Replace("[", "");
233                         if (source != "")
234                         {
235                             string str = "";
236                             if (tv.IsInteger(source) == "")
237                             {
238                                 str = "<%if(1==1){var arr=a[" + source + "];foreach(var s in arr.Split('"+separator+"')){%>";
239                             }
240                             else
241                             {
242                                 str = "<%if(1==1){var arr=\"" + source + "\";foreach(var s in arr.Split('" + separator + "')){%>";
243                             }
244                             str += s.Replace(splitMethod, "<%=s%>").Replace("<!--{split}-->", "").Replace("<!--{/split}-->", "");
245                             str += "<%}}%>";
246                             content = content.Replace(s, str);
247                         }
248                     }
249
250                 }
251             }
252             return content;
253         }
254
255
256
257
258
259         /// <summary>
260         /// 将html方法转换成aspx的方法
261         /// </summary>
262         /// <param name="methodName"></param>
263         /// <param name="content"></param>
264         /// <returns></returns>
265         public static AspxFiled HtmlFiledToAspxFiled(string methodName, string content)
266         {
267             AspxFiled af = new AspxFiled();
268             Hashtable ht = MethodFiled(content);
269             foreach (DictionaryEntry h in ht)
270             {
271                 if (h.Key.ToString() == "filed")
272                 {
273                     af.Filed = h.Value.ToString();
274                 }
275                 if (h.Key.ToString() == "num")
276                 {
277                     if (tv.IsInteger(h.Value.ToString()) == "")
278                     {
279                         af.ShowCounts = Convert.ToInt32(h.Value.ToString());
280                         if (af.ShowCounts < 0)
281                         {
282                             af.ShowCounts = 1;
283                         }
284                     }
285                     else
286                     {
287                         af.ShowCounts = 10;
288                     }
289                 }
290                 if (h.Key.ToString() == "sort")
291                 {
292                     af.Sort = h.Value.ToString();
293                 }
294                 if (h.Key.ToString() == "page")
295                 {
296                     if (h.Value.ToString() == "true")
297                     {
298                         af.Current = "An_Current";
299                     }
300                     else
301                     {
302                         af.Current = "1";
303                     }
304                 }
305                 if (h.Key.ToString() == "category")
306                 {
307                     af.CategoryID = h.Value.ToString();
308                 }
309                 if (h.Key.ToString() == "keyword")
310                 {
311                     af.Keyword = h.Value.ToString();
312                 }
313                 if (h.Key.ToString() == "id")
314                 {
315                     af.ID = h.Value.ToString();
316                 }
317             }
318             return af;
319         }
320
321         public static string AspxMethodDataSet(string methodName,string content,string datatableName)
322         {
323             AspxFiled af = HtmlFiledToAspxFiled(methodName, content);
324             string str = "";
325             if (methodName.Contains("pager"))
326             {
327                 if (methodName == "clistpager ")
328                 {
329                     str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.CategoryID + "\"));";
330                 }
331                 else
332                 {
333                     str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.ID + "\",\"" + af.CategoryID + "\",\"" + af.Keyword + "\"));";
334                 }
335             }
336             else
337             {
338                 if (methodName == "clist ")
339                 {
340                     str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.Filed + "\", " + af.Current + ", " + af.ShowCounts + ", \"" + af.Sort + "\", \"" + af.CategoryID + "\"));";
341                 }
342                 else
343                 {
344                     str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.Filed + "\", " + af.Current + ", " + af.ShowCounts + ", \"" + af.Sort + "\", \"" + af.ID + "\", \"" + af.CategoryID + "\", \"" + af.Keyword + "\"));";
345                 }
346             }
347             return str;
348         }
349         public static string AspxMethodDataRow(string datatableName,string content)
350         {
351             return "<%if(1==1){var myRows = MyRows(\"" + datatableName + "\"); if (myRows != null){foreach (var a in myRows){ %>" + content + "<%}}else{%><p style='margin:10px;'>没有相关信息</p><%}}%>";
352         }
353
354         public static string ServerFunction(string content)
355         {
356             return "<script runat=\"server\">protected override void OnLoad(EventArgs e){" + content + "}</script>";
357         }
358         public static string ServerFunction(string content, string fnName)
359         {
360             return "<script runat=\"server\">public void " + fnName + "(){" + content + "}</script>";
361         }
362
363     }
364
365     public class AspxFiled
366     {
367         private string _Filed;
368         public string Filed
369         {
370             get{ return _Filed; }
371             set
372             {
373                 if (value.Contains("TextFile.ID,"))
374                 {
375                     _Filed = value;
376                 }
377                 else
378                 {
379                     _Filed = "TextFile.ID," + value;
380                 }
381             }
382         }
383         private string _ID = "";
384         public string ID
385         {
386             get { return _ID; }
387             set { _ID = value; }
388         }
389         private string _CategoryID = "";
390         public string CategoryID
391         {
392             get { return _CategoryID; }
393             set { _CategoryID = value; }
394         }
395         private string _Keyword = "";
396         public string Keyword
397         {
398             get { return _Keyword; }
399             set { _Keyword = value; }
400         }
401
402         private string _Sort;
403         public string Sort
404         {
405             get { return _Sort; }
406             set { _Sort = value; }
407         }
408         private int _ShowCounts = 0;
409         public int ShowCounts
410         {
411             get{return _ShowCounts;}
412             set { _ShowCounts = value; }
413         }
414         private string _Current = "1";
415         public string Current
416         {
417             get { return _Current; }
418             set { _Current = value;}
419         }
420     }
421     /// <summary>
422     /// html和aspx的方法名称对应替换
423     /// </summary>
424     public class ConvertHtmlToAspx
425     {
426         public string DataTableName(int num,string name)
427         {
428             return Method.Md5(DateTime.Now.ToLongDateString() + new Random().Next(1000) + num + new Random().Next(1000) + name);
429         }
430         public string ToAspx(string html,string ascxName)
431         {
432             html = AllToAspx(html);
433             List<string> list_AllMethodContent = HtmlToAspxMethod.AllMethodContent(html);
434             string load = "";
435             int i = 0;
436             foreach (string allMethodContent in list_AllMethodContent)
437             {
438                 i++;
439                 string allMethod = HtmlToAspxMethod.AllMethod(allMethodContent)[0];//<!--{an:list}-->
440                 string methodName = HtmlToAspxMethod.MethodName(allMethod);//方法名称<!--{an:list
441                 Hashtable methodFiled = HtmlToAspxMethod.MethodFiled(allMethod);//filed=[title] sort=[time desc]
442                 string content = allMethodContent.Replace(allMethod, "").Replace("<!--{/an}-->", "");
443                 content = HtmlToAspxMethod.ValueOne(content);
444                 content = HtmlToAspxMethod.ValueOneToStringFormat(content);
445                 string dataTableName = DataTableName(i,ascxName);
446                 load += HtmlToAspxMethod.AspxMethodDataSet(methodName, allMethod, dataTableName);
447                 content = HtmlToAspxMethod.AspxMethodDataRow(dataTableName, content);
448                 html = html.Replace(allMethodContent, content);
449             }
450             if (ascxName == "")
451             {
452                 string[] js=HtmlToAspxMethod.JsInclude(html);
453                 html = js[1];
454                 load += js[0];
455                 load = HtmlToAspxMethod.ServerFunction(load);
456             }
457             else
458             {
459                 load = HtmlToAspxMethod.ServerFunction(load, ascxName);
460             }
461             html = HtmlToAspxMethod.HrefHtml(html);//链接转换
462             return load + html;
463         }
464         private string AllToAspx(string html)
465         {
466             html = html.Replace("$categoryname", "<%=An_CategoryName%>");
467             //html = html.Replace("$title$", "<%=An_Title%>").//页面标题
468             //            Replace("$keywords$", "<%=An_KeyWords%>").//页面关键词
469             //            Replace("$description$", "<%=An_Description%>").//页面描述
470             //            Replace("$domain$", "<%=An_DoMain%>").//网站域名
471             //            Replace("$categoryname$", "<%=An_CategoryName%>").//分类名称
472             //            Replace("$contact$", "\"+An_Contact+\"").//联系我们
473             //            Replace("$id$", "An_ID").//明细ID
474             //            Replace("$category$", "An_CategoryID").//分类ID
475             //            Replace("$tongji$", "<%=An_TongJi%>").
476             //            Replace("$keyword$", "\"+An_KeyWord+\"");//搜索的关键词
477             html = HtmlToAspxMethod.UrlRoute(html);
478             html = HtmlToAspxMethod.Paging(html);
479             html = HtmlToAspxMethod.FnSplit(html);
480
481             return html;
482         }
483
484     }
485 }

收藏地址:http://www.cnblogs.com/19aspx/archive/2012/12/16/2820558.html

转载于:https://www.cnblogs.com/chennie/archive/2012/12/22/2829097.html

asp.net的cms 核心代码篇相关推荐

  1. ASP.NET中常用功能代码总结(1)——发送邮件篇

    ASP.NET中常用功能代码总结(1)--发送邮件篇<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office: ...

  2. Python科学计算库核心知识点总结_代码篇(ML/DL依赖语法)

    Python科学计算库核心知识点总结_代码篇(ML/DL依赖语法)                                                                    ...

  3. 微信公众号抢现金红包活动的核心代码分析(asp.net C#)

    今年春节微信抢红包,我想各位都还记得.最近很多商家也在使用公众号给粉丝发红包,做营销活动.吸粉活动或者是反馈老用户等. 我们作为第3方开发者,就义不容辞的来给这些商家服务了.首先我们得会使用程序来写抢 ...

  4. [收藏] 最受欢迎的ASP.NET的CMS下载

    摘要:本文向您总结了13款最受欢迎的ASP.NET的CMS下载,供开发者学习.参考. 1. Umbraco 项目地址 下载 Umbraco是一个开放源码的CMS内容管理系统,基于asp.net建立,使 ...

  5. asp.net开源CMS推荐

    随着网络技术的发展,目前国内CMS的开发商越来越多,各自都有其独特的优势,大家在选择的时候觉得眼花缭乱,不知道选择哪个比较好,我个人认为开源的CMS还是适合我们学习及研究使用,下边就几个国内的asp. ...

  6. ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第二篇:第一个页面

    摘要       本文首先一步一步完成Demo的第一个页面--首页.然后根据实现过程,说明一下其中用到的与ASP.NET MVC相关的概念与原理. 让第一个页面跑起来       现在,我们来实现公告 ...

  7. 我心中的ASP.NET Core 新核心对象WebHost(二)

    这是ASP.NET Core新核心对象系列的第二篇,上一篇 WebHost准备阶段 我们讲到了WebHostBuilder的初始化及配置.我们给WebHostBuilder进行以下配置 UseKest ...

  8. asp.net开源CMS汇总

    列举出自己见过的asp.net开源cms,方便初学者入门学习,如果有更好的开源cms请在评论中推荐,谢谢! 1. DotNetNuke(ASP.NET 2.0) 个人推荐深入研究 DotNetNuke ...

  9. 最全的ASP.NET开源CMS汇总

    国内: 1.SiteServer CMS SiteServer CMS 网站内容管理系统(著作权登记号2008SR15710)是定位于中高端市场的CMS内容管理系统,能 最近汇总了一些asp.net开 ...

最新文章

  1. 如何入门生信Linux
  2. Silverlight C# 游戏开发:Flyer03大图裁剪,高效动画的艺术
  3. html5 网页桌面图标,打开控制面板是网页,而且桌面上很多图标变成网页
  4. 回溯算法之购物车(0-1 背包问题)
  5. 小程序购物车抛物线(贝塞尔曲线实现)
  6. 计算机显示休眠状态进不去,解决方法:关闭计算机,或使其进入睡眠或休眠状态...
  7. Python 检测系统时间,k8s版本,redis集群,etcd,mysql,ceph,kafka
  8. 先有产品管理,后有产品经理
  9. 【Struts1】--beanutils
  10. 推荐记录片系列:Ultimate Factories系列和MegaStructures系列
  11. MMORPG大型游戏设计与开发(构架)
  12. 关于设计思维的理解与思考
  13. java吊打面试官系列,java高级程序员面试笔试宝典蔡羽
  14. 洛谷 2197 nim游戏
  15. 哔哩大学计算机学院跟着B站学C语言
  16. 0.96寸OLED显示屏驱动手册(SSD1306)
  17. GitHub提交出错处理【2022年】
  18. python神经网络算法pdf_高清图解:神经网络、机器学习、数据科学一网打尽|附PDF...
  19. 恩智浦智能车MOS双驱动
  20. jdk8升级成jdk17报错 module java.base does not “opens java.lang“ to unnamed module @1941a8ff

热门文章

  1. VMware排错:Number of virtual devices exceeds the maximum for a given controller
  2. [JavaScript]牛人的JS是怎么玩的
  3. 计算机专业申请,申请计算机专业
  4. java命令行生成jar_命令行生成可执行的jar包
  5. pip19离线_更新pip为20后不显示下载链接无法离线下载回退pip版本
  6. opencv-python 9.3 图像 ROI
  7. hbuilder能断点吗_知乎点赞破4万!这些PPT小秘密你知道吗?
  8. 华为机试第九题python
  9. web后端轻量级框架flask基础调用程序模板
  10. 使用vue的mixins混入实现对正在编辑的页面离开时提示