package com.xcong.farmer.cms.core.template;
|
|
import cn.hutool.core.collection.CollUtil;
|
import com.xcong.farmer.cms.core.node.PartNode;
|
import com.xcong.farmer.cms.core.tag.TagsEnum;
|
import org.jsoup.nodes.Document;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author wzy
|
* @date 2022-07-01
|
**/
|
public class TemplateConfiguration extends Configuration{
|
|
private TemplateLoader templateLoader;
|
|
public TemplateConfiguration(String templatePath, String staticPath, String outputPath) {
|
super(templatePath, staticPath, outputPath);
|
}
|
|
public void templateLoader(TemplateLoader templateLoader) {
|
this.templateLoader = templateLoader;
|
}
|
|
public void process(Map<String, Object> map, String templateName) {
|
if (this.templateLoader == null) {
|
throw new RuntimeException("TemplateLoader do not able to be null");
|
}
|
|
this.templateLoader.data(map);
|
output(template(templateName));
|
}
|
|
|
public Template template(String templateName) {
|
return template(new File(path(this.templatePath) + templateName));
|
}
|
|
public Template template(File file) {
|
if (file == null) {
|
throw new RuntimeException("template not exist");
|
}
|
|
return this.templateLoader.template(file);
|
}
|
|
// public void columnProcess(Map<String, Object> data, String templateName) {
|
// process;
|
// }
|
//
|
//
|
// public void articleProcess(Map<String, Object> data, String templateName) {
|
// process(data, templateName);
|
// }
|
|
public void output(Template template) {
|
Document document = template.getDocument();
|
List<PartNode> partNodes = template.getPartNodes();
|
StringBuilder sb = new StringBuilder();
|
for (PartNode partNode : partNodes) {
|
sb.append(partNode.getHtml());
|
}
|
document.body().empty().html(sb.toString());
|
String outPath = path(this.outputPath);
|
|
String html = document.html();
|
try {
|
FileOutputStream outputStream = new FileOutputStream(outPath + template.getName());
|
outputStream.write(html.getBytes());
|
outputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
private String path(String path) {
|
if (!path.endsWith("/")) {
|
path = path + "/";
|
}
|
return path;
|
}
|
}
|