侧边栏壁纸
  • 累计撰写 793 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

目 录CONTENT

文章目录

完整示例

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 171 阅读 / 279 字
温馨提示:
本文最后更新于 2022-07-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
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!!!");
0

评论区