From 7a29c86547f3779c9f4e17407a8911ba2abf9212 Mon Sep 17 00:00:00 2001 From: Thorsten Marx Date: Sun, 12 Nov 2023 16:24:52 +0100 Subject: [PATCH] #12 add simple template theme support --- cms-api/pom.xml | 2 +- .../thmarx/cms/api/CMSModuleContext.java | 4 +- .../com/github/thmarx/cms/api/Constants.java | 7 ++ .../thmarx/cms/api/ServerProperties.java | 7 +- .../github/thmarx/cms/api/SiteProperties.java | 7 +- .../github/thmarx/cms/api/YamlProperties.java | 20 +++++ .../github/thmarx/cms/api/theme/Theme.java | 42 ++++++++++ .../hosts/demo/{site.yaml => site_no.yaml} | 0 .../theme-demo/content/.technical/404.md | 5 ++ .../content/.technical/test/example.md | 5 ++ cms-server/hosts/theme-demo/content/index.md | 8 ++ .../example-module/configuration.properties | 0 .../markedjs-module/configuration.properties | 0 .../modules_data/moduleconfiguration.json | 1 + .../thymeleaf-module/configuration.properties | 0 cms-server/hosts/theme-demo/site.yaml | 23 ++++++ cms-server/pom.xml | 2 +- .../com/github/thmarx/cms/server/VHost.java | 32 +++++++- .../github/thmarx/cms/theme/DefaultTheme.java | 78 +++++++++++++++++++ .../com/github/thmarx/cms/theme/Theme.java | 33 -------- modules/example-module/pom.xml | 2 +- modules/flexmark-module/pom.xml | 2 +- modules/freemarker-module/pom.xml | 2 +- modules/markedjs-module/pom.xml | 2 +- modules/pebble-module/pom.xml | 2 +- modules/pom.xml | 2 +- modules/pug-module/pom.xml | 2 +- modules/search-module/pom.xml | 2 +- modules/thymeleaf-module/pom.xml | 2 +- .../ThymeleafLifecycleExtension.java | 9 ++- .../thymeleaf/ThymeleafTemplateEngine.java | 28 +++++-- pom.xml | 2 +- 32 files changed, 271 insertions(+), 62 deletions(-) create mode 100644 cms-api/src/main/java/com/github/thmarx/cms/api/theme/Theme.java rename cms-server/hosts/demo/{site.yaml => site_no.yaml} (100%) create mode 100644 cms-server/hosts/theme-demo/content/.technical/404.md create mode 100644 cms-server/hosts/theme-demo/content/.technical/test/example.md create mode 100644 cms-server/hosts/theme-demo/content/index.md create mode 100644 cms-server/hosts/theme-demo/modules_data/example-module/configuration.properties create mode 100644 cms-server/hosts/theme-demo/modules_data/markedjs-module/configuration.properties create mode 100644 cms-server/hosts/theme-demo/modules_data/moduleconfiguration.json create mode 100644 cms-server/hosts/theme-demo/modules_data/thymeleaf-module/configuration.properties create mode 100644 cms-server/hosts/theme-demo/site.yaml create mode 100644 cms-server/src/main/java/com/github/thmarx/cms/theme/DefaultTheme.java delete mode 100644 cms-server/src/main/java/com/github/thmarx/cms/theme/Theme.java diff --git a/cms-api/pom.xml b/cms-api/pom.xml index ef38830e..e6f6cbe3 100644 --- a/cms-api/pom.xml +++ b/cms-api/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms cms-parent - v2.7.0-SNAPSHOT + 2.7.0 cms-api jar diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/CMSModuleContext.java b/cms-api/src/main/java/com/github/thmarx/cms/api/CMSModuleContext.java index 891a4622..55028b33 100644 --- a/cms-api/src/main/java/com/github/thmarx/cms/api/CMSModuleContext.java +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/CMSModuleContext.java @@ -21,12 +21,12 @@ */ import com.github.thmarx.cms.api.eventbus.EventBus; +import com.github.thmarx.cms.api.theme.Theme; import com.github.thmarx.modules.api.Context; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.BiFunction; -import java.util.function.Function; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -46,4 +46,6 @@ public class CMSModuleContext implements Context { private final EventBus eventBus; @Getter private final BiFunction>, Optional> renderContentFunction; + @Getter + private final Theme theme; } diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/Constants.java b/cms-api/src/main/java/com/github/thmarx/cms/api/Constants.java index 05795689..68d423cb 100644 --- a/cms-api/src/main/java/com/github/thmarx/cms/api/Constants.java +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/Constants.java @@ -39,6 +39,13 @@ public static class MetaFields { public static final String MENU_TITLE = "title"; } + public static class Folders { + public static final String CONTENT = "content/"; + public static final String TEMPALTES = "templates/"; + public static final String ASSETS = "assets/"; + public static final String EXTENSIONS = "extensions/"; + } + public static final String SPLIT_PATH_PATTERN = Pattern.quote("/"); public static final Pattern SECTION_PATTERN = Pattern.compile("\\w+[a-zA-Z0-9-]*\\.(?
[a-zA-Z0-9]+[a-zA-Z0-9-]*)\\.md"); diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/ServerProperties.java b/cms-api/src/main/java/com/github/thmarx/cms/api/ServerProperties.java index 3aba1fe0..61b286d3 100644 --- a/cms-api/src/main/java/com/github/thmarx/cms/api/ServerProperties.java +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/ServerProperties.java @@ -20,9 +20,8 @@ * #L% */ -import java.util.Collections; +import java.nio.file.Path; import java.util.Map; -import lombok.RequiredArgsConstructor; /** * @@ -47,4 +46,8 @@ public String serverIp () { public int serverPort () { return (int)getSubMap("server").getOrDefault("port", 8080); } + + public Path getThemesFolder () { + return Path.of("themes/"); + } } diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/SiteProperties.java b/cms-api/src/main/java/com/github/thmarx/cms/api/SiteProperties.java index 11994538..ceca164c 100644 --- a/cms-api/src/main/java/com/github/thmarx/cms/api/SiteProperties.java +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/SiteProperties.java @@ -20,10 +20,7 @@ * #L% */ -import java.util.Collections; -import java.util.List; import java.util.Map; -import lombok.RequiredArgsConstructor; /** * @@ -42,4 +39,8 @@ public String hostname () { public String markdownEngine () { return (String)getSubMap("markdown").getOrDefault("engine", "flexmark"); } + + public String theme () { + return (String) properties.get("theme"); + } } diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/YamlProperties.java b/cms-api/src/main/java/com/github/thmarx/cms/api/YamlProperties.java index dbd66ebc..bfa36518 100644 --- a/cms-api/src/main/java/com/github/thmarx/cms/api/YamlProperties.java +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/YamlProperties.java @@ -1,5 +1,25 @@ package com.github.thmarx.cms.api; +/*- + * #%L + * cms-api + * %% + * Copyright (C) 2023 Marx-Software + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + import java.util.Collections; import java.util.Map; diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/theme/Theme.java b/cms-api/src/main/java/com/github/thmarx/cms/api/theme/Theme.java new file mode 100644 index 00000000..4fc0b360 --- /dev/null +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/theme/Theme.java @@ -0,0 +1,42 @@ +package com.github.thmarx.cms.api.theme; + +/*- + * #%L + * cms-api + * %% + * Copyright (C) 2023 Marx-Software + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.github.thmarx.cms.api.ThemeProperties; +import java.nio.file.Path; + +/** + * + * @author thmar + */ + +public interface Theme { + + Path templatePath(); + + ThemeProperties properties(); + + /** + * empty theme is used for sites without configured theme + * @return + */ + boolean empty(); +} diff --git a/cms-server/hosts/demo/site.yaml b/cms-server/hosts/demo/site_no.yaml similarity index 100% rename from cms-server/hosts/demo/site.yaml rename to cms-server/hosts/demo/site_no.yaml diff --git a/cms-server/hosts/theme-demo/content/.technical/404.md b/cms-server/hosts/theme-demo/content/.technical/404.md new file mode 100644 index 00000000..776ccf04 --- /dev/null +++ b/cms-server/hosts/theme-demo/content/.technical/404.md @@ -0,0 +1,5 @@ +--- +title: Leider nichts gefunden +template: error.html +--- +Da haben wir leider nichts gefunden! diff --git a/cms-server/hosts/theme-demo/content/.technical/test/example.md b/cms-server/hosts/theme-demo/content/.technical/test/example.md new file mode 100644 index 00000000..b06f805a --- /dev/null +++ b/cms-server/hosts/theme-demo/content/.technical/test/example.md @@ -0,0 +1,5 @@ +--- +title: Das ist der neue Titel +--- + +Das ist der ganz aktuelle Inhalt \ No newline at end of file diff --git a/cms-server/hosts/theme-demo/content/index.md b/cms-server/hosts/theme-demo/content/index.md new file mode 100644 index 00000000..f8800c91 --- /dev/null +++ b/cms-server/hosts/theme-demo/content/index.md @@ -0,0 +1,8 @@ +--- +title: Startseite +template: start.html +--- + +# Demo Project + +![TestBild!](/media/images/test.jpg?format=small "Test bild") \ No newline at end of file diff --git a/cms-server/hosts/theme-demo/modules_data/example-module/configuration.properties b/cms-server/hosts/theme-demo/modules_data/example-module/configuration.properties new file mode 100644 index 00000000..e69de29b diff --git a/cms-server/hosts/theme-demo/modules_data/markedjs-module/configuration.properties b/cms-server/hosts/theme-demo/modules_data/markedjs-module/configuration.properties new file mode 100644 index 00000000..e69de29b diff --git a/cms-server/hosts/theme-demo/modules_data/moduleconfiguration.json b/cms-server/hosts/theme-demo/modules_data/moduleconfiguration.json new file mode 100644 index 00000000..1e076bde --- /dev/null +++ b/cms-server/hosts/theme-demo/modules_data/moduleconfiguration.json @@ -0,0 +1 @@ +{"modules":{"flexmark-module":{"active":false,"id":"flexmark-module","moduleDir":"flexmark-module"},"thymeleaf-module":{"active":true,"id":"thymeleaf-module","moduleDir":"thymeleaf-module"},"freemarker-module":{"active":false,"id":"freemarker-module","moduleDir":"freemarker-module"},"ui-module":{"active":false,"id":"ui-module","moduleDir":"ui-module"},"pebble-module":{"active":false,"id":"pebble-module","moduleDir":"pebble-module"},"example-module":{"active":true,"id":"example-module","moduleDir":"example-module"},"markedjs-module":{"active":true,"id":"markedjs-module","moduleDir":"markedjs-module"}}} \ No newline at end of file diff --git a/cms-server/hosts/theme-demo/modules_data/thymeleaf-module/configuration.properties b/cms-server/hosts/theme-demo/modules_data/thymeleaf-module/configuration.properties new file mode 100644 index 00000000..e69de29b diff --git a/cms-server/hosts/theme-demo/site.yaml b/cms-server/hosts/theme-demo/site.yaml new file mode 100644 index 00000000..765c32f0 --- /dev/null +++ b/cms-server/hosts/theme-demo/site.yaml @@ -0,0 +1,23 @@ +hostname: localhost2 +baseurl: "http://localhost2:1010" +language: en +template: + engine: thymeleaf +markdown: + engine: markedjs +theme: default +modules: + active: + - markedjs-module +media: + formats: + - name: small + width: 256 + height: 256 + format: webp + compression: true + - name: big + width: 512 + height: 512 + format: webp + compression: true \ No newline at end of file diff --git a/cms-server/pom.xml b/cms-server/pom.xml index 0a852782..67bf0cba 100644 --- a/cms-server/pom.xml +++ b/cms-server/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms cms-parent - v2.7.0-SNAPSHOT + 2.7.0 cms-server jar diff --git a/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java b/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java index c0b98f02..bf36f730 100644 --- a/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java +++ b/cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java @@ -37,12 +37,16 @@ import com.github.thmarx.cms.extensions.ExtensionManager; import com.github.thmarx.cms.api.markdown.MarkdownRenderer; import com.github.thmarx.cms.api.template.TemplateEngine; +import com.github.thmarx.cms.api.theme.Theme; import com.github.thmarx.cms.module.RenderContentFunction; +import com.github.thmarx.cms.theme.DefaultTheme; import com.github.thmarx.modules.api.ModuleManager; import com.github.thmarx.modules.manager.ModuleAPIClassLoader; import com.github.thmarx.modules.manager.ModuleManagerImpl; import java.io.IOException; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Optional; import lombok.Getter; @@ -70,6 +74,8 @@ public class VHost { @Getter private String hostname; + private Theme theme; + @Getter private final EventBus eventBus; @@ -94,6 +100,16 @@ public void shutdown() { } } + private Theme loadTheme() throws IOException { + + if (siteProperties.theme() != null) { + Path themeFolder = serverProperties.getThemesFolder().resolve(siteProperties.theme()); + return DefaultTheme.load(themeFolder); + } + + return DefaultTheme.EMPTY; + } + public void init(Path modules) throws IOException { fileSystem.init(); @@ -101,6 +117,8 @@ public void init(Path modules) throws IOException { var props = fileSystem.resolve("site.yaml"); siteProperties = PropertiesLoader.hostProperties(props); + theme = loadTheme(); + var classLoader = new ModuleAPIClassLoader(ClassLoader.getSystemClassLoader(), List.of( "org.slf4j", @@ -115,7 +133,8 @@ public void init(Path modules) throws IOException { this.moduleManager = ModuleManagerImpl.create(modules.toFile(), fileSystem.resolve("modules_data").toFile(), new CMSModuleContext(siteProperties, serverProperties, fileSystem, eventBus, - new RenderContentFunction(() -> contentResolver, () -> extensionManager, (context) -> resolveMarkdownRenderer()) + new RenderContentFunction(() -> contentResolver, () -> extensionManager, (context) -> resolveMarkdownRenderer()), + theme ), classLoader ); @@ -135,7 +154,14 @@ public void init(Path modules) throws IOException { contentResolver = new ContentResolver(contentBase, contentRenderer, fileSystem); this.moduleManager.initModules(); - siteProperties.activeModules().stream() + + List activeModules = new ArrayList<>(); + activeModules.addAll(siteProperties.activeModules()); + if (!theme.empty()) { + activeModules.addAll(theme.properties().activeModules()); + } + + activeModules.stream() .filter(module_id -> moduleManager.getModuleIds().contains(module_id)) .forEach(module_id -> { try { @@ -147,7 +173,7 @@ public void init(Path modules) throws IOException { }); moduleManager.getModuleIds().stream() - .filter(id -> !siteProperties.activeModules().contains(id)) + .filter(id -> !activeModules.contains(id)) .forEach((module_id) -> { try { log.debug("deactivate module {}", module_id); diff --git a/cms-server/src/main/java/com/github/thmarx/cms/theme/DefaultTheme.java b/cms-server/src/main/java/com/github/thmarx/cms/theme/DefaultTheme.java new file mode 100644 index 00000000..e80d8d98 --- /dev/null +++ b/cms-server/src/main/java/com/github/thmarx/cms/theme/DefaultTheme.java @@ -0,0 +1,78 @@ +package com.github.thmarx.cms.theme; + +/*- + * #%L + * cms-server + * %% + * Copyright (C) 2023 Marx-Software + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.github.thmarx.cms.api.ThemeProperties; +import com.github.thmarx.cms.api.theme.Theme; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.yaml.snakeyaml.Yaml; + +/** + * + * @author thmar + */ +@Slf4j +@RequiredArgsConstructor +public class DefaultTheme implements Theme { + + public static final Theme EMPTY = new DefaultTheme(null, null, true); + + private final Path themePath; + private final ThemeProperties properties; + private boolean empty = false; + + private DefaultTheme (final Path templatePath, final ThemeProperties themeProperties, final boolean empty) { + this(templatePath, themeProperties); + this.empty = empty; + } + + public static Theme load (Path themePath) throws IOException { + Yaml yaml = new Yaml(); + Path themeYaml = themePath.resolve("theme.yaml"); + + var content = Files.readString(themeYaml, StandardCharsets.UTF_8); + Map config = (Map)yaml.load(content); + + return new DefaultTheme(themePath, new ThemeProperties(config)); + } + + @Override + public Path templatePath() { + return themePath.resolve("templates/"); + } + + @Override + public boolean empty() { + return empty; + } + + @Override + public ThemeProperties properties() { + return properties; + } +} diff --git a/cms-server/src/main/java/com/github/thmarx/cms/theme/Theme.java b/cms-server/src/main/java/com/github/thmarx/cms/theme/Theme.java deleted file mode 100644 index 77322957..00000000 --- a/cms-server/src/main/java/com/github/thmarx/cms/theme/Theme.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.github.thmarx.cms.theme; - -import com.github.thmarx.cms.api.ThemeProperties; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Map; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.yaml.snakeyaml.Yaml; - -/** - * - * @author thmar - */ -@Slf4j -@RequiredArgsConstructor -public class Theme { - private final Path themePath; - private final ThemeProperties properties; - - - public static Theme load (Path themePath) throws IOException { - Yaml yaml = new Yaml(); - Path themeYaml = themePath.resolve("theme.yaml"); - - var content = Files.readString(themeYaml, StandardCharsets.UTF_8); - Map config = (Map)yaml.load(content); - - return new Theme(themePath, new ThemeProperties(config)); - } -} diff --git a/modules/example-module/pom.xml b/modules/example-module/pom.xml index aa38350d..e52904cc 100644 --- a/modules/example-module/pom.xml +++ b/modules/example-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 example-module jar diff --git a/modules/flexmark-module/pom.xml b/modules/flexmark-module/pom.xml index efd77d6a..1681d04d 100644 --- a/modules/flexmark-module/pom.xml +++ b/modules/flexmark-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 flexmark-module jar diff --git a/modules/freemarker-module/pom.xml b/modules/freemarker-module/pom.xml index a868cc11..eb0f0fd7 100644 --- a/modules/freemarker-module/pom.xml +++ b/modules/freemarker-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 freemarker-module jar diff --git a/modules/markedjs-module/pom.xml b/modules/markedjs-module/pom.xml index 69814d5b..2b2a2068 100644 --- a/modules/markedjs-module/pom.xml +++ b/modules/markedjs-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 markedjs-module jar diff --git a/modules/pebble-module/pom.xml b/modules/pebble-module/pom.xml index 2b7336de..6f02c020 100644 --- a/modules/pebble-module/pom.xml +++ b/modules/pebble-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 pebble-module jar diff --git a/modules/pom.xml b/modules/pom.xml index f05c1cda..74b1ac4b 100644 --- a/modules/pom.xml +++ b/modules/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms cms-parent - v2.7.0-SNAPSHOT + 2.7.0 com.github.thmarx.cms.modules cms-modules diff --git a/modules/pug-module/pom.xml b/modules/pug-module/pom.xml index 80343550..7f6cafef 100644 --- a/modules/pug-module/pom.xml +++ b/modules/pug-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 pug-module jar diff --git a/modules/search-module/pom.xml b/modules/search-module/pom.xml index ed6b39d3..1c38f6ee 100644 --- a/modules/search-module/pom.xml +++ b/modules/search-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 search-module jar diff --git a/modules/thymeleaf-module/pom.xml b/modules/thymeleaf-module/pom.xml index 1592c9ac..bb67a8bf 100644 --- a/modules/thymeleaf-module/pom.xml +++ b/modules/thymeleaf-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - v2.7.0-SNAPSHOT + 2.7.0 thymeleaf-module jar diff --git a/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafLifecycleExtension.java b/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafLifecycleExtension.java index 325a5c4a..edb45450 100644 --- a/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafLifecycleExtension.java +++ b/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafLifecycleExtension.java @@ -19,7 +19,6 @@ * limitations under the License. * #L% */ - import com.github.thmarx.cms.api.CMSModuleContext; import com.github.thmarx.modules.api.ModuleLifeCycleExtension; import com.github.thmarx.modules.api.annotation.Extension; @@ -32,14 +31,18 @@ public class ThymeleafLifecycleExtension extends ModuleLifeCycleExtension { static ThymeleafTemplateEngine templateEngine; - + @Override public void init() { } @Override public void activate() { - templateEngine = new ThymeleafTemplateEngine(getContext().getFileSystem(), getContext().getServerProperties()); + templateEngine = new ThymeleafTemplateEngine( + getContext().getFileSystem(), + getContext().getServerProperties(), + getContext().getTheme() + ); } @Override diff --git a/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafTemplateEngine.java b/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafTemplateEngine.java index 5db043c9..fce855b2 100644 --- a/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafTemplateEngine.java +++ b/modules/thymeleaf-module/src/main/java/com/github/thmarx/cms/modules/thymeleaf/ThymeleafTemplateEngine.java @@ -22,17 +22,22 @@ import com.github.thmarx.cms.api.ModuleFileSystem; import com.github.thmarx.cms.api.ServerProperties; +import com.github.thmarx.cms.api.SiteProperties; +import com.github.thmarx.cms.api.ThemeProperties; import com.github.thmarx.cms.api.template.TemplateEngine; +import com.github.thmarx.cms.api.theme.Theme; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.nio.file.Path; import java.util.Locale; +import java.util.Set; import java.util.concurrent.TimeUnit; import org.thymeleaf.context.Context; import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.FileTemplateResolver; +import org.thymeleaf.templateresolver.ITemplateResolver; /** * @@ -46,14 +51,28 @@ public class ThymeleafTemplateEngine implements TemplateEngine { private final ModuleFileSystem fileSystem; private final ServerProperties serverProperties; - public ThymeleafTemplateEngine(final ModuleFileSystem fileSystem, final ServerProperties serverProperties) { + public ThymeleafTemplateEngine(final ModuleFileSystem fileSystem, + final ServerProperties serverProperties, + final Theme theme) { this.fileSystem = fileSystem; this.templateBase = fileSystem.resolve("templates/"); this.serverProperties = serverProperties; + + + engine = new org.thymeleaf.TemplateEngine(); + engine.setTemplateResolver(templateResolver(this.templateBase, 2)); + + if (!theme.empty()) { + engine.addTemplateResolver(templateResolver(theme.templatePath(), 1)); + } + } + + private ITemplateResolver templateResolver (final Path templatePath, final int order) { var templateResolver = new FileTemplateResolver(); + templateResolver.setOrder(order); templateResolver.setTemplateMode(TemplateMode.HTML); - templateResolver.setPrefix(this.templateBase.toString() + File.separatorChar); + templateResolver.setPrefix(templatePath.toString() + File.separatorChar); //templateResolver.setSuffix(".html"); if (serverProperties.dev()) { templateResolver.setCacheable(false); @@ -61,9 +80,8 @@ public ThymeleafTemplateEngine(final ModuleFileSystem fileSystem, final ServerPr templateResolver.setCacheable(true); templateResolver.setCacheTTLMs(TimeUnit.MINUTES.toMillis(1)); } - - engine = new org.thymeleaf.TemplateEngine(); - engine.setTemplateResolver(templateResolver); + + return templateResolver; } @Override diff --git a/pom.xml b/pom.xml index c3f90279..d6bbf4c7 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.github.thmarx.cms cms-parent - v2.7.0-SNAPSHOT + 2.7.0 pom UTF-8