试过了 Velocity 各种转义 也没能找到输出 Markdown 标题的方法,于是干脆将标题设置为自定义为字符串了,具体实现如下:
Java 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 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 模版
1 2 3 4 5
| $H1 $msg $H2 $msg $H3 $msg $H4 $msg $H5 $msg
|
模版输出结果
1 2 3 4 5
| # Hello World! ## Hello World! ### Hello World! #### Hello World! ##### Hello World!
|
求问
还有没有更优雅的方式?