public string CreateIncludeFile(string virtualUrl, bool isCreateIfExists) { var templateInfo = new TemplateInfo(0, PublishmentSystemId, string.Empty, ETemplateType.FileTemplate, string.Empty, string.Empty, string.Empty, ECharsetUtils.GetEnumType(PublishmentSystemInfo.Additional.Charset), false); var pageInfo = new PageInfo(PublishmentSystemId, 0, PublishmentSystemInfo, templateInfo, null); var contextInfo = new ContextInfo(pageInfo); var parsedVirtualUrl = virtualUrl.Substring(0, virtualUrl.LastIndexOf('.')) + "_parsed" + virtualUrl.Substring(virtualUrl.LastIndexOf('.')); var filePath = PathUtility.MapPath(PublishmentSystemInfo, parsedVirtualUrl); if (!isCreateIfExists && FileUtils.IsFileExists(filePath)) { return(parsedVirtualUrl); } var contentBuilder = new StringBuilder(StlCacheManager.FileContent.GetIncludeContent(PublishmentSystemInfo, virtualUrl, pageInfo.TemplateInfo.Charset)); StlParserManager.ParseTemplateContent(contentBuilder, pageInfo, contextInfo); var pageAfterBodyScripts = StlParserManager.GetPageInfoScript(pageInfo, true); var pageBeforeBodyScripts = StlParserManager.GetPageInfoScript(pageInfo, false); contentBuilder.Insert(0, pageBeforeBodyScripts); contentBuilder.Append(pageAfterBodyScripts); GenerateFile(filePath, pageInfo.TemplateInfo.Charset, contentBuilder); return(parsedVirtualUrl); }
public static void Parse(SiteInfo siteInfo, PageInfo pageInfo, ContextInfo contextInfo, StringBuilder contentBuilder, string filePath, bool isDynamic) { foreach (var service in PluginManager.Services) { try { service.OnBeforeStlParse(new ParseEventArgs(pageInfo.SiteId, pageInfo.PageChannelId, pageInfo.PageContentId, pageInfo.TemplateInfo.TemplateType, pageInfo.TemplateInfo.Id, filePath, contentBuilder)); } catch (Exception ex) { LogUtils.AddPluginErrorLog(service.PluginId, ex, nameof(service.OnBeforeStlParse)); } } if (contentBuilder.Length > 0) { StlParserManager.ParseTemplateContent(contentBuilder, pageInfo, contextInfo); } if (EFileSystemTypeUtils.IsHtml(PathUtils.GetExtension(filePath))) { if (isDynamic) { var pageUrl = PageUtils.AddProtocolToUrl(PageUtils.ParseNavigationUrl($"~/{PathUtils.GetPathDifference(WebConfigUtils.PhysicalApplicationPath, filePath)}")); string templateString = $@" <base href=""{pageUrl}"" />"; StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString); } if (pageInfo.SiteInfo.Additional.IsCreateBrowserNoCache) { const string templateString = @" <META HTTP-EQUIV=""Pragma"" CONTENT=""no-cache""> <META HTTP-EQUIV=""Expires"" CONTENT=""-1"">"; StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString); } if (pageInfo.SiteInfo.Additional.IsCreateIe8Compatible) { const string templateString = @" <META HTTP-EQUIV=""x-ua-compatible"" CONTENT=""ie=7"" />"; StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString); } if (pageInfo.SiteInfo.Additional.IsCreateJsIgnoreError) { const string templateString = @" <script type=""text/javascript"">window.onerror=function(){return true;}</script>"; StringUtils.InsertAfter(new[] { "<head>", "<HEAD>" }, contentBuilder, templateString); } if (pageInfo.PageContentId > 0 && pageInfo.SiteInfo.Additional.IsCountHits && !pageInfo.IsPageScriptsExists(PageInfo.Const.JsAdStlCountHits)) { pageInfo.AddPageEndScriptsIfNotExists(PageInfo.Const.JsAdStlCountHits, $@" <script src=""{ApiRouteActionsAddContentHits.GetUrl(pageInfo.ApiUrl, pageInfo.SiteId, pageInfo.PageChannelId, pageInfo.PageContentId)}"" type=""text/javascript""></script>"); } var isShowPageInfo = pageInfo.SiteInfo.Additional.IsCreateShowPageInfo; if (!pageInfo.IsLocal) { if (pageInfo.SiteInfo.Additional.IsCreateDoubleClick) { var fileTemplateId = 0; if (pageInfo.TemplateInfo.TemplateType == TemplateType.FileTemplate) { fileTemplateId = pageInfo.TemplateInfo.Id; } var apiUrl = pageInfo.ApiUrl; var ajaxUrl = ApiRouteActionsTrigger.GetUrl(apiUrl, pageInfo.SiteId, contextInfo.ChannelId, contextInfo.ContentId, fileTemplateId, true); pageInfo.AddPageEndScriptsIfNotExists("CreateDoubleClick", $@" <script type=""text/javascript"" language=""javascript"">document.ondblclick=function(x){{location.href = '{ajaxUrl}&returnUrl=' + encodeURIComponent(location.search);}}</script>"); } } else { isShowPageInfo = true; } if (isShowPageInfo) { contentBuilder.Append($@" <!-- {pageInfo.TemplateInfo.RelatedFileName}({TemplateTypeUtils.GetText(pageInfo.TemplateInfo.TemplateType)}) -->"); } var headScripts = StlParserManager.GetPageInfoHeadScript(pageInfo, contextInfo); if (!string.IsNullOrEmpty(headScripts)) { if (contentBuilder.ToString().IndexOf("</head>", StringComparison.Ordinal) != -1 || contentBuilder.ToString().IndexOf("</HEAD>", StringComparison.Ordinal) != -1) { StringUtils.InsertBefore(new[] { "</head>", "</HEAD>" }, contentBuilder, headScripts); } else { contentBuilder.Insert(0, headScripts); } } var afterBodyScripts = StlParserManager.GetPageInfoScript(pageInfo, true); if (!string.IsNullOrEmpty(afterBodyScripts)) { if (contentBuilder.ToString().IndexOf("<body", StringComparison.Ordinal) != -1 || contentBuilder.ToString().IndexOf("<BODY", StringComparison.Ordinal) != -1) { var index = contentBuilder.ToString().IndexOf("<body", StringComparison.Ordinal); if (index == -1) { index = contentBuilder.ToString().IndexOf("<BODY", StringComparison.Ordinal); } index = contentBuilder.ToString().IndexOf(">", index, StringComparison.Ordinal); contentBuilder.Insert(index + 1, StringUtils.Constants.ReturnAndNewline + afterBodyScripts + StringUtils.Constants.ReturnAndNewline); } else { contentBuilder.Insert(0, afterBodyScripts); } } var beforeBodyScripts = StlParserManager.GetPageInfoScript(pageInfo, false); if (!string.IsNullOrEmpty(beforeBodyScripts)) { if (contentBuilder.ToString().IndexOf("</body>", StringComparison.Ordinal) != -1 || contentBuilder.ToString().IndexOf("</BODY>", StringComparison.Ordinal) != -1) { var index = contentBuilder.ToString().IndexOf("</body>", StringComparison.Ordinal); if (index == -1) { index = contentBuilder.ToString().IndexOf("</BODY>", StringComparison.Ordinal); } contentBuilder.Insert(index, StringUtils.Constants.ReturnAndNewline + beforeBodyScripts + StringUtils.Constants.ReturnAndNewline); } else { contentBuilder.Append(beforeBodyScripts); } } if (pageInfo.PageEndScriptKeys.Count > 0) { var endScriptBuilder = new StringBuilder(); foreach (string scriptKey in pageInfo.PageEndScriptKeys) { endScriptBuilder.Append(pageInfo.GetPageEndScripts(scriptKey)); } endScriptBuilder.Append(StringUtils.Constants.ReturnAndNewline); //contentBuilder.Append(endScriptBuilder.ToString()); //StringUtils.InsertBeforeOrAppend(new string[] { "</body>", "</BODY>" }, contentBuilder, endScriptBuilder.ToString()); StringUtils.InsertAfterOrAppend(new[] { "</html>", "</html>" }, contentBuilder, endScriptBuilder.ToString()); } } foreach (var service in PluginManager.Services) { try { service.OnAfterStlParse(new ParseEventArgs(pageInfo.SiteId, pageInfo.PageChannelId, pageInfo.PageContentId, pageInfo.TemplateInfo.TemplateType, pageInfo.TemplateInfo.Id, filePath, contentBuilder)); } catch (Exception ex) { LogUtils.AddPluginErrorLog(service.PluginId, ex, nameof(service.OnAfterStlParse)); } } }