Exemplo n.º 1
0
        /// <summary>
        /// 转换标签
        /// </summary>
        /// <param name="nest">深度</param>
        /// <param name="channelName">频道名称</param>
        /// <param name="sitePath">站点目录</param>
        /// <param name="skinName">模板名称</param>
        /// <param name="inputStr">模板内容</param>
        /// <returns></returns>
        private static string ConvertTags(int nest, string channelName, string pageSize, string sitePath, string tempPath, string skinName, string inputStr)
        {
            string strReturn   = "";
            string strTemplate = string.Empty;

            strTemplate = inputStr.Replace("\\", "\\\\");
            strTemplate = strTemplate.Replace("\"", "\\\"");
            strTemplate = strTemplate.Replace("</script>", "</\");\r\n\ttemplateBuilder.Append(\"script>");
            bool IsCodeLine = false;

            #region 解析嵌套标签====================================================OK
            foreach (Match m in r[0].Matches(strTemplate))
            {
                IsCodeLine = true;
                if (m.Groups[3].ToString() != string.Empty)
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n" + GetTemplate(sitePath, "templates", m.Groups[3].ToString(), m.Groups[4].ToString(), string.Empty, string.Empty, string.Empty, channelName, pageSize, nest + 1) + "\r\n");
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n" + GetTemplate(sitePath, tempPath, skinName, m.Groups[4].ToString(), string.Empty, string.Empty, string.Empty, channelName, pageSize, nest + 1) + "\r\n");
                }
            }
            #endregion

            #region 解析模板路径标签================================================OK
            foreach (Match m in r[1].Matches(strTemplate))
            {
                //IsCodeLine = true;
                if (m.Groups[3].ToString() != string.Empty)
                {
                    //strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\ttemplateBuilder.Append(\"{0}{1}/{2}\");", sitePath, "templates", m.Groups[3].ToString()));
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("{0}{1}/{2}", sitePath, "templates", m.Groups[3].ToString()));
                }
                else
                {
                    //strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\ttemplateBuilder.Append(\"{0}{1}/{2}\");", sitePath, tempPath, skinName));
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("{0}{1}/{2}", sitePath, tempPath, skinName));
                }
            }
            #endregion

            #region 解析csharp标签==================================================OK
            foreach (Match m in r[3].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(), m.Groups[1].ToString().Replace("\r\t\r", "\r\n\t").Replace("\\\"", "\""));
            }
            #endregion

            #region 解析loop标签====================================================OK
            foreach (Match m in r[4].Matches(strTemplate))
            {
                IsCodeLine = true;
                if (m.Groups[3].ToString() == "")
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                      string.Format("\r\n\tint {0}__loop__id=0;\r\n\tforeach(DataRow {0} in {1}.Rows)\r\n\t{{\r\n\t\t{0}__loop__id++;\r\n", m.Groups[4], m.Groups[5]));
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                      string.Format("\r\n\tint {1}__loop__id=0;\r\n\tforeach({0} {1} in {2})\r\n\t{{\r\n\t\t{1}__loop__id++;\r\n", m.Groups[3], m.Groups[4], m.Groups[5]));
                }
            }
            #endregion

            #region 解析foreach标签=================================================OK
            foreach (Match m in r[5].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  string.Format("\r\n\tforeach({0} {1} in {2})\r\n\t{{", m.Groups[1], m.Groups[2], m.Groups[3]));
            }
            #endregion

            #region 解析for标签=====================================================OK
            foreach (Match m in r[6].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  string.Format("\r\n\tfor({0})\r\n\t{{", m.Groups[1]));
            }
            #endregion

            #region 解译判断语句if==================================================OK
            foreach (Match m in r[7].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  "\r\n\tif (" + m.Groups[1].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
            }
            foreach (Match m in r[8].Matches(strTemplate))
            {
                IsCodeLine = true;
                if (m.Groups[1].ToString() == string.Empty)
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                      "\r\n\t}\r\n\telse\r\n\t{");
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                      "\r\n\t}\r\n\telse if (" + m.Groups[3].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
                }
            }
            foreach (Match m in r[9].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  "\r\n\tif (" + m.Groups[1].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
            }
            foreach (Match m in r[10].Matches(strTemplate))
            {
                IsCodeLine = true;
                if (m.Groups[1].ToString() == string.Empty)
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                      "\r\n\t}\r\n\telse\r\n\t{");
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                      "\r\n\t}\r\n\telse if (" + m.Groups[3].ToString().Replace("\\\"", "\"") + ")\r\n\t{");
                }
            }
            #endregion

            #region 解析循环判断结束标签============================================OK
            foreach (Match m in r[11].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  "\r\n\t}\t//end for if");
            }
            #endregion

            #region 解析continue,break标签==========================================OK
            foreach (Match m in r[12].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\tcontinue;\r\n");
            }
            foreach (Match m in r[13].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\tbreak;\r\n");
            }
            #endregion

            #region 解析截取字符串标签==============================================OK
            foreach (Match m in r[15].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  string.Format("\r\n\ttemplateBuilder.Append(Utils.DropHTML({0},{1}));", m.Groups[2], m.Groups[3].ToString().Trim()));
            }
            #endregion

            #region 解析时间格式转换================================================OK
            foreach (Match m in r[20].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  string.Format("\ttemplateBuilder.Append(Utils.ObjectToDateTime({0}).ToString(\"{1}\"));", m.Groups[2], m.Groups[3].ToString().Replace("\\\"", string.Empty)));
            }
            #endregion

            #region 字符串转换整型==================================================OK
            foreach (Match m in r[21].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  "Utils.StrToInt(" + m.Groups[2] + ", 0)");
            }
            #endregion

            #region 解析url链接标签=================================================OK
            foreach (Match m in r[16].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  string.Format("\r\n\ttemplateBuilder.Append(linkurl({0}));", m.Groups[2]).Replace("\\\"", "\""));
            }
            #endregion

            #region 解析赋值标签====================================================OK
            foreach (Match m in r[17].Matches(strTemplate))
            {
                IsCodeLine = true;
                string type = "";
                if (m.Groups[3].ToString() != string.Empty)
                {
                    type = m.Groups[3].ToString();
                }
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  string.Format("\r\n\t{0} {1} = {2};", type, m.Groups[4], m.Groups[5]).Replace("\\\"", "\""));
            }
            #endregion

            #region 解析request标签=================================================OK
            foreach (Match m in r[14].Matches(strTemplate))
            {
                if (IsCodeLine)
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "DTRequest.GetString(\"" + m.Groups[2] + "\")");
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + DTRequest.GetString(\"{0}\") + \"", m.Groups[2]));
                }
            }
            #endregion

            #region 解析直接输出标签================================================OK
            foreach (Match m in r[22].Matches(strTemplate))
            {
                IsCodeLine  = true;
                strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                  string.Format("\r\n\ttemplateBuilder.Append({0}{1}.ToString());", m.Groups[1], m.Groups[2]).Replace("\\\"", "\""));
            }
            #endregion

            #region 解析数据变量标签================================================OK
            foreach (Match m in r[18].Matches(strTemplate))
            {
                if (IsCodeLine)
                {
                    if (Utils.IsNumeric(m.Groups[3].ToString()))
                    {
                        strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "Utils.ObjectToStr(" + m.Groups[2] + "[" + m.Groups[3] + "])");
                    }
                    else
                    {
                        if (m.Groups[3].ToString() == "_id")
                        {
                            strTemplate = strTemplate.Replace(m.Groups[0].ToString(), m.Groups[2] + "__loop__id");
                        }
                        else
                        {
                            strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "Utils.ObjectToStr(" + m.Groups[2] + "[\"" + m.Groups[3] + "\"])");
                        }
                    }
                }
                else
                {
                    if (Utils.IsNumeric(m.Groups[3].ToString()))
                    {
                        strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + Utils.ObjectToStr({0}[{1}]) + \"", m.Groups[2], m.Groups[3]));
                    }
                    else
                    {
                        if (m.Groups[3].ToString() == "_id")
                        {
                            strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + {0}__loop__id.ToString() + \"", m.Groups[2]));
                        }
                        else
                        {
                            strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + Utils.ObjectToStr({0}[\"{1}\"]) + \"", m.Groups[2], m.Groups[3]));
                        }
                    }
                }
            }
            #endregion

            #region 解析普通变量标签================================================OK
            foreach (Match m in r[19].Matches(strTemplate))
            {
                if (IsCodeLine)
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(), m.Groups[2].ToString());
                }
                else
                {
                    strTemplate = strTemplate.Replace(m.Groups[0].ToString(),
                                                      string.Format("\");\r\n\ttemplateBuilder.Append(Utils.ObjectToStr({0}));\r\n\ttemplateBuilder.Append(\"", m.Groups[2].ToString().Trim()));
                }
            }
            #endregion

            #region 最后处理========================================================OK
            if (IsCodeLine)
            {
                strReturn = strTemplate + "\r\n";
            }
            else
            {
                if (strTemplate.Trim() != "")
                {
                    strReturn = "\r\n\ttemplateBuilder.Append(\"" + strTemplate.Replace("\r\r\r", "\\r\\n") + "\");";
                    strReturn = strReturn.Replace("\\r\\n<?xml", "<?xml");
                    strReturn = strReturn.Replace("\\r\\n<!DOCTYPE", "<!DOCTYPE");
                }
            }
            #endregion

            return(strReturn);
        }