From 6d0d09535698e8ee899338821255288521aabbd7 Mon Sep 17 00:00:00 2001 From: Thorsten Marx Date: Wed, 26 Jun 2024 13:22:16 +0200 Subject: [PATCH] #218 template hooks added --- .../cms/content/DefaultContentRenderer.java | 8 +++ .../hooks/TemplateFunctionWrapper.java | 43 ++++++++++++++ .../cms/extensions/hooks/TemplateHooks.java | 58 +++++++++++++++++++ .../hooks/TemplateSupplierWrapper.java | 43 ++++++++++++++ .../extensions/request/RequestExtensions.java | 4 +- .../github/thmarx/cms/media/MediaManager.java | 7 ++- .../features/extensions/template.extension.js | 30 ++++++++++ .../features/extensions/test.extension.js | 13 +---- .../hosts/features/templates/start.html | 14 +++++ .../cms/request/RequestContextFactory.java | 2 + .../com/github/thmarx/cms/TestHelper.java | 3 + .../reload/config/taxonomy.tags.yaml | 9 +++ integration-tests/reload/config/taxonomy.yaml | 10 ++++ 13 files changed, 227 insertions(+), 17 deletions(-) create mode 100644 cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateFunctionWrapper.java create mode 100644 cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateHooks.java create mode 100644 cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateSupplierWrapper.java create mode 100644 cms-server/hosts/features/extensions/template.extension.js create mode 100644 integration-tests/reload/config/taxonomy.tags.yaml create mode 100644 integration-tests/reload/config/taxonomy.yaml diff --git a/cms-content/src/main/java/com/github/thmarx/cms/content/DefaultContentRenderer.java b/cms-content/src/main/java/com/github/thmarx/cms/content/DefaultContentRenderer.java index c6c13816..c50563f5 100644 --- a/cms-content/src/main/java/com/github/thmarx/cms/content/DefaultContentRenderer.java +++ b/cms-content/src/main/java/com/github/thmarx/cms/content/DefaultContentRenderer.java @@ -48,6 +48,7 @@ import com.github.thmarx.cms.api.feature.features.ServerPropertiesFeature; import com.github.thmarx.cms.template.functions.LinkFunction; import com.github.thmarx.cms.content.views.model.View; +import com.github.thmarx.cms.extensions.hooks.TemplateHooks; import com.github.thmarx.cms.template.functions.query.QueryFunction; import com.github.thmarx.cms.template.functions.taxonomy.TaxonomyFunction; import com.github.thmarx.cms.extensions.request.RequestExtensions; @@ -189,6 +190,13 @@ public String render(final Path contentFile, final RequestContext context, model.values.put("USERNAME", context.get(AuthFeature.class).username()); } + context.get(TemplateHooks.class).getTemplateSupplier().getRegisterTemplateSupplier().forEach(service -> { + model.values.put(service.name(), service.supplier()); + }); + context.get(TemplateHooks.class).getTemplateFunctions().getRegisterTemplateFunctions().forEach(service -> { + model.values.put(service.name(), service.function()); + }); + context.get(RequestExtensions.class).getRegisterTemplateSupplier().forEach(service -> { model.values.put(service.name(), service.supplier()); }); diff --git a/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateFunctionWrapper.java b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateFunctionWrapper.java new file mode 100644 index 00000000..def7bd04 --- /dev/null +++ b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateFunctionWrapper.java @@ -0,0 +1,43 @@ +package com.github.thmarx.cms.extensions.hooks; + +/*- + * #%L + * cms-extensions + * %% + * 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 + * . + * #L% + */ + +import com.github.thmarx.cms.extensions.TemplateFunctionExtension; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; +import lombok.Getter; + +/** + * + * @author t.marx + */ +public class TemplateFunctionWrapper { + + @Getter + private final List registerTemplateFunctions = new ArrayList<>(); + + public void add(final String path, final Function function) { + registerTemplateFunctions.add(new TemplateFunctionExtension(path, function)); + } +} diff --git a/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateHooks.java b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateHooks.java new file mode 100644 index 00000000..8e66856a --- /dev/null +++ b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateHooks.java @@ -0,0 +1,58 @@ +package com.github.thmarx.cms.extensions.hooks; + +/*- + * #%L + * cms-server + * %% + * 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 + * . + * #L% + */ + +import com.github.thmarx.cms.api.annotations.Experimental; +import com.github.thmarx.cms.api.feature.Feature; +import com.github.thmarx.cms.api.feature.features.HookSystemFeature; +import com.github.thmarx.cms.api.request.RequestContext; +import java.util.Map; +import lombok.RequiredArgsConstructor; + +/** + * + * @author t.marx + */ +@Experimental +@RequiredArgsConstructor +public class TemplateHooks implements Feature { + + private final RequestContext requestContext; + + + public TemplateSupplierWrapper getTemplateSupplier () { + var templateSupplier = new TemplateSupplierWrapper(); + requestContext.get(HookSystemFeature.class).hookSystem() + .execute("template/supplier/register", Map.of("suppliers", templateSupplier)); + + return templateSupplier; + } + + public TemplateFunctionWrapper getTemplateFunctions () { + var templateFunctions = new TemplateFunctionWrapper(); + requestContext.get(HookSystemFeature.class).hookSystem() + .execute("template/function/register", Map.of("functions", templateFunctions)); + + return templateFunctions; + } +} diff --git a/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateSupplierWrapper.java b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateSupplierWrapper.java new file mode 100644 index 00000000..17f59ea9 --- /dev/null +++ b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/hooks/TemplateSupplierWrapper.java @@ -0,0 +1,43 @@ +package com.github.thmarx.cms.extensions.hooks; + +/*- + * #%L + * cms-extensions + * %% + * 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 + * . + * #L% + */ + +import com.github.thmarx.cms.extensions.TemplateSupplierExtension; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Supplier; +import lombok.Getter; + +/** + * + * @author t.marx + */ +public class TemplateSupplierWrapper { + + @Getter + private final List registerTemplateSupplier = new ArrayList<>(); + + public void add(final String path, final Supplier supplier) { + registerTemplateSupplier.add(new TemplateSupplierExtension(path, supplier)); + } +} diff --git a/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/request/RequestExtensions.java b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/request/RequestExtensions.java index 986a7001..1337bf83 100644 --- a/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/request/RequestExtensions.java +++ b/cms-extensions/src/main/java/com/github/thmarx/cms/extensions/request/RequestExtensions.java @@ -84,11 +84,11 @@ public void registerHttpRouteExtension(final String method, final String path, f public Optional findHttpRouteHandler (final String method, final String path) { return httpRouteHandlerExtensions.stream().filter(handler -> handler.method().equalsIgnoreCase(method) && handler.path().equalsIgnoreCase(path)).findFirst(); } - + @Deprecated public void registerTemplateSupplier(final String path, final Supplier supplier) { registerTemplateSupplier.add(new TemplateSupplierExtension(path, supplier)); } - + @Deprecated public void registerTemplateFunction(final String path, final Function function) { registerTemplateFunctions.add(new TemplateFunctionExtension(path, function)); } diff --git a/cms-media/src/main/java/com/github/thmarx/cms/media/MediaManager.java b/cms-media/src/main/java/com/github/thmarx/cms/media/MediaManager.java index b384c9f2..b3a20951 100644 --- a/cms-media/src/main/java/com/github/thmarx/cms/media/MediaManager.java +++ b/cms-media/src/main/java/com/github/thmarx/cms/media/MediaManager.java @@ -161,12 +161,13 @@ private Optional getTempContent(final String mediaPath, final MediaForma private Map getMediaFormats() { if (mediaFormats == null) { - mediaFormats = new HashMap<>(); + Map tempFormats = new HashMap<>(); if (!theme.empty()) { - mediaFormats.putAll(theme.properties().getMediaFormats()); + tempFormats.putAll(theme.properties().getMediaFormats()); } - mediaFormats.putAll(configuration.get(SiteConfiguration.class).siteProperties().getMediaFormats()); + tempFormats.putAll(configuration.get(SiteConfiguration.class).siteProperties().getMediaFormats()); + mediaFormats = tempFormats; } return mediaFormats; diff --git a/cms-server/hosts/features/extensions/template.extension.js b/cms-server/hosts/features/extensions/template.extension.js new file mode 100644 index 00000000..8eca36a5 --- /dev/null +++ b/cms-server/hosts/features/extensions/template.extension.js @@ -0,0 +1,30 @@ +import { $template } from 'system/template.mjs'; +import { $hooks } from 'system/hooks.mjs'; + +/* +$template.registerTemplateSupplier( + "myName", + () => "Thorsten" +) + */ +$hooks.registerAction("template/supplier/register", (context) => { + context.arguments().get("suppliers").add( + "myName", + () => "My name is Thorsten" + ) + return null; +}) + +/* +$template.registerTemplateFunction( + "getHello", + (name) => "Hello " + name + "!" +) + */ +$hooks.registerAction("template/function/register", (context) => { + context.arguments().get("functions").add( + "getHello", + (name) => "Hello " + name + "!!" + ) + return null; +}) diff --git a/cms-server/hosts/features/extensions/test.extension.js b/cms-server/hosts/features/extensions/test.extension.js index a1726ffe..03612bec 100644 --- a/cms-server/hosts/features/extensions/test.extension.js +++ b/cms-server/hosts/features/extensions/test.extension.js @@ -1,7 +1,6 @@ import { UTF_8 } from 'system/charsets.mjs'; import { $http } from 'system/http.mjs'; import { $routes } from 'system/routes.mjs'; -import { $template } from 'system/template.mjs'; import { getLogger } from 'system/logging.mjs'; const logger = getLogger("extensions"); @@ -20,14 +19,4 @@ $http.post("/form", (request, response) => { $routes.get("/hello-extension", (request, response) => { response.addHeader("Content-Type", "text/html; charset=utf-8") response.write("extension route", UTF_8) -}) - -$template.registerTemplateSupplier( - "myName", - () => "Thorsten" -) - -$template.registerTemplateFunction( - "getHello", - (name) => "Hello " + name + "!" -) \ No newline at end of file +}) \ No newline at end of file diff --git a/cms-server/hosts/features/templates/start.html b/cms-server/hosts/features/templates/start.html index 78b16cf1..5fe621ae 100644 --- a/cms-server/hosts/features/templates/start.html +++ b/cms-server/hosts/features/templates/start.html @@ -69,6 +69,20 @@

