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

pass current request context to shortcodes #370

Merged
merged 3 commits into from
Jan 11, 2025
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>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-api</artifactId>
<packaging>jar</packaging>
Expand Down
18 changes: 18 additions & 0 deletions cms-api/src/main/java/com/condation/cms/api/model/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/


import com.condation.cms.api.request.RequestContext;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -31,9 +32,26 @@
* @author t.marx
*/
public class Parameter extends HashMap<String, Object> {

private RequestContext requestContext = null;

public Parameter () {
}

public Parameter (final RequestContext requestContext) {
this.requestContext = requestContext;
}

public Parameter (final Map<String, Object> parameters) {
super(parameters);
}

public Parameter (final Map<String, Object> parameters, final RequestContext requestContext) {
super(parameters);
this.requestContext = requestContext;
}

public RequestContext getRequestContext() {
return requestContext;
}
}
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>7.5.0</version>
<version>7.6.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 @@ -6,7 +6,7 @@
<parent>
<groupId>com.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-content</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public Map<String, List<Section>> renderSections(final List<ContentNode> section
}

private ShortCodeTemplateFunction createShortCodeFunction(RequestContext context) {
return new ShortCodeTemplateFunction(context.get(RenderContext.class).shortCodes());
return new ShortCodeTemplateFunction(context, context.get(RenderContext.class).shortCodes());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private String processMarkdown(FilterContext<String> context) {
}

private String processShortCodes(FilterContext<String> context) {
return requestContext.get(RenderContext.class).shortCodes().replace(context.value(), model.values);
return requestContext.get(RenderContext.class).shortCodes().replace(context.value(), model.values, requestContext);
}

private String processTemplate(FilterContext<String> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@


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;
Expand Down Expand Up @@ -53,14 +54,18 @@ public Set<String> getShortCodeNames () {
}

public String replace (final String content) {
return replace(content, Collections.emptyMap());
return replace(content, Collections.emptyMap(), null);
}

public String replace (final String content, Map<String, Object> contextModel) {
return replace(content, contextModel, null);
}

public String replace (final String content, Map<String, Object> contextModel, RequestContext requestContext) {
return parser.parse(content, tagMap, contextModel);
}

public String execute (String name, Map<String, Object> parameters) {
public String execute (String name, Map<String, Object> parameters, RequestContext requestContext) {
if (!tagMap.has(name)) {
return "";
}
Expand Down
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 java.util.Map;
import lombok.Getter;
Expand All @@ -37,14 +38,16 @@ public class ShortCodeTemplateFunction {

public static final String KEY = "shortCodes";

private final RequestContext requestContext;

@Getter
private final ShortCodes shortCodes;

public String call (String name, Map<String, Object> parameters) {
return shortCodes.execute(name, parameters);
return shortCodes.execute(name, parameters, requestContext);
}
public String call (String name) {
return shortCodes.execute(name, Map.of());
return shortCodes.execute(name, Map.of(), requestContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testSomeMethod() throws Exception {
Template template = new Template("templateName", new StringReader(templateString), cfg);

Map<String, Object> model = new HashMap<>();
model.put("shortCode", new ShortCodeTemplateFunction(shortCodes));
model.put("shortCode", new ShortCodeTemplateFunction(null, shortCodes));
Writer out = new StringWriter();
template.process(model, out);

Expand All @@ -82,7 +82,7 @@ public void test_greet() throws Exception {
Template template = new Template("templateName", new StringReader(templateString), cfg);

Map<String, Object> model = new HashMap<>();
model.put("shortCode", new ShortCodeTemplateFunction(shortCodes));
model.put("shortCode", new ShortCodeTemplateFunction(null, shortCodes));
Writer out = new StringWriter();
template.process(model, out);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testSomeMethod() throws Exception {
PebbleTemplate template = engine.getTemplate(templateString);

Map<String, Object> context = new HashMap<>();
context.put("shortCode", new ShortCodeTemplateFunction(shortCodes));
context.put("shortCode", new ShortCodeTemplateFunction(null, shortCodes));

Writer writer = new StringWriter();
template.evaluate(writer, context);
Expand All @@ -84,7 +84,7 @@ public void test_greet() throws Exception {
PebbleTemplate template = engine.getTemplate(templateString);

Map<String, Object> context = new HashMap<>();
context.put("shortCode", new ShortCodeTemplateFunction(shortCodes));
context.put("shortCode", new ShortCodeTemplateFunction(null, shortCodes));

Writer writer = new StringWriter();
template.evaluate(writer, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testSomeMethod() throws Exception {
String templateString = "[(${shortCode.call('echo')})]";

Context context = new Context();
context.setVariable("shortCode", new ShortCodeTemplateFunction(shortCodes));
context.setVariable("shortCode", new ShortCodeTemplateFunction(null, shortCodes));
String renderedString = templateEngine.process(templateString, context);
Assertions.assertThat(renderedString).isEqualTo("Hello world");
}
Expand All @@ -78,7 +78,7 @@ public void test_greet() throws Exception {
String templateString = "[(${shortCode.call('greet', #{'name': 'CondationCMS'})})]";

Context context = new Context();
context.setVariable("shortCode", new ShortCodeTemplateFunction(shortCodes));
context.setVariable("shortCode", new ShortCodeTemplateFunction(null, shortCodes));
String renderedString = templateEngine.process(templateString, context);
Assertions.assertThat(renderedString).isEqualTo("Hello CondationCMS");
}
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>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-core</artifactId>
<packaging>jar</packaging>
Expand Down
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>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-extensions</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-filesystem/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>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-filesystem</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-git/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>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-git</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-media/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>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-media</artifactId>
<packaging>jar</packaging>
Expand Down
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.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>7.5.0</version>
<version>7.6.0</version>
</parent>
<artifactId>cms-server</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cms-templates/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.condation.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>7.5.0</version>
<version>7.6.0</version>
</parent>

<artifactId>cms-templates</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public class DefaultTemplate implements Template {
private final Renderer renderer;

@Override
public void evaluate(Map<String, Object> context, Writer writer) throws IOException {
public void evaluate(Map<String, Object> context, Writer writer, DynamicConfiguration dynamicConfiguration) throws IOException {

ScopeStack scopes = new ScopeStack(context);

evaluate(scopes, writer);
evaluate(scopes, writer, dynamicConfiguration);

writer.flush();
}
Expand All @@ -65,8 +65,8 @@ public String evaluate(Map<String, Object> context, DynamicConfiguration dynamic
}
}

public void evaluate (ScopeStack scopes, Writer writer) throws IOException {
renderer.render(rootNode, scopes, writer, null);
public void evaluate (ScopeStack scopes, Writer writer, DynamicConfiguration dynamicConfiguration) throws IOException {
renderer.render(rootNode, scopes, writer, dynamicConfiguration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.request.RequestContext;
import com.condation.cms.content.shortcodes.ShortCodes;
import com.condation.cms.templates.tags.component.EndComponentTag;
import com.condation.cms.templates.tags.component.ComponentTag;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -33,27 +34,33 @@
*
* @author t.marx
*/
public record DynamicConfiguration(ShortCodes shortCodes, Map<String, Component> components) {

public DynamicConfiguration {
public record DynamicConfiguration(ShortCodes shortCodes, Map<String, Component> components, RequestContext requestContext) {

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

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

components.put(openTag.getName(), openTag);
components.put(closeTag.getName(), closeTag);
}
}
public DynamicConfiguration(ShortCodes shortcodes) {
this(shortcodes, new HashMap<>());

public DynamicConfiguration(ShortCodes shortcodes, RequestContext requestContext) {
this(shortcodes, new HashMap<>(), requestContext);
}
public boolean hasComponent (String name) {

public boolean hasComponent(String name) {
return components.containsKey(name);
}
public Optional<Component> getComponent (String name) {

public Optional<Component> getComponent(String name) {
return Optional.ofNullable(components.get(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ default String evaluate () throws IOException {

default String evaluate(Map<String, Object> context) throws IOException {
var writer = new StringWriter();
evaluate(context, writer);
evaluate(context, writer, DynamicConfiguration.EMPTY);
return writer.toString();
}

String evaluate(Map<String, Object> context, DynamicConfiguration dynamicConfiguration) throws IOException;

void evaluate (Map<String, Object> context, Writer writer) throws IOException;
void evaluate (Map<String, Object> context, Writer writer, DynamicConfiguration dynamicConfiguration) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ 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);
DynamicConfiguration dynamicConfig = new DynamicConfiguration(shortCodes, model.requestContext);
return dynamicConfig;
}

Expand Down
Loading
Loading