Create Freemarker custom tags

pull/137/head
ruibaby 2019-03-22 15:59:14 +08:00
parent fadf12a065
commit a2335bb9a2
13 changed files with 266 additions and 95 deletions

View File

@ -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);

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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;
/**
* <pre>
* FreeMarker
* </pre>
*
* @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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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";

View File

@ -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());
}
}

View File

@ -36,7 +36,7 @@ public class HaloConst {
/**
* All of the options
*/
public static Map<String, String> OPTIONS = new HashMap<>();
public static Map<String, String> OPTIONS;
/**
* All of the Owo
@ -46,7 +46,7 @@ public class HaloConst {
/**
* All of the themes
*/
public static List<Theme> THEMES = new ArrayList<>();
public static List<Theme> THEMES;
/**
* user_session
@ -56,7 +56,7 @@ public class HaloConst {
/**
*
*/
public static Map<Long, Long> POSTS_VIEWS = new HashMap<>();
public static Map<Long, Long> POSTS_VIEWS;
/**
*

View File

@ -5,6 +5,7 @@ import cc.ryanc.halo.model.entity.Category;
import cc.ryanc.halo.model.params.CategoryParam;
import cc.ryanc.halo.model.vo.CategoryVO;
import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.PostCategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
@ -27,8 +28,11 @@ public class CategoryController {
private final CategoryService categoryService;
public CategoryController(CategoryService categoryService) {
private final PostCategoryService postCategoryService;
public CategoryController(CategoryService categoryService, PostCategoryService postCategoryService) {
this.categoryService = categoryService;
this.postCategoryService = postCategoryService;
}
@GetMapping("tree")
@ -67,5 +71,6 @@ public class CategoryController {
@ApiOperation("Delete category by id")
public void deletePermanently(@PathVariable("id") Integer id) {
categoryService.removeById(id);
postCategoryService.removeByCategoryId(id);
}
}

View File

@ -7,7 +7,9 @@ import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.params.PostParam;
import cc.ryanc.halo.model.vo.PostDetailVO;
import cc.ryanc.halo.service.PostCategoryService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.PostTagService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -31,8 +33,14 @@ public class PostController {
private final PostService postService;
public PostController(PostService postService) {
private final PostCategoryService postCategoryService;
private final PostTagService postTagService;
public PostController(PostService postService, PostCategoryService postCategoryService, PostTagService postTagService) {
this.postService = postService;
this.postCategoryService = postCategoryService;
this.postTagService = postTagService;
}
@GetMapping("latest")
@ -80,5 +88,7 @@ public class PostController {
public void deletePermanently(@PathVariable("postId") Integer postId) {
// Remove it
postService.removeById(postId);
postCategoryService.removeByPostId(postId);
postTagService.removeByPostId(postId);
}
}

View File

@ -78,5 +78,6 @@ public class TagController {
@ApiOperation("Delete tag by id")
public void deletePermanently(@PathVariable("id") Integer id) {
tagService.removeById(id);
postTagService.removeByTagId(id);
}
}

View File

@ -5,6 +5,7 @@ import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.vo.PostListVO;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.TagService;
import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.domain.Page;
@ -80,10 +81,7 @@ public class ContentTagController extends BaseContentController {
if (null == tag) {
return this.renderNotFound();
}
int size = 10;
if (StrUtil.isNotBlank(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue()))) {
size = Integer.parseInt(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue()));
}
int size = HaloUtils.getDefaultPageSize();
final Pageable pageable = PageRequest.of(page - 1, size, sort);
// TODO get posts by tag