试过了 Velocity 各种转义也没能找到输出 Markdown 标题的方法,于是干脆将标题设置为自定义为字符串了,具体实现如下:
Java 代码
package com.corning.test;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class VelocityAnnotationOutput {
public static void main(String[] args) throws IOException {
VelocityEngine ve = new VelocityEngine();
ve.init();
try (FileWriter readmeWriter = new FileWriter(new File("demo.txt"))) {
VelocityContext context = getVelocityContextMD();
context.put("msg", "Hello World!");
ve.getTemplate("src/main/resources/templates/demo.vm").merge(context, readmeWriter);
readmeWriter.flush();
}
}
private static VelocityContext getVelocityContextMD() {
VelocityContext context = new VelocityContext();
context.put("H1", "#");
context.put("H2", "##");
context.put("H3", "###");
context.put("H4", "####");
context.put("H5", "#####");
return context;
}
}
Velocity 模版
$H1 $msg
$H2 $msg
$H3 $msg
$H4 $msg
$H5 $msg
模版输出结果
# Hello World!
## Hello World!
### Hello World!
#### Hello World!
##### Hello World!
求问
还有没有更优雅的方式?