Пример #1
0
        private TemplateDocument LoadDocument(int documentId, string filePath)
        {
            TemplateDocument Document = RenderDocumentCache.GetRenderDocument(documentId);

            if (Document == null)
            {
                string newContent = FileUtils.ReadText(filePath);
                Document = new TemplateDocument(newContent, GlobalContext.WebRootPath, filePath);
                // RenderDocumentCache.AddRenderDocument(10005, Document);
            }
            string upperTableName = GetUpperCanelName(table.entity_name);
            string lowTableName   = upperTableName.Substring(0, 1).ToLower() + upperTableName.Substring(1);
            string roletag        = table.entity_name.Replace("-", "");

            //加入基本信息
            Document.Variables.SetValue("this", this);
            Document.Variables.SetValue("table", table);
            Document.Variables.SetValue("columns", columns);
            Document.Variables.SetValue("gentime", DateTime.Now);
            Document.Variables.SetValue("upperTableName", upperTableName);
            Document.Variables.SetValue("lowTableName", lowTableName);
            Document.Variables.SetValue("roletag", roletag);
            Document.RegisterGlobalFunction(this.GetColumnAttr);
            Document.RegisterGlobalFunction(this.GetCsType);

            return(Document);
        }
Пример #2
0
        /// <summary>
        /// 返回渲染后的页面文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public (bool genStatus, string contentHtml) GenerateContentHtml(int channelId, int id)
        {
            try
            {
                Stopwatch watcher = new Stopwatch();
                watcher.Start();
                var templateModel = ChannelManagerCache.GetContentTemplate(channelId);
                if (templateModel == null)
                {
                    return(false, "");
                }
                var content = _contentApp.GetContentInfo(id);
                if (content == null)
                {
                    return(false, "");
                }
                //加载模板 先取缓存,没有再初始化一个并且加入缓存
                this.Document = RenderDocumentCache.GetRenderDocument(templateModel.id);
                if (this.Document == null)
                {
                    string templateFile = Path.Combine(GlobalContext.WebRootPath, templateModel.template_file);
                    this.Document = new TemplateDocument(templateModel.template_content, GlobalContext.WebRootPath, templateFile);
                    RenderDocumentCache.AddRenderDocument(templateModel.id, this.Document);
                }

                this.Document.Variables.SetValue("this", this);
                //站点基本信息
                var site = SiteManagerCache.GetSiteInfo();
                this.Document.Variables.SetValue("site", site);
                //设置顶部导航条数据
                var navigations = _contentApp.GetChannelTree();
                this.Document.Variables.SetValue("navigations", navigations);

                //获取当前文章信息
                this.Document.Variables.SetValue("content", content);
                string renderHtml = this.Document.GetRenderText();
                renderHtml = HtmlPlayerHandler.CreateVideo(renderHtml);
                watcher.Stop();
                string msg = $"渲染内容页耗时:{watcher.ElapsedMilliseconds} ms";

                LoggerHelper.Info(msg);
                return(true, renderHtml);
            }
            catch (Exception ex)
            {
                LoggerHelper.Exception(ex);
            }
            return(false, "");
        }
Пример #3
0
        /// <summary>
        /// 返回渲染后的页面文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public (bool genStatus, string contentHtml) GenerateContentHtml(int channelId, int id)
        {
            try
            {
                Stopwatch watcher = new Stopwatch();
                watcher.Start();
                var templateModel = ChannelManagerCache.GetContentTemplate(channelId);
                if (templateModel == null)
                {
                    return(false, "");
                }
                var content = _contentApp.GetContentInfo(id);
                if (content == null)
                {
                    return(false, "");
                }
                //加载模板 先取缓存,没有再初始化一个并且加入缓存
                this.Document = RenderDocumentCache.GetRenderDocument(templateModel.id);
                if (this.Document == null)
                {
                    string templateFile = Path.Combine(GlobalParamsDto.WebRoot, templateModel.template_file);
                    this.Document = new TemplateDocument(templateModel.template_content, GlobalParamsDto.WebRoot, templateFile);
                    RenderDocumentCache.AddRenderDocument(templateModel.id, this.Document);
                }

                this.Document.Variables.SetValue("this", this);
                this.Document.Variables.SetValue("news", content);
                string renderHtml = this.Document.GetRenderText();
                renderHtml = HtmlElementHandler.CreateVideo(renderHtml);
                watcher.Stop();
                string msg = $"渲染内容页耗时:{watcher.ElapsedMilliseconds} ms";

                LogNHelper.Info(msg);
                return(true, renderHtml);
            }
            catch (Exception ex)
            {
                LogNHelper.Exception(ex);
            }
            return(false, "");
        }
