From 64cbe185d21a2048a7a12542dc9f40974015efe3 Mon Sep 17 00:00:00 2001 From: Mario Luo Date: Fri, 7 May 2021 10:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=8E=E7=B1=BB=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=8A=A0=E8=BD=BDvelocity=E6=A8=A1=E6=9D=BF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../screw/core/engine/EngineConfig.java | 4 ++ .../velocity/VelocityTemplateEngine.java | 49 +++++++++++++------ .../template/velocity/documentation_md.vm | 2 +- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/engine/EngineConfig.java b/screw-core/src/main/java/cn/smallbun/screw/core/engine/EngineConfig.java index 5c2a4a3..34849e4 100644 --- a/screw-core/src/main/java/cn/smallbun/screw/core/engine/EngineConfig.java +++ b/screw-core/src/main/java/cn/smallbun/screw/core/engine/EngineConfig.java @@ -55,4 +55,8 @@ public class EngineConfig implements Serializable { * 文件名称 */ private String fileName; + /** + * 模板内容 + */ + private String templateContent; } diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/engine/velocity/VelocityTemplateEngine.java b/screw-core/src/main/java/cn/smallbun/screw/core/engine/velocity/VelocityTemplateEngine.java index 0690503..3e6a5d0 100644 --- a/screw-core/src/main/java/cn/smallbun/screw/core/engine/velocity/VelocityTemplateEngine.java +++ b/screw-core/src/main/java/cn/smallbun/screw/core/engine/velocity/VelocityTemplateEngine.java @@ -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.ExceptionUtils; import cn.smallbun.screw.core.util.StringUtils; +import java.util.HashMap; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; @@ -33,6 +34,7 @@ import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; 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.engine.EngineTemplateType.velocity; @@ -74,6 +76,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine { velocityEngine.setProperty("file.resource.loader.class", "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.ENCODING_DEFAULT, DEFAULT_ENCODING); velocityEngine.setProperty(Velocity.INPUT_ENCODING, DEFAULT_ENCODING); @@ -89,21 +92,24 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine { @Override public void produce(DataModel info, String docName) throws ProduceException { Assert.notNull(info, "DataModel can not be empty!"); - Template template; + Template template = null; try { - // get template path - String path = getEngineConfig().getCustomTemplate(); - //如果自定义了模板 - if (StringUtils.isNotBlank(path)) { - template = velocityEngine.getTemplate(path, DEFAULT_ENCODING); - } - //没有自定义模板,使用核心包自带 - else { - template = velocityEngine - .getTemplate(velocity.getTemplateDir() - + getEngineConfig().getFileType().getTemplateNamePrefix() - + velocity.getSuffix(), - DEFAULT_ENCODING); + String templateContent = getEngineConfig().getTemplateContent(); + if (StringUtils.isBlank(templateContent)) { + // get template path + String path = getEngineConfig().getCustomTemplate(); + //如果自定义了模板 + if (StringUtils.isNotBlank(path)) { + template = velocityEngine.getTemplate(path, DEFAULT_ENCODING); + } + //没有自定义模板,使用核心包自带 + else { + template = velocityEngine + .getTemplate(velocity.getTemplateDir() + + getEngineConfig().getFileType().getTemplateNamePrefix() + + velocity.getSuffix(), + DEFAULT_ENCODING); + } } // output try (FileOutputStream outStream = new FileOutputStream(getFile(docName)); @@ -112,8 +118,21 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine { //put data VelocityContext context = new VelocityContext(); context.put(DATA, info); + context.put("markdown", new HashMap() { + { + put("h1", "#"); + put("h2", "##"); + put("h3", "###"); + put("h4", "####"); + put("h5", "#####"); + } + }); //generate - template.merge(context, sw); + if (template != null) { + template.merge(context, sw); + } else { + velocityEngine.evaluate(context, sw, "test", templateContent); + } // open the output directory openOutputDir(); } diff --git a/screw-core/src/main/resources/template/velocity/documentation_md.vm b/screw-core/src/main/resources/template/velocity/documentation_md.vm index 88919e9..f1a7491 100644 --- a/screw-core/src/main/resources/template/velocity/documentation_md.vm +++ b/screw-core/src/main/resources/template/velocity/documentation_md.vm @@ -47,4 +47,4 @@ #foreach($c in $t.columns) | $!{velocityCount} | $!{c.columnName} | $!{c.columnType} | #if($!{c.decimalDigits})$!{c.decimalDigits}#else 0#end | $!{c.nullable}| $!{c.primaryKey} | $!{c.columnDef} | $!{c.remarks} | #end -#end \ No newline at end of file +#end