refactor: extract template functions into TemplateExtension
- Introduced `TemplateExtension` to encapsulate template functions. - Moved `static` and `asset` functions from `BlogBuilder` to `TemplateExtension`. - Adjusted package structure for `TemplateRenderer` and new `TemplateExtension`. Signed-off-by: Minecon724 <git@m724.eu>
This commit is contained in:
parent
b339d6d239
commit
8e4fae069d
3 changed files with 42 additions and 30 deletions
|
@ -3,6 +3,7 @@ package eu.m724.blog;
|
|||
import eu.m724.blog.data.Feed;
|
||||
import eu.m724.blog.data.Post;
|
||||
import eu.m724.blog.data.Site;
|
||||
import eu.m724.blog.template.TemplateRenderer;
|
||||
import org.apache.commons.io.file.PathUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.lib.RepositoryBuilder;
|
||||
|
|
39
src/main/java/eu/m724/blog/template/TemplateExtension.java
Normal file
39
src/main/java/eu/m724/blog/template/TemplateExtension.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package eu.m724.blog.template;
|
||||
|
||||
import io.pebbletemplates.pebble.extension.AbstractExtension;
|
||||
import io.pebbletemplates.pebble.extension.Function;
|
||||
import io.pebbletemplates.pebble.template.EvaluationContext;
|
||||
import io.pebbletemplates.pebble.template.PebbleTemplate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TemplateExtension extends AbstractExtension {
|
||||
@Override
|
||||
public Map<String, Function> getFunctions() {
|
||||
return Map.of(
|
||||
"static", new Function() {
|
||||
@Override
|
||||
public List<String> getArgumentNames() {
|
||||
return List.of("path");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
|
||||
return "/static/" + args.get("path"); // TODO for more advanced stuff
|
||||
}
|
||||
},
|
||||
"asset", new Function() {
|
||||
@Override
|
||||
public List<String> getArgumentNames() {
|
||||
return List.of("path");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
|
||||
return "/assets/" + args.get("path"); // TODO for more advanced stuff
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package eu.m724.blog;
|
||||
package eu.m724.blog.template;
|
||||
|
||||
import eu.m724.blog.data.Post;
|
||||
import eu.m724.blog.data.Site;
|
||||
|
@ -35,35 +35,7 @@ public class TemplateRenderer {
|
|||
|
||||
var pebbleEngine = new PebbleEngine.Builder()
|
||||
.loader(loader)
|
||||
.extension(new AbstractExtension() {
|
||||
@Override
|
||||
public Map<String, Function> getFunctions() {
|
||||
return Map.of(
|
||||
"static", new Function() {
|
||||
@Override
|
||||
public List<String> getArgumentNames() {
|
||||
return List.of("path");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
|
||||
return "/static/" + args.get("path"); // TODO for more advanced stuff
|
||||
}
|
||||
},
|
||||
"asset", new Function() {
|
||||
@Override
|
||||
public List<String> getArgumentNames() {
|
||||
return List.of("path");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
|
||||
return "/assets/" + args.get("path"); // TODO for more advanced stuff
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
})
|
||||
.extension(new TemplateExtension())
|
||||
.build();
|
||||
|
||||
this.indexTemplate = pebbleEngine.getTemplate("index_template");
|
Loading…
Add table
Reference in a new issue