Example #1
0
        /// <summary>
        /// 生成新闻静态文件
        /// </summary>
        /// <param name="news">仅指定了NewsID的新闻实体</param>
        public void Generate(AccessWebNews news)
        {
            if (news.IsFilled == false)
            {
                news = (new AccessWebNews()).GetInstanceById(news.NewsID) as AccessWebNews;
            }

            #region 判断新闻有效性
            if (news != null && news.IsExist == true && news.IsFilled == true)
            {
                AccessWebChannel newsChannel = (new AccessWebChannel(news.Channel.ChannelID)).GetInstance() as AccessWebChannel;
                if (newsChannel == null || news.IsFilled == false)
                {
                    ShowMessage("获取改新闻所在的频道设置失败,生成终止!");
                    return;
                }
                else
                {
                    TempletSetting tptConfig = newsChannel.TempletConfig;
                    if (tptConfig == null)
                    {
                        ShowMessage("该频道没有配置模板,不能生成相关文件!");
                    }
                    else
                    {
                        if (tptConfig.GenerateDetailPage == false)
                        {
                            ShowMessage("该频道模板设置关闭了生成详细文件,请先打开再执行生成操作!");
                            return;
                        }

                        string strGenFilePath, strFileName, strFileExt, strGenResult = "";
                        if (newsChannel.StaticFileGenDir.IndexOf("{#$ChannelID$#}") != -1)
                        {
                            newsChannel.StaticFileGenDir = newsChannel.StaticFileGenDir.Replace("{#$ChannelID$#}", newsChannel.ChannelID.ToString());
                        }
                        strGenFilePath = Util.ParseAppPath(newsChannel.StaticFileGenDir);
                        if (!strGenFilePath.EndsWith("\\")) strGenFilePath += "\\";

                        #region 单篇新闻生成
                        using (TempletParse tpp = new TempletParse())
                        {
                            PageTemplet pageTpt = (new PageTemplet()).GetInstanceById(tptConfig.DetailPageTemplet.TempletID) as PageTemplet;
                            tptConfig.DetailPageTemplet.FileNameConfig.FileNameTag.SetResourceDependency(news);
                            strFileName = tptConfig.DetailPageTemplet.FileNameConfig.FileNameTag.GetTagValue().ToString();
                            strFileExt = tptConfig.DetailPageTemplet.FileNameConfig.FileExtentionName;

                            MultiResDependency newsRes = new MultiResDependency(newsChannel, news);
                            tpp.SetResourceDependency(newsRes);
                            tpp.TagTrack = new System.Collections.Generic.Dictionary<string, string>();
                            tpp.TagTrack.Add("T" + pageTpt.TempletID.ToString(), pageTpt.ModifiedTime.ToString("yyyyMMddHHmmss"));
                            tpp.SetTaggedObjectCollection(pageTpt.TempletRawContent);

                            string newsfileWithFullPath = strGenFilePath + strFileName + strFileExt;
                            if (news.VirtualPath.Length > 5)
                            {
                                newsfileWithFullPath = Util.ParseAppPath(news.VirtualPath);
                            }
                            else
                            {
                                news.VirtualPath = newsChannel.StaticFileGenDir + strFileName + strFileExt;
                                news.RefreshVirtualPath();
                            }

                            string strGenFileContent = HtmlTextGenerator.GetRightHtmlText(pageTpt.SiteBaseDir, tpp.ParsedResult);
                            //插入Track代码
                            string trackPath = GetSiteRelativeUrl(newsfileWithFullPath);
                            GetTrackHtmlInSertCode(tpp.TagTrack, trackPath, "</body>", ref strGenFileContent);
                            strGenResult = OleDbHelper.SetTextFileContent(newsfileWithFullPath, "utf-8", strGenFileContent);
                            TrackUnit.RemoveUpdateRecord(0, trackPath);

                            if (strGenResult == "0")
                            {
                                FireGeneratorFileFinished(newsfileWithFullPath);
                            }

                            ShowMessage((strGenResult == "0") ? string.Format("新闻内容页文件生成成功!<a href=\"{0}\" target=\"_blank\">{0}</a>", GetSiteRelativeUrl(newsfileWithFullPath)) : "生成失败:" + strGenResult);
                        }
                        #endregion
                    }
                }
            }
            #endregion

            ShowMessage("已完成处理,如没有任何消息显示,这可能是该栏目没有内容或配置错误!");
            ShowMessage("$end$");
        }