/**********************************************************************************
 * ClassName:       GlobalTags
 * Description:     公用标签处理类
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2009-11-5     New          Ayong     Create Class
 * 
 * ********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using Fm.Bussiness;
using Fm.DataAccess;
using System.Web;
using System.Collections;

namespace Fm.Tags
{
    public class GlobalTags
    {
        Tags_sql sql = new Tags_sql();
/****************************************自由SQL标签解析**************************************************/
        /// <summary>
        /// 自由SQL标签解析
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public string Analysis_Freedom(string content)
        {
              //标签解析开始---
            string Attributes = "";//存储属性
            string Text = "";//存储标签Text
            string AllText="";
            //实例化正则对象
            Regex r = new Regex(@"(\{fm:freedom\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/fm:freedom\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match m in r.Matches(content))
            {

                //存储新的内容
                StringBuilder str = new StringBuilder();
                Attributes = m.Groups["attributes"].ToString();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();
                //获取相关属性
                string sqlstr = null;
                sqlstr = TagVal(Attributes,"sql");
                if (sqlstr == null)
                {
                    sqlstr = "";
                }
                string col = null;
                col = TagVal(Attributes,"col");
                if (col == null)
                {
                    col = "0";
                }
                if (sqlstr!= "")
                {
                    public_select ps = new public_select();
                    DataView dv = ps.Selectps(sqlstr);

                    
                    if (dv.Count > 0)
                    {
                        
                        //判断是否显示多列(如果不显示则不添加表格布局)
                        if (int.Parse(col) > 0)
                        {
                            str.Append("<table width=100%>");
                            //获取显示行数
                            int rows = 1;
                            if (dv.Table.Rows.Count % int.Parse(col) == 0)
                            {
                                rows = dv.Table.Rows.Count / int.Parse(col);

                            }
                            else
                            {
                                rows = dv.Table.Rows.Count / int.Parse(col) + 1;
                            }
                            //循环行数
                            int num = 0;
                            for (int row = 1; row <= rows; row++)
                            {
                                StringBuilder txt = new StringBuilder();
                                 txt.Append("<tr>");
                                //循环列
                                for (int c = 1; c <= int.Parse(col); c++)
                                {
                                    if (num < dv.Table.Rows.Count)
                                    {
                                        txt.Append("<td>");
                                        txt.Append(Text);
                                        foreach (DataColumn dc in dv.Table.Columns)
                                        {
                                            txt = txt.Replace("{field." + dc.ColumnName + "}", dv[num][dc.ColumnName].ToString());
                                        }
                                        txt.Append("</td>");

                                    }
                                    num++;
                                }
                                txt.Append("</tr>");
                                str.Append(txt);

                            }
                            str.Append("</table>");
                            content = content.Replace(AllText,str.ToString());
                        }
                        else
                        {
                            //循环行
                            foreach (DataRow row in dv.Table.Rows)
                            {
                                StringBuilder txt = new StringBuilder();
                                 txt.Append(Text);
                                //循环列
                                foreach (DataColumn dc in dv.Table.Columns)
                                {
                                    txt = txt.Replace("{field." + dc.ColumnName + "}", row[dc.ColumnName].ToString());
                                }
                                str.Append(txt);
                            }
                            content = content.Replace(AllText, str.ToString());
                        }
                    }
                    else
                    {
                        content = content.Replace(AllText, "");
                    }
                }
                else
                {
                    content = content.Replace(AllText,"");
                }
            }
            return content;
        }
/***************************************友情连接标签******************************************************/
        public string Analysis_Link(string content)
        {
             //标签解析开始---
            string Attributes = "";//存储属性
            string Text = "";//存储标签Text
            string AllText="";
            //实例化正则对象
            Regex r = new Regex(@"(\{fm:link\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/fm:link\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match m in r.Matches(content))
            {

                //存储新的内容
                StringBuilder str = new StringBuilder();
                Attributes = m.Groups["attributes"].ToString();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();
                //获取相关属性
                string type = null;
                string count = null;

                type = TagVal(Attributes, "type");
                if (type == null)
                {
                    type = "letter";
                }
                count = TagVal(Attributes, "count");
                if (count == null)
                {
                    count = "8";
                }
                string col = null;
                col=TagVal(Attributes,"col");
                if (col == null)
                {
                    col = "0";
                }
                DataView dv = sql.GetLink(type,count) as DataView;
                //判断是否有数据
                if (dv.Count > 0)
                {
                    StringBuilder txt = new StringBuilder();
                    //判断是否显示多列(如果不显示则不添加表格布局)
                    if (int.Parse(col) > 0)
                    {
                        str.Append("<table width=100%>");
                         //获取显示行数
                        int rows = 1;
                        if (dv.Table.Rows.Count % int.Parse(col) == 0)
                        {
                            rows = dv.Table.Rows.Count / int.Parse(col);

                        }
                        else
                        {
                            rows = dv.Table.Rows.Count / int.Parse(col) + 1;
                        }
                        //循环行数
                        int num=0;
                        for (int row = 1; row <= rows; row++)
                        {
                            txt.Append("<tr>");
                            //循环列
                            for (int c = 1; c <= int.Parse(col); c++)
                            {
                                if (num < dv.Table.Rows.Count)
                                {
                                    txt.Append("<td>");
                                    txt.Append(Text);
                                    txt = txt.Replace("{field.webname}",dv[num]["link_name"].ToString());
                                    txt = txt.Replace("{field.weburl}",dv[num]["link_url"].ToString());
                                    txt = txt.Replace("{field.weblogo}",dv[num]["link_logo"].ToString());
                                    txt = txt.Replace("{field.webintro}",dv[num]["link_intro"].ToString());
                                    txt.Append("</td>");

                                }
                                num++;
                            }
                            txt.Append("</tr>");
                            str.Append(txt);

                        }
                        str.Append("</table>");
                        content = content.Replace(AllText,str.ToString());
                    }
                    else
                    {
                        
                        foreach (DataRow row in dv.Table.Rows)
                        {
                            txt.Append(Text);
                            txt = txt.Replace("{field.webname}", row["link_name"].ToString());
                            txt = txt.Replace("{field.weburl}", row["link_url"].ToString());
                            txt = txt.Replace("{field.weblogo}", row["link_logo"].ToString());
                            txt = txt.Replace("{field.webintro}", row["link_intro"].ToString());

                            str.Append(txt);
                        }
                        content = content.Replace(AllText,str.ToString());
                    }
                }
                else
                {
                    content = content.Replace(AllText,"");
                }
            }
            return content;

        }
        /// <summary>
        /// 网站导航栏目部分获取方法
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public string Analysis_Sitemap(int cid)
        {
            StringBuilder str = new StringBuilder();
            if (cid != 0)
            {
                //获取当前栏目的PARENT 值
                //DataView dv = 
                public_select ps = new public_select();
                string parent = ps.Getps("Fm_column", "cl_parent", "cl_id=" + cid + "").Table.Rows[0]["cl_parent"].ToString();

                //获取列表页导航
                DataView dw = sql.GetSitemap(cid) as DataView;
                int i = 0;
                while (i <= parent.Length)
                {
                    i = i + 2;
                    foreach (DataRow row in dw.Table.Rows)
                    {
                        if (i <= parent.Length)
                        {
                            if (row["cl_parent"].ToString() == parent.Substring(0, i))
                            {
                                //判断URL地址
                                StringBuilder url = new StringBuilder();
                                //判断栏目属性
                                if (int.Parse(row["cl_attribute"].ToString()) == 0)
                                {
                                    if (int.Parse(row["cl_options"].ToString()) == 0)
                                    {
                                        url.Append("/");
                                        url.Append(row["cl_path"].ToString());
                                        url.Append(row["cl_default"].ToString());
                                    }
                                    else if (int.Parse(row["cl_options"].ToString()) == 1)
                                    {
                                        url.Append("/");
                                        url.Append(row["cl_path"].ToString());
                                        url.Append(row["cl_list"].ToString().Replace("{cid}", row["cl_id"].ToString()).Replace("{page}", "1"));
                                    }
                                    else
                                    {
                                        url.Append("/list.aspx?cid=");
                                        url.Append(row["cl_id"].ToString());
                                        url.Append("&page=1");
                                    }
                                }
                                else if (int.Parse(row["cl_attribute"].ToString()) == 1)
                                {
                                    url.Append("/");
                                    url.Append(row["cl_path"].ToString());
                                }
                                else
                                {
                                    url.Append(row["cl_url"].ToString());
                                }
                                str.Append("<a href='");
                                str.Append(url);
                                str.Append("' title='");
                                str.Append(row["cl_name"].ToString());
                                str.Append("'>");
                                str.Append(row["cl_name"].ToString());
                                str.Append("</a>");
                                str.Append(">");

                            }
                        }
                    }
                }
            }
            return str.ToString();
        }
/*****************************************公共标签({fm:global.field})*****************************************************/
        /// <summary>
        /// 公共标签解析
        /// </summary>
        /// <param name="title">网站标题</param>
        /// <param name="key">关键字</param>
        /// <param name="intro">描述</param>
        /// <param name="content">解析内容</param>
        /// <returns></returns>
        public string Analysis_Global(string title,string key,string intro,string content,string sitemap)
        {
            //变动标签
            content = content.Replace("{fm:global.title}",title);
            content = content.Replace("{fm:global.keywords}",key);
            content = content.Replace("{fm:global.description}",intro);
            //固定标签
            Fm.Bussiness.public_select ps = new public_select();
            DataView dv = ps.Getps("Fm_webset","*","");

            content = content.Replace("{fm:global.webname}",dv[0]["webname"].ToString());
            content = content.Replace("{fm:global.weburl}",dv[0]["weburl"].ToString());
            content = content.Replace("{fm:global.icp}",dv[0]["beian"].ToString());
            content = content.Replace("{fm:global.copyright}", dv[0]["banquan"].ToString() + "<br/>Powered by<a href='http://www.37151.cn' target='_blank'> Fmnews V1.5 </a>");
            content = content.Replace("{fm:global.charset}","gb2312");
            //导航加上首页链接
            sitemap = "<a href="+dv[0]["weburl"].ToString()+" title=首页>首页</a>>"+sitemap;
            content = content.Replace("{fm:global.navigation}",sitemap);
            return content;
        }
/******************************************{fm:include file=""}标签解析****************************************************/
        /// <summary>
        /// 页面导入标签解析
        /// </summary>
        /// <param name="content"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public string Analysis_Include(string content,string path)
        {
            Regex r = new Regex(@"(\{fm:include\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            //标签解析开始---
            //获取属性
            string Attributes = "";
            string AllText = "";
            //存储新的内容
            //string str = "";
            foreach (Match m in r.Matches(content))
            {
                //获取属性
                Attributes = m.Groups["attributes"].ToString();
                AllText = m.Groups[0].Value.ToString();
                string file = null;
                file=TagVal(Attributes, "file");
                if (file == null)
                {
                    //获取属性参数
                    content = content.Replace(AllText, "");
                }
                else
                {
                    //读取导入标签内容
                    ATL.IO.IOStream stream = new ATL.IO.IOStream();
                    string str = stream.ReadFile(path+file);
                    content = content.Replace(AllText,str);
                }
            }
            return content;
        }
/*********************************Article标签**************************************************/
        /// <summary>
        /// 解析文章调用标签
        /// </summary>
        /// <param name="content">包含标签的页面内容</param>
        /// <param name="cid">指定栏目ID</param>
        /// <returns></returns>
        public string Analysis_Article(string content, int colid)
        {
            //标签解析开始---
            string Attributes = "";//存储属性
            string Text = "";//存储标签Text
            string AllText = "";
            //实例化正则对象
            Regex r = new Regex(@"(\{fm:article\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/fm:article\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match m in r.Matches(content))
            {
                //栏目URL
                string columnurl="";

                Attributes = m.Groups["attributes"].ToString();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();
                //获取相关属性
                string type = null;//栏目类型(top,self,son)

                type = TagVal(Attributes, "type");
                if (type == null)
                {
                    type = "new";
                }
                string cid = null;//栏目ID
                cid = TagVal(Attributes, "cid");
                if (cid == null)
                {
                    cid = colid.ToString();
                }
                string count = null;//显示行数
                count = TagVal(Attributes, "count");
                if (count == null)
                {
                    count = "8";
                }
                string col = null;//显示的列数
                col = TagVal(Attributes,"col");
                if (col == null)
                {
                    col = "0";
                }
                //获取数据
                DataView dv = sql.GetArticle(type, cid, count) as DataView;
                //循环替换数据
                if (dv.Count > 0)
                {
                    int index = 1;
                    //存储新的内容
                    StringBuilder str = new StringBuilder();
                    if (int.Parse(col) > 0)
                    {
                        //获取显示行数
                        int rows = 1;
                        if (dv.Table.Rows.Count % int.Parse(col) == 0)
                        {
                            rows = dv.Table.Rows.Count / int.Parse(col);

                        }
                        else
                        {
                            rows = dv.Table.Rows.Count / int.Parse(col) + 1;
                        }
                        //循环行数
                        int num = 0;
                        for (int row = 1; row <= rows; row++)
                        {
                            StringBuilder txt = new StringBuilder();
                            txt.Append("<table width=100%>");
                            txt.Append("<tr>");
                            //循环列
                            for (int c = 1; c <= int.Parse(col); c++)
                            {
                                if (num < dv.Table.Rows.Count)
                                {
                                    //txt = txt + "<td>" + Text;
                                    txt.Append("<td>");
                                    txt.Append(Text);
                                    //标签解析
                                    txt = txt.Replace("{field.index}", index.ToString());
                                    index++;
                                    txt = txt.Replace("{field.docid}", dv[num]["docid"].ToString());
                                    txt = txt.Replace("{field.cid}", dv[num]["cid"].ToString());
                                    txt = txt.Replace("{field.cname}", dv[num]["cname"].ToString());
                                    //栏目URL
                                    columnurl = GetColumnUrl(int.Parse(dv[num]["cid"].ToString()));
                                    txt = txt.Replace("{field.curl}", columnurl);
                                    //txt = txt.Replace("{$title}",dv[num]["title"].ToString());
                                    txt = txt.Replace("{field.click}", dv[num]["click"].ToString());
                                    if (dv[0]["thumbnail"].ToString() == "")
                                    {
                                        txt = txt.Replace("{field.thumbnail}", "/images/thumbnail.jpg");
                                    }
                                    else
                                    {
                                        txt = txt.Replace("{field.thumbnail}", dv[num]["thumbnail"].ToString());
                                    }
                                    txt = txt.Replace("{field.top}", dv[num]["topclick"].ToString());
                                    txt = txt.Replace("{field.tread}", dv[num]["tread"].ToString());

                                    
                                    txt = txt.Replace("field.color", dv[num]["color"].ToString());

                                    txt = txt.Replace("{field.keywords}", dv[num]["keyword"].ToString());
                                    /*****************Start***************/
                                    //描述字符控制处理
                                    Regex Rintro = new Regex(@"(\{field.intro\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                    string IntroAttribute = "";//存储属性
                                    string IntroAll = "";
                                    string IntroCount = null;
                                    string intro =dv[num]["intro"].ToString();
                                    //txt = txt.Replace("{field.intro}", );
                                    //if (Rintro.Matches(txt.ToString()).Count <= 0)
                                    //{
                                        txt = txt.Replace("{field.intro}",intro);
                                    //}
                                    foreach (Match mat in Rintro.Matches(txt.ToString()))
                                    {
                                        IntroAttribute = mat.Groups["attributes"].ToString();
                                        IntroAll = mat.Groups[0].Value.ToString();

                                        IntroCount = TagVal(IntroAttribute, "count");

                                        if (IntroCount != null)
                                        {
                                            int ICount = int.Parse(IntroCount);
                                            intro = intro.Length > ICount ? intro.Substring(0, ICount) : intro;
                                        }
                                        txt.Replace(IntroAll, intro);

                                    }
                                    /*************End*************/
                                    txt = txt.Replace("{field.username}", dv[num]["username"].ToString());

                                    //标题处理
                                    /*****************Start***************/
                                    //txt = txt.Replace("{field.title}", );
                                    Regex Rtitle = new Regex(@"(\{field.title\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                    string TitleAttribute = "";//存储属性
                                    string TitleAll = "";
                                    string TitleCount = null;
                                    string title = dv[num]["title"].ToString();
                                    //如果没有设置该参数则显示全部标题
                                    //if (Rtitle.Matches(txt.ToString()).Count <= 0)
                                    //{
                                        txt = txt.Replace("{field.title}", title);
                                    //}
                                    foreach (Match mat in Rtitle.Matches(txt.ToString()))
                                    {
                                        TitleAttribute = mat.Groups["attributes"].ToString();
                                        TitleAll = mat.Groups[0].Value.ToString();

                                        TitleCount = TagVal(TitleAttribute,"count");

                                        if (TitleCount != null)
                                        {
                                            int TCount=int.Parse(TitleCount);
                                            title = title.Length > TCount?title.Substring(0,TCount):title;
                                        }
                                        txt.Replace(TitleAll, title);

                                    }
                                    /*************End*************/
                                    //处理连接地址
                                    if (dv[num]["html"].ToString().ToLower() == "true")
                                    {
                                        txt = txt.Replace("{field.url}", dv[num]["path"].ToString());
                                    }
                                    else
                                    {
                                        txt = txt.Replace("{field.url}", "/article.aspx?docid=" + dv[num]["docid"].ToString());
                                    }
                                    //处理时间
                                    /*************Start*************/
                                    Regex r1 = new Regex(@"(\{field.writertime\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                                    string att = "";//存储属性
                                    string all = "";
                                    string format = null;
                                    foreach (Match m1 in r1.Matches(txt.ToString()))
                                    {
                                        //获取属性
                                        att = m1.Groups["attributes"].ToString();
                                        all = m1.Groups[0].Value.ToString();

                                        format = TagVal(att, "format");

                                        DateTime time = DateTime.Parse(dv[num]["writertime"].ToString());

                                        if (format != null)
                                        {
                                            format = format.Replace("yyyy", time.Year.ToString());

                                            format = format.Replace("mm", time.Month.ToString());

                                            format = format.Replace("dd", time.Day.ToString());
                                        }
                                        else
                                        {
                                            format = time.Date.ToString();
                                        }
                                        //替换时间标签
                                        txt = txt.Replace(all, format);

                                    }
                                    /*************End*************/
                                    txt.Append("</td>");

                                }
                                num++;

                            }
                            txt.Append("</tr>");
                            str.Append(txt);

                        }
                        str.Append("</table>");
                        content = content.Replace(AllText, str.ToString());
                    }
                    else
                    {

                        foreach (DataRow row in dv.Table.Rows)
                        {
                            StringBuilder txt = new StringBuilder();
                            txt.Append(Text);
                            //标签解析
                            //索引标签
                            txt = txt.Replace("{field.index}", index.ToString());
                            index++;

                            txt = txt.Replace("{field.docid}", row["docid"].ToString());
                            txt = txt.Replace("{field.cid}", row["cid"].ToString());
                            txt = txt.Replace("{field.cname}", row["cname"].ToString());
                            //栏目URL
                             columnurl= GetColumnUrl(int.Parse(row["cid"].ToString()));
                             txt = txt.Replace("{field.curl}",columnurl);
                            //txt = txt.Replace("{$title}",dv[num]["title"].ToString());
                            txt = txt.Replace("{field.click}", row["click"].ToString());
                            if (row["thumbnail"].ToString().Equals(""))
                            {
                                txt = txt.Replace("{field.thumbnail}", "/images/thumbnail.jpg");
                            }
                            else
                            {
                                txt = txt.Replace("{field.thumbnail}", row["thumbnail"].ToString());
                            }
                            txt = txt.Replace("{field.top}", row["topclick"].ToString());
                            txt = txt.Replace("{field.tread}", row["tread"].ToString());

                            //标题处理
                            
                            Regex Rtitle = new Regex(@"(\{field.title\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            string TitleAttribute = "";//存储属性
                            string TitleAll = "";
                            string TitleCount = null;
                            string title = row["title"].ToString();
                            //如果没有设置该参数则显示全部标题
                            //if (Rtitle.Matches(txt.ToString()).Count <= 0)
                            //{
                                txt = txt.Replace("{field.title}", title);
                            //}
                            foreach (Match mat in Rtitle.Matches(txt.ToString()))
                            {
                                TitleAttribute = mat.Groups["attributes"].ToString();
                                TitleAll = mat.Groups[0].Value.ToString();

                                TitleCount = TagVal(TitleAttribute, "count");

                                if (TitleCount != null)
                                {
                                    int TCount = int.Parse(TitleCount);
                                    title = title.Length > TCount ? title.Substring(0, TCount) : title;
                                }
                                txt.Replace(TitleAll, title);

                            }

                            txt = txt.Replace("field.color", row["color"].ToString());


                            txt = txt.Replace("{field.keywords}", row["keyword"].ToString());
                            /*****************Start***************/
                            //描述字符控制处理
                            Regex Rintro = new Regex(@"(\{field.intro\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            string IntroAttribute = "";//存储属性
                            string IntroAll = "";
                            string IntroCount = null;
                            string intro = row["intro"].ToString();
                            //txt = txt.Replace("{field.intro}", );
                            //if (Rintro.Matches(txt.ToString()).Count <= 0)
                            //{
                                txt = txt.Replace("{field.intro}", intro);
                            //}
                            foreach (Match mat in Rintro.Matches(txt.ToString()))
                            {
                                IntroAttribute = mat.Groups["attributes"].ToString();
                                IntroAll = mat.Groups[0].Value.ToString();

                                IntroCount = TagVal(IntroAttribute, "count");

                                if (IntroCount != null)
                                {
                                    int ICount = int.Parse(IntroCount);
                                    intro = intro.Length > ICount ? intro.Substring(0, ICount) : intro;
                                }
                                txt.Replace(IntroAll, intro);

                            }
                            /*************************End*****************/
                            //txt = txt.Replace("{field.intro}", row["intro"].ToString());
                            txt = txt.Replace("{field.username}", row["username"].ToString());


                            //处理连接地址
                            if (row["html"].ToString().ToLower() == "true")
                            {
                                txt = txt.Replace("{field.url}", row["path"].ToString());
                            }
                            else
                            {
                                txt = txt.Replace("{field.url}", "/article.aspx?docid=" + row["docid"].ToString());
                            }
                            //处理时间
                            Regex r1 = new Regex(@"(\{field.writertime\s+(?<attributes>[^\]]*?)\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            string att = "";//存储属性
                            string all = "";
                            string format = null;
                            foreach (Match m1 in r1.Matches(txt.ToString()))
                            {
                                //获取属性
                                att = m1.Groups["attributes"].ToString();
                                all = m1.Groups[0].Value.ToString();

                                format = TagVal(att, "format");

                                DateTime time = DateTime.Parse(row["writertime"].ToString());

                                if (format != null)
                                {

                                    format = format.Replace("yyyy", time.Year.ToString());

                                    format = format.Replace("mm", time.Month.ToString());

                                    format = format.Replace("dd", time.Day.ToString());
                                }
                                else
                                {
                                    format = time.Date.ToString();
                                }
                                //替换时间标签
                                txt = txt.Replace(all, format);
                            }


                            str.Append(txt);
                        }
                        //替换整个标签的数据
                        content = content.Replace(AllText, str.ToString());

                    }
                }
                else
                {
                    content = content.Replace(AllText, "");
                }
               // content += "type=" + type + "|cid=" + cid + "|count=" + count + "|col=" + col+"</br>";
            }
            //返回数据
            return content;
        }
/***********************************栏目标签解析***********************************/
        public string Analysis_Channels(string content,int cid)
        {
            //标签解析开始---
            string Text = "";//存储标签Text
            string AllText = "";
            //实例化正则对象
            Regex r = new Regex(@"(\{fm:channels\}(?<text>[\s\S]*?)\{/fm:channels\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match m in r.Matches(content))
            {
                //存储新的内容
                StringBuilder str = new StringBuilder();
                Text = m.Groups["text"].ToString();
                AllText = m.Groups[0].Value.ToString();

                DataView dv = sql.GetChannels(cid) as DataView;
                if (dv.Count > 0)
                {
                    foreach (DataRow row in dv.Table.Rows)
                    {
                        StringBuilder txt =new StringBuilder();
                            txt.Append(Text);
                            //栏目连接地址
                            StringBuilder url = new StringBuilder();
                            //判断栏目属性
                            if (int.Parse(row["cl_attribute"].ToString()) == 0)
                            {
                                if (int.Parse(row["cl_options"].ToString()) == 0)
                                {
                                    //url.Append("/"+row["cl_path"].ToString()+row["cl_default"].ToString());
                                    url.Append("/");
                                    url.Append(row["cl_path"].ToString());
                                    url.Append(row["cl_default"].ToString());
                                    //去掉index.html 这些
                                    //url = url.Replace("index.html","");
                                    //url = url.Replace("index.htm","");
                                    //url = url.Replace("default.html","");
                                    //url = url.Replace("default.htm","");
                                }
                                else if (int.Parse(row["cl_options"].ToString()) == 1)
                                {
                                    url.Append("/");
                                    url.Append(row["cl_path"].ToString());
                                    url.Append(row["cl_list"].ToString().Replace("{cid}", row["cl_id"].ToString()).Replace("{page}", "1"));
                                }
                                else
                                {
                                    url.Append("/list.aspx?cid=");
                                    url.Append(row["cl_id"].ToString());
                                    url.Append("&page=1");
                                }
                            }
                            else if (int.Parse(row["cl_attribute"].ToString()) == 1)
                            {
                                url.Append("/");
                                url.Append(row["cl_path"].ToString());
                            }
                            else
                            {
                                url.Append(row["cl_url"].ToString());
                            }
                            txt = txt.Replace("{channels.cid}",row["cl_id"].ToString());
                            txt = txt.Replace("{channels.name}", row["cl_name"].ToString());
                            txt = txt.Replace("{channels.url}", url.ToString());
                            str.Append(txt);
                        }
                        //替换当前标签调用的数据
                        content = content.Replace(AllText, str.ToString());
                    } 
                    else
                    {
                        content = content.Replace(AllText,"");
                    }
               
            }
            return content;

        }
        /// <summary>
        /// 菜单栏循环标签显示
        /// </summary>
        /// <param name="content"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string Analysis_Column(string content, int colid)
        {

            //标签解析开始---

            string Attributes ="";//存储属性
            string Text = "";//存储标签Text
            string AllText = "";
            //实例化正则对象
            Regex r = new Regex(@"(\{fm:column\s+(?<attributes>[^\]]*?)\}(?<text>[\s\S]*?)\{/fm:column\})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match m in r.Matches(content))
            {

                //存储新的内容
                StringBuilder str = new StringBuilder();

                Attributes=m.Groups["attributes"].ToString();
                Text=m.Groups["text"].ToString();
                AllText=m.Groups[0].Value.ToString();
                //获取相关属性
                string count = null;//显示数量
                string type = null;//栏目类型(top,self,son)
                string cid = null;//栏目ID

                type = TagVal(Attributes, "type");
                if (type == null)
                {
                    type = "top";
                }
                cid = TagVal(Attributes, "cid");
                if (cid == null)
                {
                    cid = colid.ToString();
                }
                count = TagVal(Attributes, "count");
                if (count == null)
                {
                    count = "20";
                }

                if (int.Parse(cid) > 0)
                {
                    //Tags_sql sql = new Tags_sql();
                    //获取数据

                    DataView dv = sql.GetMenu(type, cid, count) as DataView;

                    if (dv.Count > 0)
                    {
                        foreach (DataRow row in dv.Table.Rows)
                        {
                            StringBuilder txt =new StringBuilder();
                            txt.Append(Text);
                            string url = GetColumnUrl(int.Parse(row["cl_id"].ToString()));
                            txt = txt.Replace("{field.cid}",row["cl_id"].ToString());
                            txt = txt.Replace("{field.name}", row["cl_name"].ToString());
                            txt = txt.Replace("{field.url}", url);
                            str.Append(txt);
                        }

                    }
                    //替换当前标签调用的数据
                    content = content.Replace(AllText, str.ToString());
                }
                else
                {
                    content = content.Replace(AllText,"");
                }
               

            }

            return content;
        }
        /// <summary>
        /// 获取栏目URL 地址
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        string GetColumnUrl(int cid)
        {
            public_select ps = new public_select();
            DataView dv= ps.Getps("Fm_column","*","cl_id="+cid);
            //栏目连接地址
            StringBuilder url = new StringBuilder();
            foreach (DataRow row in dv.Table.Rows)
            {
                //判断栏目属性
                if (int.Parse(row["cl_attribute"].ToString()) == 0)
                {
                    if (int.Parse(row["cl_options"].ToString()) == 0)
                    {
                        //url.Append("/"+row["cl_path"].ToString()+row["cl_default"].ToString());
                        url.Append("/");
                        url.Append(row["cl_path"].ToString());
                        url.Append(row["cl_default"].ToString());
                    }
                    else if (int.Parse(row["cl_options"].ToString()) == 1)
                    {
                        url.Append("/");
                        url.Append(row["cl_path"].ToString());
                        url.Append(row["cl_list"].ToString().Replace("{cid}", row["cl_id"].ToString()).Replace("{page}", "1"));
                    }
                    else
                    {
                        url.Append("/list.aspx?cid=");
                        url.Append(row["cl_id"].ToString());
                        url.Append("&page=1");
                    }
                }
                else if (int.Parse(row["cl_attribute"].ToString()) == 1)
                {
                    url.Append("/");
                    url.Append(row["cl_path"].ToString());
                }
                else
                {
                    url.Append(row["cl_url"].ToString());
                }
            }
            return url.ToString();
        }
/*********************获取属性值的方法******************************************/
        /// <summary>
        /// 获取标签属性值
        /// </summary>
        /// <param name="tag">属性集合</param>
        /// <param name="tagName">标签名</param>
        /// <returns>返回属性值</returns>
        public string TagVal(string tag, string tagName)
        {
            Regex r = new Regex(@"(?<key>\w+)\s*=\s*(?<value>" + "\"" + "[^\"]*\")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            foreach (Match m in r.Matches(tag))
            {
                if (m.Groups["key"].ToString().Trim().ToLower() == tagName.ToLower())
                {
                    return m.Groups["value"].ToString().ToLower().Replace("\"", "");
                }
            }
            return null;
        }
    }
}
