From 53665a006c25700c4214ce95c19114bd540d8e6b Mon Sep 17 00:00:00 2001 From: Thorsten Marx Date: Mon, 13 Nov 2023 10:10:12 +0100 Subject: [PATCH] #12 add theme support --- modules/pebble-module/pom.xml | 2 +- .../pebble/PebbleLifecycleExtension.java | 6 +- .../modules/pebble/PebbleTemplateEngine.java | 37 ++++++---- .../pebble/ThemeTemplateLoaderTest.java | 70 +++++++++++++++++++ 4 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 modules/pebble-module/src/test/java/com/github/thmarx/cms/modules/pebble/ThemeTemplateLoaderTest.java diff --git a/modules/pebble-module/pom.xml b/modules/pebble-module/pom.xml index 6f02c020..2b78c1e9 100644 --- a/modules/pebble-module/pom.xml +++ b/modules/pebble-module/pom.xml @@ -4,7 +4,7 @@ com.github.thmarx.cms.modules cms-modules - 2.7.0 + 2.7.0-SNAPSHOT pebble-module jar diff --git a/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleLifecycleExtension.java b/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleLifecycleExtension.java index 4e1f4cb2..a650756a 100644 --- a/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleLifecycleExtension.java +++ b/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleLifecycleExtension.java @@ -1,7 +1,3 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ package com.github.thmarx.cms.modules.pebble; /*- @@ -43,7 +39,7 @@ public void init() { @Override public void activate() { - templateEngine = new PebbleTemplateEngine(getContext().getFileSystem(), getContext().getServerProperties()); + templateEngine = new PebbleTemplateEngine(getContext().getFileSystem(), getContext().getServerProperties(), getContext().getTheme()); } @Override diff --git a/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleTemplateEngine.java b/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleTemplateEngine.java index 03766255..c4d48270 100644 --- a/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleTemplateEngine.java +++ b/modules/pebble-module/src/main/java/com/github/thmarx/cms/modules/pebble/PebbleTemplateEngine.java @@ -24,17 +24,21 @@ import com.github.thmarx.cms.api.ModuleFileSystem; import com.github.thmarx.cms.api.ServerProperties; import com.github.thmarx.cms.api.template.TemplateEngine; +import com.github.thmarx.cms.api.theme.Theme; import io.pebbletemplates.pebble.PebbleEngine; import io.pebbletemplates.pebble.cache.tag.CaffeineTagCache; import io.pebbletemplates.pebble.cache.template.CaffeineTemplateCache; +import io.pebbletemplates.pebble.loader.DelegatingLoader; import io.pebbletemplates.pebble.loader.FileLoader; +import io.pebbletemplates.pebble.loader.Loader; import io.pebbletemplates.pebble.template.PebbleTemplate; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; -import java.nio.file.Path; import java.time.Duration; +import java.util.ArrayList; +import java.util.List; /** * @@ -43,21 +47,12 @@ public class PebbleTemplateEngine implements TemplateEngine { private final PebbleEngine engine; - private final Path templateBase; - final Path contentBase; - - final ModuleFileSystem fileSystem; - public PebbleTemplateEngine(final ModuleFileSystem fileSystem, final ServerProperties properties) { - this.fileSystem = fileSystem; - this.templateBase = fileSystem.resolve("templates/"); - this.contentBase = fileSystem.resolve("content/"); + + public PebbleTemplateEngine(final ModuleFileSystem fileSystem, final ServerProperties properties, final Theme theme) { - var loader = new FileLoader(); - loader.setPrefix(this.templateBase.toString() + File.separatorChar); - //loader.setSuffix(".html"); final PebbleEngine.Builder builder = new PebbleEngine.Builder() - .loader(loader); + .loader(createLoader(fileSystem, theme)); if (properties.dev()) { builder.templateCache(null); @@ -83,6 +78,22 @@ public PebbleTemplateEngine(final ModuleFileSystem fileSystem, final ServerPrope engine = builder .build(); } + + private Loader createLoader (final ModuleFileSystem fileSystem, final Theme theme) { + List> loaders = new ArrayList<>(); + + var siteLoader = new FileLoader(); + siteLoader.setPrefix(fileSystem.resolve("templates/").toString() + File.separatorChar); + loaders.add(siteLoader); + + if (!theme.empty()) { + var themeLoader = new FileLoader(); + themeLoader.setPrefix(theme.templatesPath().toString() + File.separatorChar); + loaders.add(themeLoader); + } + + return new DelegatingLoader(loaders); + } @Override public String render(String template, Model model) throws IOException { diff --git a/modules/pebble-module/src/test/java/com/github/thmarx/cms/modules/pebble/ThemeTemplateLoaderTest.java b/modules/pebble-module/src/test/java/com/github/thmarx/cms/modules/pebble/ThemeTemplateLoaderTest.java new file mode 100644 index 00000000..9ad9ee9d --- /dev/null +++ b/modules/pebble-module/src/test/java/com/github/thmarx/cms/modules/pebble/ThemeTemplateLoaderTest.java @@ -0,0 +1,70 @@ +package com.github.thmarx.cms.modules.pebble; + +/*- + * #%L + * pebble-module + * %% + * 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 io.pebbletemplates.pebble.loader.DelegatingLoader; +import io.pebbletemplates.pebble.loader.Loader; +import java.io.IOException; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +/** + * + * @author t.marx + */ +@ExtendWith(MockitoExtension.class) +public class ThemeTemplateLoaderTest { + + @Mock + public Loader siteTemplateLoader; + @Mock + public Loader themeTemplateLoader; + + public Loader sut; + + @BeforeEach + void setup () { + sut = new DelegatingLoader(List.of(siteTemplateLoader, themeTemplateLoader)); + } + + @Test + public void site_template() throws IOException { + Mockito.when(siteTemplateLoader.resourceExists("test")).thenReturn(Boolean.TRUE); + + sut.resourceExists("test"); + + Mockito.verify(themeTemplateLoader, Mockito.times(0)).resourceExists("test"); + } + + @Test + public void theme_template() throws IOException { + Mockito.when(siteTemplateLoader.resourceExists("test")).thenReturn(Boolean.FALSE); + + sut.resourceExists("test"); + + Mockito.verify(themeTemplateLoader, Mockito.times(1)).resourceExists("test"); + } +}