forked from lxm_tools/screw
优化从类路径加载velocity模板.
This commit is contained in:
@@ -55,4 +55,8 @@ public class EngineConfig implements Serializable {
|
|||||||
* 文件名称
|
* 文件名称
|
||||||
*/
|
*/
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
/**
|
||||||
|
* 模板内容
|
||||||
|
*/
|
||||||
|
private String templateContent;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import cn.smallbun.screw.core.metadata.model.DataModel;
|
|||||||
import cn.smallbun.screw.core.util.Assert;
|
import cn.smallbun.screw.core.util.Assert;
|
||||||
import cn.smallbun.screw.core.util.ExceptionUtils;
|
import cn.smallbun.screw.core.util.ExceptionUtils;
|
||||||
import cn.smallbun.screw.core.util.StringUtils;
|
import cn.smallbun.screw.core.util.StringUtils;
|
||||||
|
import java.util.HashMap;
|
||||||
import org.apache.velocity.Template;
|
import org.apache.velocity.Template;
|
||||||
import org.apache.velocity.VelocityContext;
|
import org.apache.velocity.VelocityContext;
|
||||||
import org.apache.velocity.app.Velocity;
|
import org.apache.velocity.app.Velocity;
|
||||||
@@ -33,6 +34,7 @@ import java.io.BufferedWriter;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import org.apache.velocity.runtime.log.NullLogChute;
|
||||||
|
|
||||||
import static cn.smallbun.screw.core.constant.DefaultConstants.DEFAULT_ENCODING;
|
import static cn.smallbun.screw.core.constant.DefaultConstants.DEFAULT_ENCODING;
|
||||||
import static cn.smallbun.screw.core.engine.EngineTemplateType.velocity;
|
import static cn.smallbun.screw.core.engine.EngineTemplateType.velocity;
|
||||||
@@ -74,6 +76,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
velocityEngine.setProperty("file.resource.loader.class",
|
velocityEngine.setProperty("file.resource.loader.class",
|
||||||
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
||||||
}
|
}
|
||||||
|
velocityEngine.setProperty("runtime.log.logsystem.class", NullLogChute.class.getName());
|
||||||
velocityEngine.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
|
velocityEngine.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
|
||||||
velocityEngine.setProperty(Velocity.ENCODING_DEFAULT, DEFAULT_ENCODING);
|
velocityEngine.setProperty(Velocity.ENCODING_DEFAULT, DEFAULT_ENCODING);
|
||||||
velocityEngine.setProperty(Velocity.INPUT_ENCODING, DEFAULT_ENCODING);
|
velocityEngine.setProperty(Velocity.INPUT_ENCODING, DEFAULT_ENCODING);
|
||||||
@@ -89,8 +92,10 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
@Override
|
@Override
|
||||||
public void produce(DataModel info, String docName) throws ProduceException {
|
public void produce(DataModel info, String docName) throws ProduceException {
|
||||||
Assert.notNull(info, "DataModel can not be empty!");
|
Assert.notNull(info, "DataModel can not be empty!");
|
||||||
Template template;
|
Template template = null;
|
||||||
try {
|
try {
|
||||||
|
String templateContent = getEngineConfig().getTemplateContent();
|
||||||
|
if (StringUtils.isBlank(templateContent)) {
|
||||||
// get template path
|
// get template path
|
||||||
String path = getEngineConfig().getCustomTemplate();
|
String path = getEngineConfig().getCustomTemplate();
|
||||||
//如果自定义了模板
|
//如果自定义了模板
|
||||||
@@ -105,6 +110,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
+ velocity.getSuffix(),
|
+ velocity.getSuffix(),
|
||||||
DEFAULT_ENCODING);
|
DEFAULT_ENCODING);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// output
|
// output
|
||||||
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
|
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(outStream, DEFAULT_ENCODING);
|
OutputStreamWriter writer = new OutputStreamWriter(outStream, DEFAULT_ENCODING);
|
||||||
@@ -112,8 +118,21 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
//put data
|
//put data
|
||||||
VelocityContext context = new VelocityContext();
|
VelocityContext context = new VelocityContext();
|
||||||
context.put(DATA, info);
|
context.put(DATA, info);
|
||||||
|
context.put("markdown", new HashMap<String, String>() {
|
||||||
|
{
|
||||||
|
put("h1", "#");
|
||||||
|
put("h2", "##");
|
||||||
|
put("h3", "###");
|
||||||
|
put("h4", "####");
|
||||||
|
put("h5", "#####");
|
||||||
|
}
|
||||||
|
});
|
||||||
//generate
|
//generate
|
||||||
|
if (template != null) {
|
||||||
template.merge(context, sw);
|
template.merge(context, sw);
|
||||||
|
} else {
|
||||||
|
velocityEngine.evaluate(context, sw, "test", templateContent);
|
||||||
|
}
|
||||||
// open the output directory
|
// open the output directory
|
||||||
openOutputDir();
|
openOutputDir();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user