Пример #4
0
        /// <summary>
        /// 返回渲染后的模板文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public (bool genStatus, string contentHtml) GenerateChannelHtml(int channelId, int page)
        {
            try
            {
                //Stopwatch watcher = new Stopwatch();
                //watcher.Start();
                var channel = _generateContentApp.GetChannel(channelId);
                if (channel == null)
                {
                    return(false, null);
                }
                var templateModel = TemplateManagerCache.GetChannelTemplate(channel.channel_template);
                if (templateModel == null)
                {
                    return(false, null);
                }

                //加载模板 先取缓存,没有再初始化一个并且加入缓存
                //this.LoadTemplate(templateModel.template_content);"Template/Channel"
                this.Document = RenderDocumentCache.GetRenderDocument(templateModel.id);
                if (this.Document == null)
                {
                    string templateFile = Path.Combine(GlobalParamsDto.WebRoot, templateModel.template_file);
                    this.Document = new TemplateDocument(templateModel.template_content, GlobalParamsDto.WebRoot, templateFile);
                    RenderDocumentCache.AddRenderDocument(templateModel.id, this.Document);
                }

                this.Document.Variables.SetValue("this", this);
                //站点数据
                var site = SiteManagerCache.GetSiteInfo();
                site.site_title = channel.channel_name;
                this.Document.Variables.SetValue("site", site);
                //设置顶部导航条数据
                var navigations = _generateContentApp.GetChannelTree(channelId);
                this.Document.Variables.SetValue("navigations", navigations);

                //解析文章列表模板设置数据源
                Tag    element = this.Document.GetChildTagById("contents");
                string total   = element.Attributes.GetValue("total", "8");

                var contents = _generateContentApp.GetContentSummaryPage(channelId, page, int.Parse(total));
                //设置变量newsdata的值
                this.Document.Variables.SetValue("channel", channel);
                this.Document.Variables.SetValue("contents", contents.Contents);
                this.Document.Variables.SetValue("pageHtml", contents.PageHtml);
                string renderHtml = this.Document.GetRenderText();


                //watcher.Stop();
                //string msg = $"渲染栏目耗时:{watcher.ElapsedMilliseconds} ms";

                //LogNHelper.Info(msg);

                return(true, renderHtml);
            }
            catch (Exception ex)
            {
                LogNHelper.Exception(ex);
            }
            return(false, null);
        }
