From 1ddb1043d4697dfa26b4f86576bf415e0cc8de45 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Fri, 22 Mar 2019 18:51:07 +0800 Subject: [PATCH] Refactor theme module --- pom.xml | 5 ++ .../cc/ryanc/halo/model/support/Theme.java | 6 +- .../halo/model/support/ThemeProperties.java | 27 ++++++++ .../java/cc/ryanc/halo/utils/ThemeUtils.java | 65 +++++++++++++------ .../content/ContentFeedController.java | 2 +- .../content/ContentIndexController.java | 2 +- .../templates/themes/anatole/theme.properties | 9 +++ .../themes/material/theme.properties | 0 8 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 src/main/java/cc/ryanc/halo/model/support/ThemeProperties.java create mode 100644 src/main/resources/templates/themes/anatole/theme.properties create mode 100644 src/main/resources/templates/themes/material/theme.properties diff --git a/pom.xml b/pom.xml index d228f4a0e..925b9a816 100755 --- a/pom.xml +++ b/pom.xml @@ -157,6 +157,11 @@ hutool-extra ${hutool.version} + + cn.hutool + hutool-setting + ${hutool.version} + diff --git a/src/main/java/cc/ryanc/halo/model/support/Theme.java b/src/main/java/cc/ryanc/halo/model/support/Theme.java index cf255fd90..24e280f0f 100644 --- a/src/main/java/cc/ryanc/halo/model/support/Theme.java +++ b/src/main/java/cc/ryanc/halo/model/support/Theme.java @@ -16,9 +16,9 @@ public class Theme implements Serializable { private static final long serialVersionUID = 1L; /** - * Theme name + * Theme dir */ - private String themeName; + private String themeDir; /** * Is support setting options @@ -34,4 +34,6 @@ public class Theme implements Serializable { * Is internal theme */ private boolean isInternal; + + private ThemeProperties properties; } diff --git a/src/main/java/cc/ryanc/halo/model/support/ThemeProperties.java b/src/main/java/cc/ryanc/halo/model/support/ThemeProperties.java new file mode 100644 index 000000000..befbf26cf --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/support/ThemeProperties.java @@ -0,0 +1,27 @@ +package cc.ryanc.halo.model.support; + +import lombok.Data; + +/** + * @author : RYAN0UP + * @date : 2019-03-22 + */ +@Data +public class ThemeProperties { + + private String id; + + private String name; + + private String website; + + private String description; + + private String logo; + + private String version; + + private String author; + + private String authorWebsite; +} diff --git a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java b/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java index 08ca12e34..e5d9f6f5e 100644 --- a/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java +++ b/src/main/java/cc/ryanc/halo/utils/ThemeUtils.java @@ -2,9 +2,11 @@ package cc.ryanc.halo.utils; import cc.ryanc.halo.model.support.HaloConst; import cc.ryanc.halo.model.support.Theme; +import cc.ryanc.halo.model.support.ThemeProperties; import cc.ryanc.halo.web.controller.content.base.BaseContentController; import cn.hutool.core.text.StrBuilder; import cn.hutool.core.util.StrUtil; +import cn.hutool.setting.dialect.Props; import lombok.extern.slf4j.Slf4j; import org.springframework.util.ResourceUtils; @@ -30,8 +32,8 @@ public class ThemeUtils { public static List getThemes() { final List themes = new ArrayList<>(); try { - themes.addAll(getThemesByPath(getInternalThemesPath(), true)); - themes.addAll(getThemesByPath(getUsersThemesPath(), false)); + themes.addAll(getThemesByPath(getInternalThemesBasePath(), true)); + themes.addAll(getThemesByPath(getUsersThemesBasePath(), false)); } catch (Exception e) { throw new RuntimeException("Themes scan failed", e); } @@ -41,14 +43,14 @@ public class ThemeUtils { /** * Scan themes by directory * - * @param themesPath themes Path + * @param themesPath themes base Path * @param isInternal isInternal * @return themes */ - private static List getThemesByPath(File themesPath, boolean isInternal) { + private static List getThemesByPath(File themesBasePath, boolean isInternal) { final List themes = new ArrayList<>(); try { - final File[] files = themesPath.listFiles(); + final File[] files = themesBasePath.listFiles(); if (null != files) { Theme theme; for (File file : files) { @@ -57,20 +59,21 @@ public class ThemeUtils { continue; } theme = new Theme(); - theme.setThemeName(file.getName()); - File optionsPath = new File(themesPath.getAbsolutePath(), + theme.setThemeDir(file.getName()); + File optionsPath = new File(themesBasePath.getAbsolutePath(), file.getName() + "/module/options.ftl"); if (optionsPath.exists()) { theme.setHasOptions(true); } else { theme.setHasOptions(false); } - File gitPath = new File(themesPath.getAbsolutePath(), file.getName() + "/.git"); + File gitPath = new File(themesBasePath.getAbsolutePath(), file.getName() + "/.git"); if (gitPath.exists()) { theme.setHasUpdate(true); } else { theme.setHasUpdate(false); } + theme.setProperties(getProperties(new File(themesBasePath,file.getName()))); theme.setInternal(isInternal); themes.add(theme); } @@ -83,21 +86,21 @@ public class ThemeUtils { } /** - * Get internal themes path + * Get internal themes base path * * @return File * @throws FileNotFoundException FileNotFoundException */ - public static File getInternalThemesPath() throws FileNotFoundException { + public static File getInternalThemesBasePath() throws FileNotFoundException { return new File(ResourceUtils.getURL("classpath:").getPath(), "templates/themes"); } /** - * Get user's themes path + * Get user's themes base path * * @return File */ - public static File getUsersThemesPath() { + public static File getUsersThemesBasePath() { return new File(System.getProperties().getProperty("user.home"), "halo/templates/themes"); } @@ -107,8 +110,8 @@ public class ThemeUtils { * @param themeName themeName * @return File */ - public static File getThemesPath(String themeName) throws FileNotFoundException { - return isInternal(themeName) ? getInternalThemesPath() : getUsersThemesPath(); + public static File getThemesBasePath(String themeName) throws FileNotFoundException { + return isInternal(themeName) ? getInternalThemesBasePath() : getUsersThemesBasePath(); } /** @@ -120,7 +123,7 @@ public class ThemeUtils { public static List getTemplates(String theme) { final List templates = new ArrayList<>(); try { - final File themesPath = new File(getThemesPath(theme), theme); + final File themesPath = new File(getThemesBasePath(theme), theme); final File modulePath = new File(themesPath.getAbsolutePath(), "module"); final File[] baseFiles = themesPath.listFiles(); final File[] moduleFiles = modulePath.listFiles(); @@ -152,7 +155,7 @@ public class ThemeUtils { */ public static List getCustomTpl(String theme) throws FileNotFoundException { final List templates = new ArrayList<>(); - final File themePath = new File(getThemesPath(theme), theme); + final File themePath = new File(getThemesBasePath(theme), theme); final File[] themeFiles = themePath.listFiles(); if (null != themeFiles && themeFiles.length > 0) { for (File file : themeFiles) { @@ -165,7 +168,6 @@ public class ThemeUtils { return templates; } - /** * Judging whether template exists under the specified theme * @@ -177,7 +179,7 @@ public class ThemeUtils { StrBuilder templatePath = new StrBuilder(BaseContentController.THEME); templatePath.append("/"); templatePath.append(template); - File file = new File(getThemesPath(BaseContentController.THEME), templatePath.toString()); + File file = new File(getThemesBasePath(BaseContentController.THEME), templatePath.toString()); if (file.exists()) { result = true; } @@ -193,7 +195,7 @@ public class ThemeUtils { */ public static boolean isThemeExist(String theme) throws FileNotFoundException { boolean result = false; - File file = new File(getThemesPath(theme), theme); + File file = new File(getThemesBasePath(theme), theme); if (file.exists()) { result = true; } @@ -210,11 +212,34 @@ public class ThemeUtils { boolean result = false; List themes = HaloConst.THEMES; for (Theme theme : themes) { - if (theme.getThemeName().equals(themeName) && theme.isInternal()) { + if (theme.getThemeDir().equals(themeName) && theme.isInternal()) { result = true; break; } } return result; } + + /** + * Get theme Properties. + * + * @param path path + * @return ThemeProperties + */ + public static ThemeProperties getProperties(File path) { + File propertiesFile = new File(path, "theme.properties"); + ThemeProperties properties = new ThemeProperties(); + if (propertiesFile.exists()) { + Props props = new Props(propertiesFile); + properties.setId(props.getStr("theme.id")); + properties.setName(props.getStr("theme.name")); + properties.setWebsite(props.getStr("theme.website")); + properties.setDescription(props.getStr("theme.description")); + properties.setLogo(props.getStr("theme.logo")); + properties.setVersion(props.getStr("theme.version")); + properties.setAuthor(props.getStr("theme.author")); + properties.setAuthorWebsite(props.getStr("theme.author.website")); + } + return properties; + } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/content/ContentFeedController.java b/src/main/java/cc/ryanc/halo/web/controller/content/ContentFeedController.java index e6a28df9c..4baf192f8 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/content/ContentFeedController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/content/ContentFeedController.java @@ -75,7 +75,7 @@ public class ContentFeedController { @GetMapping(value = {"atom", "atom.xml"}, produces = "application/xml;charset=UTF-8") @ResponseBody public String atom(Model model) throws IOException, TemplateException { - int pageSize = HaloUtils.getDefaultPageSize(); + int pageSize = HaloUtils.getDefaultPageSize(10); final Sort sort = new Sort(Sort.Direction.DESC, "createTime"); final Pageable pageable = PageRequest.of(0, pageSize, sort); model.addAttribute("posts", buildPosts(pageable)); diff --git a/src/main/java/cc/ryanc/halo/web/controller/content/ContentIndexController.java b/src/main/java/cc/ryanc/halo/web/controller/content/ContentIndexController.java index 0e1314b85..9105d724f 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/content/ContentIndexController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/content/ContentIndexController.java @@ -65,7 +65,7 @@ public class ContentIndexController extends BaseContentController { @SortDefault(sort = "createTime", direction = DESC) }) Sort sort) { log.debug("Requested index page, sort info: [{}]", sort); - int pageSize = HaloUtils.getDefaultPageSize(); + int pageSize = HaloUtils.getDefaultPageSize(10); Pageable pageable = PageRequest.of(page - 1, pageSize, sort); Page posts = postService.pageListVoBy(PostStatus.PUBLISHED, PostType.POST, pageable); int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3); diff --git a/src/main/resources/templates/themes/anatole/theme.properties b/src/main/resources/templates/themes/anatole/theme.properties new file mode 100644 index 000000000..e417df194 --- /dev/null +++ b/src/main/resources/templates/themes/anatole/theme.properties @@ -0,0 +1,9 @@ +theme.id=anatole +theme.name=Anatole +theme.website=https://github.com/hi-caicai/farbox-theme-Anatole +theme.description=A other farbox theme +theme.logo=https://ryanc.cc/anatole/source/images/logo@2x.png +theme.version=1.0 +theme.author=Caicai +theme.author.website=https://www.caicai.me/ + diff --git a/src/main/resources/templates/themes/material/theme.properties b/src/main/resources/templates/themes/material/theme.properties new file mode 100644 index 000000000..e69de29bb