Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.condation.cms.api.extensions;

/*-
* #%L
* cms-api
* %%
* 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.model.Parameter;
import java.util.Map;
import java.util.function.Function;

/**
*
* @author t.marx
*/
public abstract class RegisterTemplateComponentExtensionPoint extends AbstractExtensionPoint {

public abstract Map<String, Function<Parameter, String>> components ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum Hooks {
NAVIGATION_LIST("system/navigation/%s/list"),
CONTENT_SHORTCODE("system/content/shortcodes"),
CONTENT_FILTER("system/content/filter"),
TEMPLATE_COMPONENT("system/template/component"),
DB_QUERY_OPERATIONS("system/db/query/operations"),
SCHEDULER_REGISTER("system/scheduler/register"),
SCHEDULER_REMOVE("system/scheduler/remove"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.condation.cms.extensions.hooks;

/*-
* #%L
* cms-extensions
* %%
* 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.model.Parameter;
import java.util.Map;
import java.util.function.Function;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
*
* @author t.marx
*/
@RequiredArgsConstructor
public class TemplateComponentsWrapper {

@Getter
private final Map<String, Function<Parameter, String>> components;

public void put(final String name, final Function<Parameter, String> function) {
components.put(name, function);
}

public boolean contains(final String name) {
return components.containsKey(name);
}

public void remove(final String name) {
components.remove(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import com.condation.cms.api.feature.Feature;
import com.condation.cms.api.feature.features.HookSystemFeature;
import com.condation.cms.api.hooks.Hooks;
import com.condation.cms.api.model.Parameter;
import com.condation.cms.api.request.RequestContext;
import java.util.Map;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;

/**
Expand Down Expand Up @@ -59,4 +61,12 @@ public TemplateFunctionWrapper getTemplateFunctions () {

return templateFunctions;
}

public TemplateComponentsWrapper getComponents (Map<String, Function<Parameter, String>> components) {
var componentsWrapper = new TemplateComponentsWrapper(components);
requestContext.get(HookSystemFeature.class).hookSystem()
.execute(Hooks.TEMPLATE_COMPONENT.hook(), Map.of("components", componentsWrapper));

return componentsWrapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
import com.condation.cms.api.request.RequestContext;
import com.condation.cms.content.shortcodes.ShortCodes;
import com.condation.cms.templates.components.TemplateComponents;
import com.condation.cms.templates.tags.component.EndComponentTag;
import com.condation.cms.templates.tags.component.ComponentTag;
import java.util.Collections;
Expand All @@ -34,26 +35,26 @@
*
* @author t.marx
*/
public record DynamicConfiguration(ShortCodes shortCodes, Map<String, Component> components, RequestContext requestContext) {
public record DynamicConfiguration(TemplateComponents templateComponents, Map<String, Component> components, RequestContext requestContext) {

public static final DynamicConfiguration EMPTY = new DynamicConfiguration(
new ShortCodes(Collections.emptyMap(), null),
new TemplateComponents(Collections.emptyMap()),
Collections.emptyMap(),
null
);

public DynamicConfiguration {
for (var tag : shortCodes.getShortCodeNames()) {
var openTag = new ComponentTag(tag, shortCodes);
for (var tag : templateComponents.getComponentNames()) {
var openTag = new ComponentTag(tag, templateComponents);
var closeTag = new EndComponentTag(tag);

components.put(openTag.getName(), openTag);
components.put(closeTag.getName(), closeTag);
}
}

public DynamicConfiguration(ShortCodes shortcodes, RequestContext requestContext) {
this(shortcodes, new HashMap<>(), requestContext);
public DynamicConfiguration(TemplateComponents templateComponents, RequestContext requestContext) {
this(templateComponents, new HashMap<>(), requestContext);
}

public boolean hasComponent(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.condation.cms.templates.components;

/*-
* #%L
* cms-content
* %%
* 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.model.Parameter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

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

private final Map<String, Function<Parameter, String>> tags = new HashMap<>();

public Set<String> names () {
return Collections.unmodifiableSet(tags.keySet());
}

public void put(String codeName, Function<Parameter, String> function) {
tags.put(codeName, function);
}

public void putAll(Map<String, Function<Parameter, String>> tags) {
this.tags.putAll(tags);
}

public boolean has(String codeName) {
return tags.containsKey(codeName);
}

public Function<Parameter, String> get(String name) {
return tags.getOrDefault(name, (params) -> "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.condation.cms.templates.components;

/*-
* #%L
* cms-content
* %%
* 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.model.Parameter;
import com.condation.cms.api.request.RequestContext;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
*
* @author t.marx
*/
@Slf4j
public class TemplateComponents {

private final ComponentMap componentMap;

public TemplateComponents (Map<String, Function<Parameter, String>> components) {
this.componentMap = new ComponentMap();
this.componentMap.putAll(components);
}

public Set<String> getComponentNames () {
return componentMap.names();
}

public String execute (String name, Map<String, Object> parameters, RequestContext requestContext) {
if (!componentMap.has(name)) {
return "";
}
try {
Parameter params;
if (parameters != null) {
params = new Parameter(parameters);
} else {
params = new Parameter();
}
return componentMap.get(name).apply(params);
} catch (Exception e) {
log.error("",e);
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@
*/
import com.condation.cms.api.cache.CacheManager;
import com.condation.cms.api.db.DB;
import com.condation.cms.api.extensions.RegisterTemplateComponentExtensionPoint;
import com.condation.cms.api.feature.features.InjectorFeature;
import com.condation.cms.api.model.Parameter;
import com.condation.cms.api.request.RequestContext;
import com.condation.cms.api.template.TemplateEngine;
import com.condation.cms.api.theme.Theme;
import com.condation.cms.content.RenderContext;
import com.condation.cms.content.template.functions.shortcode.ShortCodeTemplateFunction;
import com.condation.cms.extensions.hooks.TemplateHooks;
import com.condation.cms.templates.CMSTemplateEngine;
import com.condation.cms.templates.DynamicConfiguration;
import com.condation.cms.templates.TemplateEngineFactory;
import com.condation.cms.templates.TemplateLoader;
import com.condation.cms.templates.components.TemplateComponents;
import com.condation.cms.templates.loaders.CompositeTemplateLoader;
import com.condation.cms.templates.loaders.FileTemplateLoader;
import com.condation.modules.api.ModuleManager;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
*
Expand Down Expand Up @@ -115,10 +123,23 @@ public String render(String template, Model model) throws IOException {
}

private DynamicConfiguration createDynamicConfiguration(Model model) {
var shortCodes = model.requestContext.get(RenderContext.class).shortCodes();
DynamicConfiguration dynamicConfig = new DynamicConfiguration(shortCodes, model.requestContext);
DynamicConfiguration dynamicConfig = new DynamicConfiguration(
createTemplateComponents(model.requestContext), model.requestContext);
return dynamicConfig;
}

private TemplateComponents createTemplateComponents(RequestContext requestContext) {
Map<String, Function<Parameter, String>> components = new HashMap<>();

var injector = requestContext.get(InjectorFeature.class).injector();

injector.getInstance(ModuleManager.class)
.extensions(RegisterTemplateComponentExtensionPoint.class)
.forEach(extension -> components.putAll(extension.components()));

var wrapper = requestContext.get(TemplateHooks.class).getComponents(components);
return new TemplateComponents(wrapper.getComponents());
}

@Override
public String renderFromString(String templateString, Model model) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.condation.cms.content.shortcodes.ShortCodes;
import com.condation.cms.templates.Component;
import com.condation.cms.templates.components.TemplateComponents;
import com.condation.cms.templates.exceptions.RenderException;
import com.condation.cms.templates.parser.ComponentNode;
import com.condation.cms.templates.renderer.Renderer;
Expand All @@ -42,19 +43,19 @@
@RequiredArgsConstructor
public class ComponentTag implements Component {

private final String shortCodeName;
private final String componentName;

private final ShortCodes shortCodes;
private final TemplateComponents components;

@Override
public String getName() {
return shortCodeName;
return componentName;
}


@Override
public Optional<String> getCloseingName() {
return Optional.of("end%s".formatted(shortCodeName));
return Optional.of("end%s".formatted(componentName));
}

@Override
Expand All @@ -66,7 +67,7 @@ public void render(ComponentNode node, Renderer.Context context, Writer writer)

params.put("_content", content);

var shortCodeResult = shortCodes.execute(shortCodeName, params, context.dynamicConfiguration().requestContext());
var shortCodeResult = components.execute(componentName, params, context.dynamicConfiguration().requestContext());
if (!Strings.isNullOrEmpty(shortCodeResult)) {
writer.write(shortCodeResult);
}
Expand Down
Loading