/// <summary> /// /// </summary> /// <returns></returns> public FileInfo Compile() { // HTML模板存放路徑 string sourceFileName = System.Web.Hosting.HostingEnvironment.MapPath(ConfigurationUtil.HtmlTemplatePath); // 靜態頁面作成后存放的路徑(文件名:日期時間形式) string destFileName = System.Web.Hosting.HostingEnvironment.MapPath(ConfigurationUtil.GetKeyValue("HtmlWork") + string.Format(CultureInfo.CurrentCulture, "{0}.html", DateTime.Now.ToString("yyyyMMddHHmmssfff", CultureInfo.CurrentCulture))); // 文件移動 File.Copy(sourceFileName, destFileName); StringBuilder htmlTemplate = new StringBuilder(); // 讀取HTML模板內容 htmlTemplate.Append(File.ReadAllText(destFileName, Encoding.UTF8)); // 取得HTML和JS的替換內容 GetHtmlReplaceContentMaps(); // 遍歷已選擇的題型 foreach (KeyValuePair <string, ConcurrentDictionary <SubstituteType, string> > d in _htmlMaps) { LogUtil.LogDebug(MessageUtil.GetMessage(() => MsgResources.I0001A, d.Key)); // 替換HTML模板中的預留內容(HTML、JS注入操作) foreach (KeyValuePair <SubstituteType, string> m in d.Value) { LogUtil.LogDebug(MessageUtil.GetMessage(() => MsgResources.I0002A, m.Key)); switch (m.Key) { // 樣式庫引用注入 case SubstituteType.Stylesheet: Stylesheet.AppendLine(m.Value); break; // 腳本引用注入 case SubstituteType.Script: Script.AppendLine(m.Value); break; // 打印前設置事件注入 case SubstituteType.PrintSettingEvent: PrintSettingEvent.AppendLine(m.Value); break; // 打印后設置事件注入 case SubstituteType.PrintAfterSettingEvent: PrintAfterSettingEvent.AppendLine(m.Value); break; // 準備事件注入 case SubstituteType.ReadyEvent: ReadyEvent.AppendLine(m.Value); break; // 交卷事件注入 case SubstituteType.TheirPapersEvent: TictheirPapersEvent.AppendLine(m.Value); break; // 答題訂正事件注入 case SubstituteType.MakeCorrectionsEvent: MakeCorrectionsEvent.AppendLine(m.Value); break; // 題型正文注入 case SubstituteType.Content: Content.AppendLine(m.Value); break; default: break; } } } // 樣式庫注入 htmlTemplate.Replace("<!--STYLESHEET-->", Stylesheet.ToString()); // 腳本注入 htmlTemplate.Replace("<!--SCRIPT-->", Script.ToString()); // 打印前設置事件注入 htmlTemplate.Replace("// PRINTSETTING", PrintSettingEvent.ToString()); // 打印后設置事件注入 htmlTemplate.Replace("// PRINTAFTERSETTING", PrintAfterSettingEvent.ToString()); // 題型準備事件注入 htmlTemplate.Replace("// READY", ReadyEvent.ToString()); // 題型訂正事件注入 htmlTemplate.Replace("// MAKECORRECTIONS", MakeCorrectionsEvent.ToString()); // 題型交卷事件注入 htmlTemplate.Replace("// TICTHEIRPAPERS", TictheirPapersEvent.ToString()); // 題型正文注入 htmlTemplate.Replace("<!--CONTENT-->", Content.Insert(0, IsEncryptScript).AppendLine().ToString()); LogUtil.LogDebug(MessageUtil.GetMessage(() => MsgResources.I0003L)); // 保存至靜態頁面 File.WriteAllText(destFileName, htmlTemplate.ToString(), Encoding.UTF8); return(new FileInfo(destFileName)); }