コード例 #1
0
 //
 public static void process(CPBaseClass cp, HtmlDocument htmlDoc, ref List <string> userMessageList)
 {
     //
     // -- data attribute
     {
         string             xPath    = "//*[@data-layout]";
         HtmlNodeCollection nodeList = htmlDoc.DocumentNode.SelectNodes(xPath);
         if (nodeList != null)
         {
             foreach (HtmlNode node in nodeList)
             {
                 string layoutRecordName = node.Attributes["data-layout"]?.Value;
                 node.Attributes.Remove("data-layout");
                 //
                 // -- body found, set the htmlDoc to the body
                 var layoutDoc = new HtmlDocument();
                 layoutDoc.LoadHtml(node.InnerHtml);
                 //
                 // -- process the layout
                 DataDeleteController.process(layoutDoc);
                 MustacheVariableController.process(layoutDoc);
                 MustacheSectionController.process(layoutDoc);
                 MustacheTruthyController.process(layoutDoc);
                 MustacheInvertedSectionController.process(layoutDoc);
                 MustacheValueController.process(layoutDoc);
                 DataAddonController.process(cp, layoutDoc);
                 //
                 // -- save the alyout
                 LayoutModel layout = null;
                 if ((layout == null) && !string.IsNullOrWhiteSpace(layoutRecordName))
                 {
                     layout = DbBaseModel.createByUniqueName <LayoutModel>(cp, layoutRecordName);
                     if (layout == null)
                     {
                         layout      = DbBaseModel.addDefault <LayoutModel>(cp);
                         layout.name = layoutRecordName;
                     }
                     layout.layout.content = layoutDoc.DocumentNode.OuterHtml;
                     layout.save(cp);
                     userMessageList.Add("Saved Layout '" + layoutRecordName + "' from data-layout attribute.");
                 }
             }
         }
     }
 }
コード例 #2
0
 //
 //====================================================================================================
 /// <summary>
 /// Process and return an htmlagility object. Any processing errors are returned in userMessageList. If UserMessageList.count is 0, there wre no errors.
 /// </summary>
 /// <param name="cp"></param>
 /// <param name="htmlDoc"></param>
 /// <param name="importTypeId"></param>
 /// <param name="userMessageList"></param>
 /// <returns></returns>
 private static bool processHtmlDoc(CPBaseClass cp, HtmlDocument htmlDoc, ImporttypeEnum importTypeId, ref List <string> userMessageList)
 {
     //
     // -- get body (except email template because it uses the full html document
     if (importTypeId != ImporttypeEnum.EmailTemplate)
     {
         //
         // -- find the data-body or body tag
         {
             string             xPath    = "//*[@data-body]";
             HtmlNodeCollection nodeList = htmlDoc.DocumentNode.SelectNodes(xPath);
             if (nodeList != null)
             {
                 //
                 // -- import data-body
                 userMessageList.Add("Body set by data-body attribute.");
                 htmlDoc.LoadHtml(nodeList.First().InnerHtml);
             }
             else
             {
                 HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");
                 if (bodyNode == null)
                 {
                     //
                     // -- no body found, use entire document
                     userMessageList.Add("No Body found, entire document imported.");
                 }
                 else
                 {
                     //
                     // -- use body
                     string body = bodyNode.InnerHtml;
                     if (string.IsNullOrWhiteSpace(body))
                     {
                         //
                         // -- body tag not found, import the whole document
                         userMessageList.Add("The content does not include a data-body attribute and the body tag is empty.");
                         return(false);
                     }
                     //
                     // -- body found, set the htmlDoc to the body
                     userMessageList.Add("Html Body imported.");
                     htmlDoc.LoadHtml(body);
                 }
             }
         }
     }
     //
     // -- process data-layout nodes. Within each node found, run all other controllers inddividually then save
     DataLayoutController.process(cp, htmlDoc, ref userMessageList);
     //
     // -- process the body
     DataDeleteController.process(htmlDoc);
     MustacheVariableController.process(htmlDoc);
     MustacheSectionController.process(htmlDoc);
     MustacheTruthyController.process(htmlDoc);
     MustacheInvertedSectionController.process(htmlDoc);
     MustacheValueController.process(htmlDoc);
     DataAddonController.process(cp, htmlDoc);
     DataHrefController.process(htmlDoc);
     DataSrcController.process(htmlDoc);
     //
     return(true);
 }