Skip to content

Commit

Permalink
#218 template hooks added
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jun 26, 2024
1 parent 8c77eac commit 6d0d095
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #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<TemplateFunctionExtension> registerTemplateFunctions = new ArrayList<>();

public void add(final String path, final Function<?, ?> function) {
registerTemplateFunctions.add(new TemplateFunctionExtension(path, function));
}
}
Original file line number Diff line number Diff line change
@@ -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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #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;
}
}
Original file line number Diff line number Diff line change
@@ -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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #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<TemplateSupplierExtension> registerTemplateSupplier = new ArrayList<>();

public void add(final String path, final Supplier<?> supplier) {
registerTemplateSupplier.add(new TemplateSupplierExtension(path, supplier));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public void registerHttpRouteExtension(final String method, final String path, f
public Optional<HttpHandlerExtension> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ private Optional<byte[]> getTempContent(final String mediaPath, final MediaForma
private Map<String, MediaFormat> getMediaFormats() {

if (mediaFormats == null) {
mediaFormats = new HashMap<>();
Map<String, MediaFormat> 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;
Expand Down
30 changes: 30 additions & 0 deletions cms-server/hosts/features/extensions/template.extension.js
Original file line number Diff line number Diff line change
@@ -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;
})
13 changes: 1 addition & 12 deletions cms-server/hosts/features/extensions/test.extension.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 + "!"
)
})
14 changes: 14 additions & 0 deletions cms-server/hosts/features/templates/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ <h2>MediaService</h2>
</th:block>
</div>

<div class="container">
<h2>
template extension
</h2>
<div th:with="name = ${myName.get()}">
<p th:text="${name}"></p>
<!-- Thorsten -->
</div>
<div th:with="hello = ${getHello.apply('Thorsten')}">
<p th:text="${hello}"></p>
<!-- Hello Thorsten -->
</div>
</div>

<th:block th:replace="libs/fragments.html :: footer"></th:block>

</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions integration-tests/reload/config/taxonomy.tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## YAML Template.
---
values:
- id: blue
title: Blau
- id: red
title: Rot
- id: orange
title: Orange
10 changes: 10 additions & 0 deletions integration-tests/reload/config/taxonomy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## YAML Template.
---
taxonomies:
- title: Kategorie
slug: kategorien
field: taxonomy.category
- title: Tags
slug: tags
field: taxonomy.tags
array: true

0 comments on commit 6d0d095

Please sign in to comment.