Skip to content

Commit

Permalink
#12 add theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Nov 13, 2023
1 parent c2f5bcf commit 53665a0
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 deletions.
2 changes: 1 addition & 1 deletion modules/pebble-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms.modules</groupId>
<artifactId>cms-modules</artifactId>
<version>2.7.0</version>
<version>2.7.0-SNAPSHOT</version>
</parent>
<artifactId>pebble-module</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/*-
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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);
Expand All @@ -83,6 +78,22 @@ public PebbleTemplateEngine(final ModuleFileSystem fileSystem, final ServerPrope
engine = builder
.build();
}

private Loader<?> createLoader (final ModuleFileSystem fileSystem, final Theme theme) {
List<Loader<?>> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 53665a0

Please sign in to comment.