diff --git a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java index b4ec84666..b317d36ca 100644 --- a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java +++ b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java @@ -1,8 +1,19 @@ 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.service.OptionService; +import cc.ryanc.halo.service.UserService; +import freemarker.template.TemplateModelException; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import javax.annotation.PostConstruct; + /** *
  *     FreeMarker配置
@@ -15,43 +26,43 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class FreeMarkerAutoConfiguration {
 
-//    @Autowired
-//    private freemarker.template.Configuration configuration;
-//
-//    @Autowired
-//    private OptionsService optionsService;
-//
-//    @Autowired
-//    private UserService userService;
-//
-//    @Autowired
-//    private CommonTagDirective commonTagDirective;
-//
-//    @Autowired
-//    private ArticleTagDirective articleTagDirective;
-//
-//    @Autowired
-//    private RandomMethod randomMethod;
-//
-//    @Autowired
-//    private RecentPostsMethod recentPostsMethod;
-//
-//    @Autowired
-//    private RecentCommentsMethod recentCommentsMethod;
-//
-//    @PostConstruct
-//    public void setSharedVariable() {
-//        try {
-//            //自定义标签
-//            configuration.setSharedVariable("commonTag", commonTagDirective);
-//            configuration.setSharedVariable("articleTag", articleTagDirective);
-//            configuration.setSharedVariable("options", optionsService.findAllOptions());
+    @Autowired
+    private freemarker.template.Configuration configuration;
+
+    @Autowired
+    private OptionService optionsService;
+
+    @Autowired
+    private UserService userService;
+
+    @Autowired
+    private CommonTagDirective commonTagDirective;
+
+    @Autowired
+    private ArticleTagDirective articleTagDirective;
+
+    @Autowired
+    private RandomMethod randomMethod;
+
+    @Autowired
+    private RecentPostsMethod recentPostsMethod;
+
+    @Autowired
+    private RecentCommentsMethod recentCommentsMethod;
+
+    @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("randomMethod", randomMethod);
-//            configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
-//            configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);
-//        } catch (TemplateModelException e) {
-//            log.error("Custom tags failed to load:{}", e.getMessage());
-//        }
-//    }
+            configuration.setSharedVariable("randomMethod", randomMethod);
+            configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
+            configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);
+        } catch (TemplateModelException e) {
+            log.error("Custom tags failed to load:{}", e.getMessage());
+        }
+    }
 }
diff --git a/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java b/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java
index b1bed8ecd..d4afe9e99 100644
--- a/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java
+++ b/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java
@@ -54,7 +54,8 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
         registry.addResourceHandler("/static/**")
                 .addResourceLocations("classpath:/static/");
         registry.addResourceHandler("/**")
-                .addResourceLocations("classpath:/templates/themes/");
+                .addResourceLocations("classpath:/templates/themes/")
+                .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/templates/themes/");
         registry.addResourceHandler("/upload/**")
                 .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/upload/");
         registry.addResourceHandler("/favicon.ico")
diff --git a/src/main/java/cc/ryanc/halo/listener/StartedListener.java b/src/main/java/cc/ryanc/halo/listener/StartedListener.java
index 31317e20e..d2d0afe62 100644
--- a/src/main/java/cc/ryanc/halo/listener/StartedListener.java
+++ b/src/main/java/cc/ryanc/halo/listener/StartedListener.java
@@ -1,14 +1,27 @@
 package cc.ryanc.halo.listener;
 
 import cc.ryanc.halo.config.properties.HaloProperties;
+import cc.ryanc.halo.model.support.HaloConst;
+import cc.ryanc.halo.model.support.Theme;
+import cc.ryanc.halo.service.OptionService;
 import cc.ryanc.halo.utils.HaloUtils;
+import cc.ryanc.halo.utils.ThemeUtils;
+import cc.ryanc.halo.web.controller.content.base.BaseContentController;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONUtil;
+import freemarker.template.TemplateModelException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.event.ApplicationStartedEvent;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationListener;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.util.ResourceUtils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.nio.charset.Charset;
+import java.util.List;
 
 /**
  * 
@@ -31,18 +44,54 @@ public class StartedListener implements ApplicationListener themes = ThemeUtils.getThemes();
+        if (null != themes) {
+            HaloConst.THEMES = themes;
+        }
+    }
+
+    /**
+     * Get active theme
+     */
+    private void getActiveTheme() {
+        final String themeValue = optionService.getByKey("theme");
+        if (StrUtil.isNotEmpty(themeValue) && !StrUtil.equals(themeValue, null)) {
+            BaseContentController.THEME = themeValue;
+        } else {
+            BaseContentController.THEME = "anatole";
+        }
+        try {
+            configuration.setSharedVariable("themeName", BaseContentController.THEME);
+        } catch (TemplateModelException e) {
+            e.printStackTrace();
+        }
+    }
+
     private void printStartInfo() {
         // Get server port
         String serverPort = applicationContext.getEnvironment().getProperty("server.port");
@@ -75,4 +124,16 @@ public class StartedListener implements ApplicationListener OPTIONS = new HashMap<>();
 
     /**
-     * OwO表情
+     * All of the Owo
      */
-    public static Map OWO = new HashMap<>();
+    public static JSONObject OWO;
 
     /**
-     * 所有主题
+     * All of the themes
      */
     public static List THEMES = new ArrayList<>();
 
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 782be3561..508430b30 100644
--- a/src/main/java/cc/ryanc/halo/model/support/Theme.java
+++ b/src/main/java/cc/ryanc/halo/model/support/Theme.java
@@ -5,9 +5,7 @@ import lombok.Data;
 import java.io.Serializable;
 
 /**
- * 
- *     主题信息
- * 
+ * Theme DTO * * @author : RYAN0UP * @date : 2018/1/3 @@ -18,17 +16,22 @@ public class Theme implements Serializable { private static final long serialVersionUID = 1L; /** - * 主题名称 + * theme name */ private String themeName; /** - * 是否支持设置 + * is support setting options */ private boolean hasOptions; /** - * 是否支持更新 + * is support update */ private boolean hasUpdate; + + /** + * is internal theme + */ + private boolean isInternal; } diff --git a/src/main/java/cc/ryanc/halo/repository/OptionRepository.java b/src/main/java/cc/ryanc/halo/repository/OptionRepository.java index 609e70c37..6b6401a5c 100755 --- a/src/main/java/cc/ryanc/halo/repository/OptionRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/OptionRepository.java @@ -3,6 +3,8 @@ package cc.ryanc.halo.repository; import cc.ryanc.halo.model.entity.Option; import cc.ryanc.halo.repository.base.BaseRepository; +import java.util.Optional; + /** * Option repository. * @@ -10,4 +12,18 @@ import cc.ryanc.halo.repository.base.BaseRepository; */ public interface OptionRepository extends BaseRepository { + /** + * Query option by key + * + * @param key key + * @return Option + */ + Optional