Skip to content

Commit

Permalink
#12 add simple template theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
thmarx committed Nov 12, 2023
1 parent 82a51f9 commit 7a29c86
Show file tree
Hide file tree
Showing 32 changed files with 271 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cms-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>v2.7.0-SNAPSHOT</version>
<version>2.7.0</version>
</parent>
<artifactId>cms-api</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -46,4 +46,6 @@ public class CMSModuleContext implements Context {
private final EventBus eventBus;
@Getter
private final BiFunction<String, Map<String, List<String>>, Optional<String>> renderContentFunction;
@Getter
private final Theme theme;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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-]*\\.(?<section>[a-zA-Z0-9]+[a-zA-Z0-9-]*)\\.md");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
* #L%
*/

import java.util.Collections;
import java.nio.file.Path;
import java.util.Map;
import lombok.RequiredArgsConstructor;

/**
*
Expand All @@ -47,4 +46,8 @@ public String serverIp () {
public int serverPort () {
return (int)getSubMap("server").getOrDefault("port", 8080);
}

public Path getThemesFolder () {
return Path.of("themes/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
* #L%
*/

import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;

/**
*
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
42 changes: 42 additions & 0 deletions cms-api/src/main/java/com/github/thmarx/cms/api/theme/Theme.java
Original file line number Diff line number Diff line change
@@ -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();
}
File renamed without changes.
5 changes: 5 additions & 0 deletions cms-server/hosts/theme-demo/content/.technical/404.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Leider nichts gefunden
template: error.html
---
Da haben wir leider nichts gefunden!
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Das ist der neue Titel
---

Das ist der ganz aktuelle Inhalt
8 changes: 8 additions & 0 deletions cms-server/hosts/theme-demo/content/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Startseite
template: start.html
---

# Demo Project

![TestBild!](/media/images/test.jpg?format=small "Test bild")
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -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"}}}
Empty file.
23 changes: 23 additions & 0 deletions cms-server/hosts/theme-demo/site.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion cms-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>v2.7.0-SNAPSHOT</version>
<version>2.7.0</version>
</parent>
<artifactId>cms-server</artifactId>
<packaging>jar</packaging>
Expand Down
32 changes: 29 additions & 3 deletions cms-server/src/main/java/com/github/thmarx/cms/server/VHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +74,8 @@ public class VHost {
@Getter
private String hostname;

private Theme theme;

@Getter
private final EventBus eventBus;

Expand All @@ -94,13 +100,25 @@ 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();

var props = fileSystem.resolve("site.yaml");
siteProperties = PropertiesLoader.hostProperties(props);

theme = loadTheme();

var classLoader = new ModuleAPIClassLoader(ClassLoader.getSystemClassLoader(),
List.of(
"org.slf4j",
Expand All @@ -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
);
Expand All @@ -135,7 +154,14 @@ public void init(Path modules) throws IOException {
contentResolver = new ContentResolver(contentBase, contentRenderer, fileSystem);

this.moduleManager.initModules();
siteProperties.activeModules().stream()

List<String> 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 {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> config = (Map<String, Object>)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;
}
}
Loading

0 comments on commit 7a29c86

Please sign in to comment.