Пример #5
0
        /// <summary>
        /// 返回渲染后的模板文件
        /// </summary>
        /// <param name="content"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public void GenerateHomeHtml()
        {
            try
            {
                Stopwatch watcher = new Stopwatch();
                watcher.Start();
                var templateModel = TemplateManagerCache.GetHomeTemplate();
                if (templateModel.id==0)
                {
                    throw new Exception("找不到模板");
                }
                //加载模板 先取缓存,没有再初始化一个并且加入缓存
                this.Document = RenderDocumentCache.GetRenderDocument(templateModel.id);
                if (this.Document == null)
                {
                    string templateFile = Path.Combine(GlobalParamsDto.WebRoot, templateModel.template_file);
                    this.Document = new TemplateDocument(templateModel.template_content, GlobalParamsDto.WebRoot, templateFile);
                    RenderDocumentCache.AddRenderDocument(templateModel.id, this.Document);
                }
                //this.LoadTemplate(templateModel.template_content);

                this.Document.Variables.SetValue("this", this);
                //站点基本信息
                var site = SiteManagerCache.GetSiteInfo();
                this.Document.Variables.SetValue("site", site);
                //设置顶部导航条数据
                var navigations = _generateContentApp.GetChannelTree();
                this.Document.Variables.SetValue("navigations", navigations);

                //获取栏目文章模板
                ElementCollection<Template> templates = this.Document.GetChildTemplatesByName("channels");
                foreach (Template template in templates)
                {
                    string total = template.Attributes.GetValue("total", "10");
                    //根据模板块里定义的type属性条件取得新闻数据
                    var data = _generateContentApp.GetContentSummary(template.Attributes.GetValue("type"),1,int.Parse(total));
                    //设置变量newsdata的值
                    template.Variables.SetValue("contents", data);

                    //取得模板块下Id为newslist的标签(也即是在cnblogs_newsdata.html文件中定义的foreach标签)
                    //Tag tag = template.GetChildTagById("newslist");
                    //if (tag is ForEachTag)
                    //{
                    //    //如果标签为foreach标签则设置其BeforeRender事件用于设置变量表达式{$:#.news.url}的值
                    //    tag.BeforeRender += new System.ComponentModel.CancelEventHandler(Tag_BeforeRender);
                    //}
                }

                string contentFilePath = Path.Combine(GlobalParamsDto.WebRoot, "index.html");
                using (var filestream = new FileStream(contentFilePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    string renderHtml = this.Document.GetRenderText();

                    using (StreamWriter writer = new StreamWriter(filestream, Encoding.UTF8))
                    {

                        writer.WriteLine(renderHtml);
                        writer.Flush();
                    }
                }
                watcher.Stop();
                string msg = $"渲染首页耗时:{watcher.ElapsedMilliseconds} ms";

                LogNHelper.Info(msg);
            }
            catch (Exception ex)
            {
                LogNHelper.Exception(ex);

            }
        }
Пример #6
0
        /// <summary>
        /// 生成静态文档,channelId和contentId都为0的时候生成首页
        /// </summary>
        /// <param name="contentId">文章id</param>
        /// <param name="channelId">栏目id</param>
        /// <param name="channelPage">栏目页的分页页面</param>
        /// <returns></returns>
        public GenerateResultDto GenerateHtml(int contentId, int channelId, int channelPage)
        {
            GenerateResultDto ret = new GenerateResultDto();

            try
            {
                //获取模板信息
                cms_template templateModel = null;
                ChannelModel channelModel  = null;
                ContentModel contentModel  = null;
                if (channelId == 0 && contentId == 0)
                {
                    templateModel = TemplateManagerCache.GetHomeTemplate();
                    RenderMode    = 1;
                }
                else if (channelId > 0)
                {
                    channelModel  = generateApp.GetChannel(channelId);
                    templateModel = ChannelManagerCache.GetChannelTemplate(channelId);
                    if (channelModel == null)
                    {
                        ret.Message = "栏目数据查找失败";
                        return(ret);
                    }
                    RenderMode = 2;
                }
                else if (contentId > 0)
                {
                    contentModel  = generateApp.GetContentInfo(contentId);
                    channelModel  = generateApp.GetChannel(contentModel.channel_id);
                    templateModel = ChannelManagerCache.GetContentTemplate(contentModel.channel_id);

                    if (contentModel == null)
                    {
                        ret.Message = "内容数据查找失败";
                        return(ret);
                    }

                    RenderMode = 3;
                }

                if (templateModel == null || templateModel.id == 0)
                {
                    throw new Exception("找不到模板");
                }

                //加载模板 先取缓存,没有再初始化一个并且加入缓存
                this.Document = RenderDocumentCache.GetRenderDocument(templateModel.id);
                if (this.Document == null)
                {
                    string templateFile = Path.Combine(GlobalContext.WebRootPath, templateModel.template_file);
                    this.Document = new TemplateDocument(templateModel.template_content, GlobalContext.WebRootPath, templateFile);
                    RenderDocumentCache.AddRenderDocument(templateModel.id, this.Document);
                }

                #region 公用数据
                //加入基本信息
                this.Document.Variables.SetValue("this", this);
                //站点基本信息
                var site = SiteManagerCache.GetSiteInfo();
                this.Document.Variables.SetValue("site", site);
                //添加所有导航条数据
                var channels = generateApp.GetChannelTree();
                this.Document.Variables.SetValue("channels", channels);

                //解析模板中的栏目文章模板
                ElementCollection <Template> templateContents = this.Document.GetChildTemplatesByName("contents");
                foreach (Template template in templateContents)
                {
                    //从第几条数据开始
                    string startNum = template.Attributes.GetValue("startNum", "1");
                    string total    = template.Attributes.GetValue("total", "10");
                    //根据模板块里定义的type属性条件取得新闻数据
                    var data = generateApp.GetContentSummary(template.Attributes.GetValue("channelIndex"), int.Parse(startNum), int.Parse(total));
                    //设置变量newsdata的值
                    template.Variables.SetValue("contents", data);
                }

                //解析模板中的channel标签
                ElementCollection <Template> templateChannel = this.Document.GetChildTemplatesByName("channel");
                foreach (Template template in templateChannel)
                {
                    string channelIndex = template.Attributes.GetValue("channelIndex", "#");
                    //根据模板块里定义的type属性条件取得新闻数据
                    var channel = ChannelManagerCache.GetChannelByIndex(channelIndex);
                    if (channel != null)
                    {
                        ChannelModel inChannelModel = generateApp.GetChannel(channel.id);
                        //设置变量newsdata的值
                        template.Variables.SetValue("channel", inChannelModel);
                    }
                }
                #endregion

                //渲染首页
                if (RenderMode == 1)
                {
                    string contentFilePath = Path.Combine(GlobalContext.WebRootPath, "index.html");
                    using (var filestream = new FileStream(contentFilePath, FileMode.Create, FileAccess.ReadWrite))
                    {
                        string renderHtml = this.Document.GetRenderText();

                        using (StreamWriter writer = new StreamWriter(filestream, Encoding.UTF8))
                        {
                            writer.WriteLine(renderHtml);
                            writer.Flush();
                        }
                    }

                    ret.Status = true;
                    return(ret);
                }
                //栏目数据
                if (RenderMode == 2)
                {
                    //获取当前栏目下的文章列表
                    //解析文章列表模板设置数据源 添加id为contents 模板
                    Tag element  = this.Document.GetChildTagById("contents");
                    int total    = int.Parse(element.Attributes.GetValue("total", "8"));
                    int startNum = int.Parse(element.Attributes.GetValue("startNum", "1"));//从第几条开始
                    //跳过多少条数据
                    int skipNum         = (channelPage - 1) * total + (startNum - 1);
                    var channelContents = generateApp.GetContentSummaryByChannelId(channelId, skipNum, total);


                    this.Document.Variables.SetValue("contents", channelContents);
                    this.Document.Variables.SetValue("channel", channelModel);

                    string renderHtml = this.Document.GetRenderText();
                    ret.Status = true;
                    ret.Html   = renderHtml;
                    return(ret);
                }
                //文章数据
                if (RenderMode == 3)
                {
                    this.Document.Variables.SetValue("channel", channelModel);
                    this.Document.Variables.SetValue("content", contentModel);
                    //渲染内容数据
                    string renderHtml = this.Document.GetRenderText();
                    renderHtml = HtmlPlayerHandler.CreateVideo(renderHtml);
                    ret.Status = true;
                    ret.Html   = renderHtml;
                    return(ret);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Exception(ex);
            }

            return(ret);
        }