Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new File(DirUtil.getResourcePath(), "template"));
cfg.setDefaultEncoding("UTF-8");
// cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
// Don't log exceptions inside FreeMarker that it will thrown at you anyway:
cfg.setLogTemplateExceptions(false);
// Wrap unchecked exceptions thrown during template processing into TemplateException-s:
cfg.setWrapUncheckedExceptions(true);
// Do not fall back to higher scopes when reading a null loop variable:
cfg.setFallbackOnNullLoopVariable(false);
//自定义标签
cfg.setSharedVariable("BlockList", new NotionBlockListTagDirective());
//获取文章数据
Notion notion = new Notion();
boolean refresh = false;
// 42b5895d-6092-4578-8a24-91d0dcb13104
String pageId = "073d0837-9ebe-4981-b533-2ce325070f6b";
String s = ConfigUtil.readFromFile("json/", pageId + ".json");
Article article;
if (s.equals("") || refresh) {
article = notion.getArticle(pageId);
ConfigUtil.writeToFile("json/" + pageId + ".json", JSON.toJSONString(article));
} else
article = JSON.parseObject(s, Article.class);
//把 生成的block列表模板当作参数生成一整个页面
HashMap<String, Object> dataModel = new HashMap<>();
//准备最终数据
// LinkedHashMap<String, BlockNode> blocks = new LinkedHashMap<>();
dataModel.put("blocks", article.getBlocks());
//模板
Template temp = cfg.getTemplate("html/page.ftl");
//输出
StringWriter stringWriter = new StringWriter();
File file = new File(DirUtil.getRootPathFile(), "html");
if (!file.exists()) file.mkdirs();
file = new File(file, pageId + ".html");
temp.process(dataModel, new FileWriter(file));
// System.out.println(stringWriter.toString());
System.out.println("done!!!");
评论区