MediaService

+
+

+ template extension +

+
+

+ +
+
+

+ +
+
+ diff --git a/cms-server/src/main/java/com/github/thmarx/cms/request/RequestContextFactory.java b/cms-server/src/main/java/com/github/thmarx/cms/request/RequestContextFactory.java index 523faaf1..cf639fc5 100644 --- a/cms-server/src/main/java/com/github/thmarx/cms/request/RequestContextFactory.java +++ b/cms-server/src/main/java/com/github/thmarx/cms/request/RequestContextFactory.java @@ -54,6 +54,7 @@ import com.github.thmarx.cms.api.utils.HTTPUtil; import com.github.thmarx.cms.api.utils.RequestUtil; import com.github.thmarx.cms.extensions.hooks.ServerHooks; +import com.github.thmarx.cms.extensions.hooks.TemplateHooks; import com.github.thmarx.modules.api.ModuleManager; import com.google.inject.Injector; import java.io.IOException; @@ -116,6 +117,7 @@ public RequestContext create( RequestExtensions requestExtensions = extensionManager.newContext(theme, requestContext); requestContext.add(ServerHooks.class, new ServerHooks(requestContext)); + requestContext.add(TemplateHooks.class, new TemplateHooks(requestContext)); RenderContext renderContext = new RenderContext( markdownRenderer, diff --git a/cms-server/src/test/java/com/github/thmarx/cms/TestHelper.java b/cms-server/src/test/java/com/github/thmarx/cms/TestHelper.java index f89bc4e1..e69e9563 100644 --- a/cms-server/src/test/java/com/github/thmarx/cms/TestHelper.java +++ b/cms-server/src/test/java/com/github/thmarx/cms/TestHelper.java @@ -40,6 +40,7 @@ import com.github.thmarx.cms.content.shortcodes.ShortCodes; import com.github.thmarx.cms.media.FileMediaService; import com.github.thmarx.cms.content.RenderContext; +import com.github.thmarx.cms.extensions.hooks.TemplateHooks; import com.github.thmarx.cms.extensions.request.RequestExtensions; import com.github.thmarx.cms.theme.DefaultTheme; import com.google.inject.Injector; @@ -80,6 +81,8 @@ public static RequestContext requestContext(String uri) { )))); context.add(ServerPropertiesFeature.class, new ServerPropertiesFeature(new ServerProperties(Map.of( )))); + + context.add(TemplateHooks.class, new TemplateHooks(context)); return context; } diff --git a/integration-tests/reload/config/taxonomy.tags.yaml b/integration-tests/reload/config/taxonomy.tags.yaml new file mode 100644 index 00000000..1b7740c3 --- /dev/null +++ b/integration-tests/reload/config/taxonomy.tags.yaml @@ -0,0 +1,9 @@ +## YAML Template. +--- +values: + - id: blue + title: Blau + - id: red + title: Rot + - id: orange + title: Orange \ No newline at end of file diff --git a/integration-tests/reload/config/taxonomy.yaml b/integration-tests/reload/config/taxonomy.yaml new file mode 100644 index 00000000..0005133f --- /dev/null +++ b/integration-tests/reload/config/taxonomy.yaml @@ -0,0 +1,10 @@ +## YAML Template. +--- +taxonomies: + - title: Kategorie + slug: kategorien + field: taxonomy.category + - title: Tags + slug: tags + field: taxonomy.tags + array: true \ No newline at end of file