///// <summary> ///// loop标签的匹配替换 ///// </summary> ///// <param name="template">模板内容</param> public string LoopTag(string template) { string Attributes = ""; string Text = ""; string AllText = ""; Regex r = new Regex(@"(\[SG:loop\s+(?<attributes>[^\]]*?)\](?<text>[\s\S]*?)\[/SG:loop\])", RegexOptions.Compiled | RegexOptions.IgnoreCase); foreach (Match m in r.Matches(template)) { Attributes = m.Groups["attributes"].ToString(); //循环属性集 Text = m.Groups["text"].ToString(); //循环的内容不包含SG:Loop AllText = m.Groups[0].Value.ToString(); //整个匹配的内容包含SG:Loop #region 取得相关属性 string NewsCount = null; //调用数量 string TitleNum = null; //新闻标题的显示字符数量 string ding = null; string tuijian = null; string NewsType = null; //调用新闻的类型 string Templet = null; // 模板ID string SQL = null; NewsCount = TagVal(Attributes, "NewsCount"); if (NewsCount == null) { NewsCount = "15"; } TitleNum = TagVal(Attributes, "TitleNum"); if (TitleNum == null) { TitleNum = "100"; } ding = TagVal(Attributes, "ding"); if (ding == null) { ding = "false"; } tuijian = TagVal(Attributes, "tuijian"); if (tuijian == null) { tuijian = "false"; } NewsType = TagVal(Attributes, "NewsType"); if (NewsType == null) { NewsType = "0"; } Templet = TagVal(Attributes, "Templet"); SQL = TagVal(Attributes, "SQL"); #endregion #region 构造 查询SQL语句 //SQL语句拼接 string strWher = ""; StringBuilder strSql = new StringBuilder(); if (SQL == null) { strWher = "classname='" + NewsType + "'"; strSql.Append("select top "); strSql.Append(NewsCount); strSql.Append(" * from [" + config.date_prefix + "news] where "); strSql.Append(strWher); strSql.Append(" AND switchs='0'"); if (ding == "true") { strSql.Append(" AND ding='0'"); } if (tuijian == "true") { strSql.Append(" AND tuijian='0'"); } strSql.Append(" order by switchs,bh DESC"); } else { strSql.Append(SQL); } #endregion #region 对循环的内容进行替换 string content = ""; DataSet dt = Bll.GetDataSet(strSql.ToString()); if (dt.Tables[0].Rows.Count > 0) { int rowsCount = dt.Tables[0].Rows.Count; for (int n = 0; n < rowsCount; n++) { string str = Text; str = Replace(str, @"\[SG:Title\]", ROYcms.Common.input.GetSubString(dt.Tables[0].Rows[n]["title"].ToString(), Convert.ToInt32(TitleNum))); str = Replace(str, @"\[SG:Id\]", dt.Tables[0].Rows[n]["bh"].ToString()); str = Replace(str, @"\[SG:Url\]", dt.Tables[0].Rows[n]["url"].ToString()); str = Replace(str, @"\[SG:Link\]", dt.Tables[0].Rows[n]["jumpurl"].ToString() == "" ? (config.web_host + TemplateZone.Zpath(this.pageHost) + "show" + Templet + "-" + dt.Tables[0].Rows[n]["bh"].ToString() + "-" + Convert.ToDateTime(dt.Tables[0].Rows[n]["time"]).ToString("yyyyMM") + config.web_forge) : dt.Tables[0].Rows[n]["jumpurl"].ToString()); str = Replace(str, @"\[SG:_Link\]", dt.Tables[0].Rows[n]["jumpurl"].ToString() == "" ? ("show-" + dt.Tables[0].Rows[n]["bh"].ToString() + "-" + Convert.ToDateTime(dt.Tables[0].Rows[n]["time"]).ToString("yyyyMM") + config.web_forge) : dt.Tables[0].Rows[n]["jumpurl"].ToString()); str = Replace(str, @"\[SG:Pic\]", dt.Tables[0].Rows[n]["pic"].ToString()); str = Replace(str, @"\[SG:Zhaiyao\]", dt.Tables[0].Rows[n]["zhaiyao"].ToString()); str = Replace(str, @"\[SG:Keyword\]", dt.Tables[0].Rows[n]["keyword"].ToString()); str = Replace(str, @"\[SG:classname\]", dt.Tables[0].Rows[n]["classname"].ToString()); str = Replace(str, @"\[SG:Content\]", dt.Tables[0].Rows[n]["contents"].ToString()); str = Replace(str, @"\[SG:Infor\]", dt.Tables[0].Rows[n]["infor"].ToString()); str = Replace(str, @"\[SG:Author\]", dt.Tables[0].Rows[n]["author"].ToString()); str = Replace(str, @"\[SG:Tag\]", dt.Tables[0].Rows[n]["tag"].ToString()); str = Replace(str, @"\[SG:Dig\]", dt.Tables[0].Rows[n]["dig"].ToString()); str = Replace(str, @"\[SG:Hits\]", dt.Tables[0].Rows[n]["hits"].ToString()); str = Replace(str, @"\[SG:Time\]", dt.Tables[0].Rows[n]["time"].ToString()); str = Replace(str, @"\[SG:Year\]", Convert.ToDateTime(dt.Tables[0].Rows[n]["time"]).Date.Year.ToString()); str = Replace(str, @"\[SG:Month\]", Convert.ToDateTime(dt.Tables[0].Rows[n]["time"]).Date.Month.ToString()); str = Replace(str, @"\[SG:Day\]", Convert.ToDateTime(dt.Tables[0].Rows[n]["time"]).Date.Day.ToString()); content += str; } } else { content = ""; } #endregion template = template.Replace(AllText, content); } return(template); }
/// <summary> /// 新闻子页面生成方法 News the HTML. /// </summary> /// <param name="Model">The model.</param> /// <returns></returns> public bool NewHtml(ROYcms.Sys.Model.ROYcms_class Model) { // StreamWriter sw = null;//定义参数 Model.FilePath = Model.FilePath.Replace("{cmspath}", "~"); string Path = ""; string templatepath = Model.TemplateShow;//模版地址 string SQL = null; if (this.bh != null) { SQL = "SELECT * FROM [ROYcms_news] " + " where bh=" + this.bh + ""; } else { SQL = "SELECT " + (this.top == null ? "" : "TOP " + this.top) + " * FROM [ROYcms_news] " + (this.star_id == null ? "" : " where bh>" + this.star_id) + (this.end_id == null ? "" : (this._star_id == null ? " where" : " and ") + " bh<" + this.end_id) + " ORDER BY bh desc"; } //写文件 try { DataSet ds = BLL.GetDataSet(SQL); DataTable dt = ds.Tables[0]; //读取模版 string Text = PublicTag(SystemCms.Read_File(HttpContext.Current.Server.MapPath(templatepath), ROYcms.Config.ROYcmsConfig.GetCmsConfigValue("templet_language"))); //列表标签过滤 Template.ROYcms_class_Model = Model;//全局赋值 Text = Template.LoopTag(Text); foreach (DataRow dr in dt.Rows) { Path = (Model.ShowRules.Contains("~/") ? Model.ShowRules : (Model.FilePath + Model.ShowRules));//文件生成地址 Path = Path.Replace("{yyyy}", Convert.ToDateTime(dr["time"]).ToString("yyyy")); Path = Path.Replace("{MM}", Convert.ToDateTime(dr["time"]).ToString("MM")); Path = Path.Replace("{dd}", Convert.ToDateTime(dr["time"]).ToString("dd")); Path = Path.Replace("{id}", dr["bh"].ToString()); string tempath = Path.Substring(0, Path.LastIndexOf("/") + 1); if (!Directory.Exists(HttpContext.Current.Server.MapPath(tempath))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(tempath)); } using (StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath(Path), false, Encoding.GetEncoding(ROYcms.Config.ROYcmsConfig.GetCmsConfigValue("templet_language")))) { try { //sw = new StreamWriter(HttpContext.Current.Server.MapPath(Path), false, Encoding.GetEncoding(config.templet_language)); string str = Text; str = Template.Replace(str, @"\[SG:Title\]", dr["title"].ToString()); str = Template.Replace(str, @"\[SG:Id\]", dr["bh"].ToString()); str = Template.Replace(str, @"\[SG:Url\]", dr["url"].ToString()); str = Template.Replace(str, @"\[SG:Pic\]", dr["pic"].ToString()); str = Template.Replace(str, @"\[SG:Zhaiyao\]", dr["zhaiyao"].ToString()); str = Template.Replace(str, @"\[SG:Keyword\]", dr["keyword"].ToString()); str = Template.Replace(str, @"\[SG:classname\]", dr["classname"].ToString()); str = Template.Replace(str, @"\[SG:Content\]", dr["contents"].ToString()); str = Template.Replace(str, @"\[SG:Infor\]", dr["infor"].ToString()); str = Template.Replace(str, @"\[SG:Author\]", dr["author"].ToString()); str = Template.Replace(str, @"\[SG:Tag\]", dr["tag"].ToString()); str = Template.Replace(str, @"\[SG:Dig\]", dr["dig"].ToString()); str = Template.Replace(str, @"\[SG:Hits\]", dr["hits"].ToString()); str = Template.Replace(str, @"\[SG:Time\]", dr["time"].ToString()); str = Template.Replace(str, @"\[SG:Year\]", Convert.ToDateTime(dr["time"]).ToString("yyyy")); str = Template.Replace(str, @"\[SG:Month\]", Convert.ToDateTime(dr["time"]).ToString("MM")); str = Template.Replace(str, @"\[SG:Day\]", Convert.ToDateTime(dr["time"]).ToString("dd")); // str = Template.Replace(str, @"\[SG:Day\]", Xml.GetValue(Convert.ToInt32(dr["bh"]), dr["type"].ToString(), "o")); sw.WriteLine(str); sw.Flush(); err = true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); err = false; } finally { sw.Close(); } } } } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); err = false; } return(err); }
/// <summary> /// 文章列表 频道 生成方法 循环标签的匹配替换 News the HTML. /// </summary> /// <param name="Model">The model.ROYcms_class 表对象</param> /// <returns></returns> public bool NewHtml(ROYcms.Sys.Model.ROYcms_class Model) { //定义参数 //StreamWriter sw = null; Model.FilePath = Model.FilePath.Replace("{cmspath}", "~"); string Path = ""; string ShowPath = ""; string templatepath = null; //模版地址 string HTML = ""; //模版内容 //最终列表栏目模式 if (Model.ColumnsType == 0) { templatepath = Model.TemplateList;//模版地址 // 生成默认页面开始 string TemplateIndexHTML = PublicTag(SystemCms.Read_File(HttpContext.Current.Server.MapPath(templatepath), ROYcms.Config.ROYcmsConfig.GetCmsConfigValue("templet_language"))); string TemplateIndexPath = (Model.FilePath + Model.DefaultFile); //文件生成地址 NewMain(TemplateIndexPath, TemplateIndexHTML); //生成默认页面结束 int onepage = (this.onepage == null ? 10 : Convert.ToInt32(this.onepage)); int Count = new ROYcms.Sys.BLL.ROYcms_news().GetCount((this.classname == null ? "" : " classname=" + this.classname)); int allpages = Count / onepage; string Text = ""; string AllText = ""; string content = ""; Regex r = new Regex(@"(\[SG:loop\s+(?<attributes>[^\]]*?)\](?<text>[\s\S]*?)\[/SG:loop\])", RegexOptions.Compiled | RegexOptions.IgnoreCase); for (int i = 0; i < (allpages < 1 ? 1 : allpages); i++) { HTML = PublicTag(SystemCms.Read_File(HttpContext.Current.Server.MapPath(templatepath), ROYcms.Config.ROYcmsConfig.GetCmsConfigValue("templet_language"))); Path = (Model.ListeRules.Contains("~/") ? Model.ListeRules : (Model.FilePath + Model.ListeRules));//文件生成地址 Path = Path.Replace("{page}", i.ToString()); ShowPath = Model.ShowRules; string tempath = Path.Substring(0, Path.LastIndexOf("/") + 1); if (!Directory.Exists(HttpContext.Current.Server.MapPath(tempath))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(tempath)); } int start = i * onepage; int nextpage = i + 1; int perpage = i - 1; if (nextpage == allpages) { nextpage = i; } if (perpage == -1) { perpage = 0; } string SQL = "SELECT TOP " + onepage + " * FROM [ROYcms_news] WHERE (bh NOT IN (SELECT TOP " + start + " bh FROM [ROYcms_news] " + (this.classname == null ? "" : " where classname=" + this.classname) + " ORDER BY bh desc ))" + (this.classname == null ? "" : " and classname=" + this.classname) + " ORDER BY bh desc "; DataSet ds = BLL.GetDataSet(SQL); DataTable dt = ds.Tables[0]; using (StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath(Path), false, Encoding.GetEncoding(ROYcms.Config.ROYcmsConfig.GetCmsConfigValue("templet_language")))) { #region 对循环的内容进行替换 foreach (Match m in r.Matches(HTML)) { Text = m.Groups["text"].ToString(); //循环的内容不包含SG:Loop AllText = m.Groups[0].Value.ToString(); //整个匹配的内容包含SG:Loop foreach (DataRow dr in dt.Rows) { string str = Text; string T_ShowPath = ShowPath; str = Template.Replace(str, @"\[SG:Id\]", dr["bh"].ToString()); str = Template.Replace(str, @"\[SG:Title\]", dr["title"].ToString()); str = Template.Replace(str, @"\[SG:zhaiyao\]", dr["zhaiyao"].ToString()); str = Template.Replace(str, @"\[SG:pic\]", dr["pic"].ToString()); str = Template.Replace(str, @"\[SG:classname\]", dr["classname"].ToString()); str = Template.Replace(str, @"\[SG:author\]", dr["author"].ToString()); str = Template.Replace(str, @"\[SG:time\]", dr["time"].ToString()); str = Template.Replace(str, @"\[SG:Year\]", Convert.ToDateTime(dr["time"]).ToString("yyyy")); str = Template.Replace(str, @"\[SG:Month\]", Convert.ToDateTime(dr["time"]).ToString("MM")); str = Template.Replace(str, @"\[SG:Day\]", Convert.ToDateTime(dr["time"]).ToString("dd")); T_ShowPath = T_ShowPath.Replace("{yyyy}", Convert.ToDateTime(dr["time"]).ToString("yyyy")); T_ShowPath = T_ShowPath.Replace("{MM}", Convert.ToDateTime(dr["time"]).ToString("MM")); T_ShowPath = T_ShowPath.Replace("{dd}", Convert.ToDateTime(dr["time"]).ToString("dd")); T_ShowPath = T_ShowPath.Replace("{id}", dr["bh"].ToString()); str = Template.Replace(str, @"\[SG:Link\]", T_ShowPath); content += AllText.Replace(AllText, str); } HTML = HTML.Replace(AllText, content); HTML = HTML.Replace(@"[SG:PageUp]", Model.ListeRules.Replace("{page}", perpage.ToString())); HTML = HTML.Replace(@"[SG:PageDow]", Model.ListeRules.Replace("{page}", nextpage.ToString())); content = ""; } #endregion //创建文件 try { sw.WriteLine(HTML); sw.Flush(); err = true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); err = false; } finally { sw.Close(); } } } } //封面频道方式 if (Model.ColumnsType == 1) { templatepath = Model.TemplateIndex; //模版地址 HTML = PublicTag(SystemCms.Read_File(HttpContext.Current.Server.MapPath(templatepath), ROYcms.Config.ROYcmsConfig.GetCmsConfigValue("templet_language"))); Path = (Model.FilePath + Model.DefaultFile); //文件生成地址 NewMain(Path, HTML); err = true; } return(err); }