From 9bdf44d1fba033ac6a98ffec32ee29ff6fb22cf6 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Fri, 08 Jul 2022 17:00:14 +0800
Subject: [PATCH] fix

---
 src/main/java/com/xcong/farmer/cms/core/node/Template.java |   99 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 91 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/xcong/farmer/cms/core/node/Template.java b/src/main/java/com/xcong/farmer/cms/core/node/Template.java
index 98501ea..51d5c27 100644
--- a/src/main/java/com/xcong/farmer/cms/core/node/Template.java
+++ b/src/main/java/com/xcong/farmer/cms/core/node/Template.java
@@ -2,14 +2,15 @@
 
 import cn.hutool.core.collection.CollUtil;
 import com.xcong.farmer.cms.core.node.PartNode;
+import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.*;
 
 /**
  * @author wzy
@@ -17,18 +18,66 @@
  **/
 public class Template {
 
-    private String name;
+    // 文件名称
+    private String name = "index";
+    // 文件保存路径
+    private String path = "";
 
     private Document document;
-
     private Map<String, Map<String, Object>> params = new HashMap<>();
     private Map<String, Object> system;
     private List<PartNode> partNodes = new ArrayList<>();
 
-    private static boolean HAS_PAGING = false;
+
+    // 页面中包含的标签
+    public static Set<String> TAGS;
+    public volatile static boolean HAS_PAGING = false;
+
+    public Template() {
+        TAGS = new HashSet<>();
+    }
+
+    public Template(File file, Map<String, Object> system) {
+        TAGS = new HashSet<>();
+
+        Document document = null;
+        try {
+            document = Jsoup.parse(file, "utf-8");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        if (document == null) {
+            throw new NullPointerException();
+        }
+
+        this.document = document;
+        this.system = system;
+
+        Object templatePath = system.get("templatePath");
+        if (templatePath != null) {
+            this.path = (String) templatePath;
+        }
+
+        Object templateType = system.get("templateType");
+        if ("article".equals(templateType)) {
+            Object templateName = system.get("templateName");
+            if (templateName != null) {
+                this.name = String.valueOf(templateName);
+            }
+        } else if ("column".equals(templateType)) {
+            Object page = system.get("page");
+            if (!new Integer(1).equals(page)) {
+                this.name = name + "_" + page;
+            }
+        } else {
+
+        }
+
+    }
 
     public void parser() {
-        Elements children = document.body().children();
+        Elements children = document.children();
         if (CollUtil.isNotEmpty(children)) {
             for (Element child : children) {
                 PartNode partNode = new PartNode(child, this.system);
@@ -39,6 +88,40 @@
         }
     }
 
+    public void output(String outputPath) {
+        String suffix = ".html";
+        Document document = this.document;
+        List<PartNode> partNodes = this.partNodes;
+        StringBuilder sb = new StringBuilder();
+        for (PartNode partNode : partNodes) {
+            sb.append(partNode.getHtml());
+        }
+        document = Jsoup.parse(sb.toString());
+        String outPath = path(outputPath);
+
+        String html = document.html();
+        try {
+            String path = outPath + path(this.path);
+            File file = new File(path);
+            if (!file.exists()) {
+                file.mkdirs();
+            }
+
+            FileOutputStream outputStream = new FileOutputStream(path +this.name + suffix);
+            outputStream.write(html.getBytes());
+            outputStream.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private String path(String path) {
+        if (!path.endsWith("/")) {
+            path = path + "/";
+        }
+        return path;
+    }
+
     public String getName() {
         return name;
     }

--
Gitblit v1.9.1