Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parent theme #292

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>6.2.1</version>
<version>6.3.0</version>
</parent>
<artifactId>cms-api</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public ThemeProperties(final Map<String, Object> properties) {
public Double version() {
return (Double)properties.get("version");
}

public String parent() {
return (String)properties.get("parent");
}

public String templateEngine() {
return (String) getSubMap("template").get("engine");
Expand Down
6 changes: 6 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/theme/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public interface Theme {

ThemeProperties properties();

Theme getParentTheme ();

Path resolveExtension (String path);
Path resolveAsset (String path);
Path resolveTemplate (String path);

/**
* empty theme is used for sites without configured theme
* @return
Expand Down
2 changes: 1 addition & 1 deletion cms-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>6.2.1</version>
<version>6.3.0</version>
</parent>
<artifactId>cms-auth</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-content/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>6.2.1</version>
<version>6.3.0</version>
</parent>
<artifactId>cms-content</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ public String render(final ReadOnlyFile contentFile, final RequestContext contex
model.values.put("mediaService", context.get(SiteMediaServiceFeature.class).mediaService());

model.values.put("taxonomies", context.get(InjectorFeature.class).injector().getInstance(TaxonomyFunction.class));
model.values.put("messages", context.get(InjectorFeature.class).injector().getInstance(MessageSource.class));

//model.values.put("messages", context.get(InjectorFeature.class).injector().getInstance(MessageSource.class));
model.values.put("messages", context.get(RenderContext.class).theme().getMessages());

model.values.put("hooks", context.get(HookSystemFeature.class).hookSystem());

model.values.put("links", new LinkFunction(context));
Expand Down
2 changes: 1 addition & 1 deletion cms-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>6.2.1</version>
<version>6.3.0</version>
</parent>
<artifactId>cms-core</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getLabel(final String bundle, final String label) {
public String getLabel(final String bundle, final String label, final List<Object> data) {
try {
var resourceBundle = fromClassLoader(bundle, siteProperties.locale());
if (resourceBundle != null) {
if (resourceBundle != null && resourceBundle.containsKey(label)) {
var messageFormat = new MessageFormat(resourceBundle.getString(label), siteProperties.locale());
return messageFormat.format(data.toArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ThemeMessageSource(SiteProperties siteProperties, Path messageFolder, Mes
public String getLabel (final String bundle, final String label) {

var message = priorizedMessageSource.getLabel(bundle, label);
if (!("[" + label + "]").equals(label)) {
if (!("[" + label + "]").equals(message)) {
return message;
}

Expand All @@ -59,16 +59,16 @@ public String getLabel (final String bundle, final String label) {
@Override
public String getLabel (final String bundle, final String label, final List<Object> data) {

var message = priorizedMessageSource.getLabel(bundle, bundle, data);
if (!("[" + label + "]").equals(label)) {
var message = priorizedMessageSource.getLabel(bundle, label, data);
if (!("[" + label + "]").equals(message)) {
return message;
}

try {
var resourceBundle = fromClassLoader(bundle, siteProperties.locale());
if (resourceBundle != null) {
if (resourceBundle != null && resourceBundle.containsKey(label)) {
var messageFormat = new MessageFormat(resourceBundle.getString(label), siteProperties.locale());
return messageFormat.format(data);
return messageFormat.format(data.toArray());
}
} catch (Exception e) {
log.error("bundle not found", bundle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.condation.cms.theme;
package com.condation.cms.core.theme;

/*-
* #%L
Expand All @@ -22,6 +22,7 @@
* #L%
*/
import com.condation.cms.api.Constants;
import com.condation.cms.api.ServerProperties;
import com.condation.cms.api.SiteProperties;
import com.condation.cms.api.ThemeProperties;
import com.condation.cms.core.messages.EmptyMessageSource;
Expand Down Expand Up @@ -52,22 +53,50 @@ public class DefaultTheme implements Theme {
private final ThemeProperties properties;
private final MessageSource messages;
private boolean empty = false;

private Theme parent;

private DefaultTheme(final Path themePath, final ThemeProperties themeProperties, final boolean empty, final MessageSource messages) {
this(themePath, themeProperties, messages);
this.empty = empty;
}

public static Theme load(Path themePath, SiteProperties siteProperties, MessageSource siteMessages) throws IOException {
public static Theme load(
Path themePath,
SiteProperties siteProperties,
MessageSource siteMessages,
ServerProperties serverProperties) throws IOException {

return load(themePath, siteProperties, siteMessages, serverProperties, true);
}

private static Theme load(
Path themePath,
SiteProperties siteProperties,
MessageSource siteMessages,
ServerProperties serverProperties, boolean withParent) throws IOException {
Yaml yaml = new Yaml();
Path themeYaml = themePath.resolve("theme.yaml");

MessageSource messages = new ThemeMessageSource(siteProperties, themePath.resolve("messages/"), siteMessages);

var content = Files.readString(themeYaml, StandardCharsets.UTF_8);
Map<String, Object> config = (Map<String, Object>) yaml.load(content);

final ThemeProperties themeProperties = new ThemeProperties(config);
final DefaultTheme defaultTheme = new DefaultTheme(themePath, themeProperties, messages);
if (withParent && themeProperties.parent() != null) {
var parentTheme = DefaultTheme.load(
serverProperties.getThemesFolder().resolve(themeProperties.parent()),
siteProperties,
messages,
serverProperties,
false
);
defaultTheme.parent = parentTheme;
}

return new DefaultTheme(themePath, new ThemeProperties(config), messages);
return defaultTheme;
}

@Override
Expand Down Expand Up @@ -112,6 +141,53 @@ public Path extensionsPath() {

@Override
public MessageSource getMessages() {
if (parent != null) {
return parent.getMessages();
}
return messages;
}

@Override
public Theme getParentTheme() {
return parent;
}


public Path resolve(String path, Path override, Path parent) {
var resolved = override.resolve(path);
if (resolved != null) {
return resolved;
}
if (parent == null) {
return null;
}
return parent.resolve(path);
}

@Override
public Path resolveExtension(String path) {
return resolve(
path,
extensionsPath(),
getParentTheme() != null ? getParentTheme().extensionsPath() : null
);
}

@Override
public Path resolveAsset(String path) {
return resolve(
path,
assetsPath(),
getParentTheme() != null ? getParentTheme().assetsPath() : null
);
}

@Override
public Path resolveTemplate(String path) {
return resolve(
path,
templatesPath(),
getParentTheme() != null ? getParentTheme().templatesPath() : null
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.condation.cms.core.messages;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2024 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/


import com.condation.cms.api.SiteProperties;
import java.nio.file.Path;
import java.util.Map;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
*
* @author t.marx
*/
public class ThemeMessageSourceTest {

private static DefaultMessageSource messageSource;
private static ThemeMessageSource themeMessageSource;

@BeforeAll
public static void setup() {
final SiteProperties siteProperties = new SiteProperties(Map.of("language", "de"));
messageSource = new DefaultMessageSource(
siteProperties,
Path.of("src/test/resources/messages")
);

themeMessageSource = new ThemeMessageSource(
siteProperties,
Path.of("src/test/resources/parent_messages"),
messageSource
);
}

@Test
public void from_child() {
var label = themeMessageSource.getLabel("abundle", "message.child");
Assertions.assertThat(label).isEqualTo("hello child");
}

@Test
public void from_parent() {
var label = themeMessageSource.getLabel("abundle", "message.parent");
Assertions.assertThat(label).isEqualTo("hello parent");
}

@Test
public void override_parent() {
var label = themeMessageSource.getLabel("abundle", "parent");
Assertions.assertThat(label).isEqualTo("I override the parent");
}
}
2 changes: 2 additions & 0 deletions cms-core/src/test/resources/messages/abundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
###

button.submit=Absenden
message.child=hello child
parent=I override the parent
23 changes: 23 additions & 0 deletions cms-core/src/test/resources/parent_messages/abundle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# #%L
# cms-api
# %%
# Copyright (C) 2023 - 2024 Marx-Software
# %%
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/gpl-3.0.html>.
# #L%
###

message.parent=hello parent
parent=I'm the parent
2 changes: 1 addition & 1 deletion cms-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>6.2.1</version>
<version>6.3.0</version>
</parent>
<artifactId>cms-extensions</artifactId>
<packaging>jar</packaging>
Expand Down
Loading