diff --git a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java index b317d36ca..0a21e3084 100644 --- a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java +++ b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java @@ -3,10 +3,8 @@ package cc.ryanc.halo.config; import cc.ryanc.halo.model.freemarker.method.RandomMethod; import cc.ryanc.halo.model.freemarker.method.RecentCommentsMethod; import cc.ryanc.halo.model.freemarker.method.RecentPostsMethod; -import cc.ryanc.halo.model.freemarker.tag.ArticleTagDirective; -import cc.ryanc.halo.model.freemarker.tag.CommonTagDirective; +import cc.ryanc.halo.model.freemarker.tag.*; import cc.ryanc.halo.service.OptionService; -import cc.ryanc.halo.service.UserService; import freemarker.template.TemplateModelException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -33,13 +31,22 @@ public class FreeMarkerAutoConfiguration { private OptionService optionsService; @Autowired - private UserService userService; + private PostTagDirective postTagDirective; @Autowired - private CommonTagDirective commonTagDirective; + private CategoryTagDirective categoryTagDirective; @Autowired - private ArticleTagDirective articleTagDirective; + private CommentTagDirective commentTagDirective; + + @Autowired + private LinkTagDirective linkTagDirective; + + @Autowired + private MenuTagDirective menuTagDirective; + + @Autowired + private TagTagDirective tagTagDirective; @Autowired private RandomMethod randomMethod; @@ -53,11 +60,14 @@ public class FreeMarkerAutoConfiguration { @PostConstruct public void setSharedVariable() { try { - //自定义标签 - configuration.setSharedVariable("commonTag", commonTagDirective); - configuration.setSharedVariable("articleTag", articleTagDirective); configuration.setSharedVariable("options", optionsService.listOptions()); -// configuration.setSharedVariable("user", userService.findUser()); + //自定义标签 + configuration.setSharedVariable("categoryTag", categoryTagDirective); + configuration.setSharedVariable("commentTag", commentTagDirective); + configuration.setSharedVariable("linkTag", linkTagDirective); + configuration.setSharedVariable("menuTag", menuTagDirective); + configuration.setSharedVariable("tagTag", tagTagDirective); + configuration.setSharedVariable("postTag", postTagDirective); configuration.setSharedVariable("randomMethod", randomMethod); configuration.setSharedVariable("recentPostsMethod", recentPostsMethod); configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod); diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CategoryTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CategoryTagDirective.java new file mode 100644 index 000000000..88bab1b8f --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CategoryTagDirective.java @@ -0,0 +1,44 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.CategoryService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class CategoryTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final CategoryService categoryService; + + public CategoryTagDirective(CategoryService categoryService) { + this.categoryService = categoryService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("categories", builder.build().wrap(categoryService.listAll())); + break; + case "count": + env.setVariable("count", builder.build().wrap(categoryService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommentTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommentTagDirective.java new file mode 100644 index 000000000..b64371c35 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommentTagDirective.java @@ -0,0 +1,43 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.CommentService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class CommentTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final CommentService commentService; + + public CommentTagDirective(CommentService commentService) { + this.commentService = commentService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + int top = Integer.parseInt(params.get("top").toString()); + switch (method) { + case "latest": + env.setVariable("categories", builder.build().wrap(builder.build().wrap(builder.build().wrap(commentService.pageLatest(top))))); + break; + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java deleted file mode 100644 index 143e2594f..000000000 --- a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java +++ /dev/null @@ -1,75 +0,0 @@ -package cc.ryanc.halo.model.freemarker.tag; - -import cc.ryanc.halo.service.*; -import freemarker.core.Environment; -import freemarker.template.*; -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.util.Map; - -/** - *
- * FreeMarker自定义标签 - *- * - * @author : RYAN0UP - * @date : 2018/4/26 - */ -@Component -public class CommonTagDirective implements TemplateDirectiveModel { - - private static final String METHOD_KEY = "method"; - - private final MenuService menuService; - - private final CategoryService categoryService; - - private final TagService tagService; - - private final LinkService linkService; - - private final CommentService commentService; - - public CommonTagDirective(MenuService menuService, - CategoryService categoryService, - TagService tagService, - LinkService linkService, - CommentService commentService) { - this.menuService = menuService; - this.categoryService = categoryService; - this.tagService = tagService; - this.linkService = linkService; - this.commentService = commentService; - } - - @Override - public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { - final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); - - if (params.containsKey(METHOD_KEY)) { - String method = params.get(METHOD_KEY).toString(); - switch (method) { - case "menus": - env.setVariable("menus", builder.build().wrap(menuService.listAll())); - break; - case "categories": - env.setVariable("categories", builder.build().wrap(categoryService.listAll())); - break; - case "tags": - env.setVariable("tags", builder.build().wrap(tagService.listAll())); - break; - case "links": - env.setVariable("links", builder.build().wrap(linkService.listAll())); - break; - case "newComments": - env.setVariable("newComments", builder.build().wrap(commentService.listAll())); - break; - default: - break; - } - } - body.render(env.getOut()); - } - -} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/LinkTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/LinkTagDirective.java new file mode 100644 index 000000000..d5ad108d8 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/LinkTagDirective.java @@ -0,0 +1,47 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.LinkService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class LinkTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final LinkService linkService; + + public LinkTagDirective(LinkService linkService) { + this.linkService = linkService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("links", builder.build().wrap(linkService.listAll())); + break; + case "listTeam": + env.setVariable("links", builder.build().wrap(null)); + break; + case "count": + env.setVariable("count", builder.build().wrap(linkService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/MenuTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/MenuTagDirective.java new file mode 100644 index 000000000..8b179c25c --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/MenuTagDirective.java @@ -0,0 +1,44 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.MenuService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class MenuTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final MenuService menuService; + + public MenuTagDirective(MenuService menuService) { + this.menuService = menuService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("menus", builder.build().wrap(menuService.listAll())); + break; + case "count": + env.setVariable("count", builder.build().wrap(menuService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/ArticleTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/PostTagDirective.java similarity index 91% rename from src/main/java/cc/ryanc/halo/model/freemarker/tag/ArticleTagDirective.java rename to src/main/java/cc/ryanc/halo/model/freemarker/tag/PostTagDirective.java index 3945e4792..a9f1f368a 100644 --- a/src/main/java/cc/ryanc/halo/model/freemarker/tag/ArticleTagDirective.java +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/PostTagDirective.java @@ -19,7 +19,7 @@ import java.util.Map; * @date : 2018/4/26 */ @Component -public class ArticleTagDirective implements TemplateDirectiveModel { +public class PostTagDirective implements TemplateDirectiveModel { private static final String METHOD_KEY = "method"; diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/TagTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/TagTagDirective.java new file mode 100644 index 000000000..12d847b1b --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/TagTagDirective.java @@ -0,0 +1,44 @@ +package cc.ryanc.halo.model.freemarker.tag; + +import cc.ryanc.halo.service.TagService; +import freemarker.core.Environment; +import freemarker.template.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Map; + +/** + * @author : RYAN0UP + * @date : 2019/3/22 + */ +@Component +public class TagTagDirective implements TemplateDirectiveModel { + + private static final String METHOD_KEY = "method"; + + private final TagService tagService; + + public TagTagDirective(TagService tagService) { + this.tagService = tagService; + } + + @Override + public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { + final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); + + if (params.containsKey(METHOD_KEY)) { + String method = params.get(METHOD_KEY).toString(); + switch (method) { + case "list": + env.setVariable("tags", builder.build().wrap(tagService.listAll())); + break; + case "count": + env.setVariable("count", builder.build().wrap(tagService.count())); + default: + break; + } + } + body.render(env.getOut()); + } +} diff --git a/src/main/java/cc/ryanc/halo/model/support/HaloConst.java b/src/main/java/cc/ryanc/halo/model/support/HaloConst.java index 0196a336f..968e91c7e 100644 --- a/src/main/java/cc/ryanc/halo/model/support/HaloConst.java +++ b/src/main/java/cc/ryanc/halo/model/support/HaloConst.java @@ -36,7 +36,7 @@ public class HaloConst { /** * All of the options */ - public static Map