feat: support post custom template.

pull/435/head
ruibaby 2019-12-20 16:47:02 +08:00
parent a0d4c7493f
commit 0c189ee06c
4 changed files with 48 additions and 4 deletions

View File

@ -80,9 +80,14 @@ public class ThemeController {
themeService.saveTemplateContent(themeId, param.getPath(), param.getContent());
}
@GetMapping("files/custom")
public Set<String> customTemplate() {
return themeService.listCustomTemplates(themeService.getActivatedThemeId());
@GetMapping("activation/template/custom/sheet")
public Set<String> customSheetTemplate() {
return themeService.listCustomTemplates(themeService.getActivatedThemeId(), ThemeService.CUSTOM_SHEET_PREFIX);
}
@GetMapping("activation/template/custom/post")
public Set<String> customPostTemplate() {
return themeService.listCustomTemplates(themeService.getActivatedThemeId(), ThemeService.CUSTOM_POST_PREFIX);
}
@PostMapping("{themeId}/activation")

View File

@ -21,6 +21,7 @@ import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.entity.Tag;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.support.HaloConst;
import run.halo.app.model.vo.BaseCommentVO;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*;
@ -160,6 +161,10 @@ public class ContentArchiveController {
model.addAttribute("metas", postMetaService.convertToMap(metas));
model.addAttribute("comments", comments);
if (themeService.templateExists(ThemeService.CUSTOM_POST_PREFIX + post.getTemplate() + HaloConst.SUFFIX_FTL)) {
return themeService.render(ThemeService.CUSTOM_POST_PREFIX + post.getTemplate());
}
return themeService.render("post");
}

View File

@ -70,10 +70,15 @@ public interface ThemeService {
String THEMES_CACHE_KEY = "themes";
/**
* Custom sheet prefix.
* Custom sheet template prefix.
*/
String CUSTOM_SHEET_PREFIX = "sheet_";
/**
* Custom post template prefix.
*/
String CUSTOM_POST_PREFIX = "post_";
/**
* Theme provider remote name.
*/
@ -132,8 +137,18 @@ public interface ThemeService {
* @param themeId theme id must not be blank
* @return a set of templates
*/
@Deprecated
Set<String> listCustomTemplates(@NonNull String themeId);
/**
* Lists a set of custom template, such as sheet_xxx.ftl/post_xxx.ftl, and xxx will be template name
*
* @param themeId theme id must not be blank
* @param prefix post_ or sheet_
* @return a set of templates
*/
Set<String> listCustomTemplates(@NonNull String themeId, @NonNull String prefix);
/**
* Judging whether template exists under the specified theme
*

View File

@ -187,6 +187,25 @@ public class ThemeServiceImpl implements ThemeService {
}
}
@Override
public Set<String> listCustomTemplates(String themeId, String prefix) {
// Get the theme path
Path themePath = Paths.get(getThemeOfNonNullBy(themeId).getThemePath());
try (Stream<Path> pathStream = Files.list(themePath)) {
return pathStream.filter(path -> StringUtils.startsWithIgnoreCase(path.getFileName().toString(), prefix))
.map(path -> {
// Remove prefix
String customTemplate = StringUtils.removeStartIgnoreCase(path.getFileName().toString(), prefix);
// Remove suffix
return StringUtils.removeEndIgnoreCase(customTemplate, HaloConst.SUFFIX_FTL);
})
.collect(Collectors.toSet());
} catch (IOException e) {
throw new ServiceException("Failed to list files of path " + themePath.toString(), e);
}
}
@Override
public boolean templateExists(String template) {
if (StringUtils.isBlank(template)) {