Skip to content

Commit

Permalink
#200 example to add custom route to named menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jun 3, 2024
1 parent 3364e59 commit e6cc871
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@
* #L%
*/

import java.util.Map;
import java.util.function.Function;

/**
*
* @author t.marx
*/
public record Action(String name, int priority, Function<ActionContext<Object>, Object> function) implements Hook {
public record Action(String name, int priority, ActionFunction function) implements Hook {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.thmarx.cms.api.hooks;

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

/**
*
* @author t.marx
*/
@FunctionalInterface
public interface ActionFunction extends Function<ActionContext<Object>, Object> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@
* #L%
*/

import java.util.List;
import java.util.function.Function;

/**
*
* @author t.marx
*/
public record Filter(String name, int priority, Function<FilterContext<Object>, List<Object>> function) implements Hook {
public record Filter<T>(String name, int priority, FilterFunction<T> function) implements Hook {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.thmarx.cms.api.hooks;

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

/**
*
* @author t.marx
* @param <T>
*/
@FunctionalInterface
public interface FilterFunction<T> extends Function<FilterContext<T>, List<T>> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,35 @@
import java.util.Map;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
import org.checkerframework.common.value.qual.ArrayLen;
import lombok.extern.slf4j.Slf4j;

/**
*
* Request based hook system.
*
* @author t.marx
*/
@Slf4j
@RequiredArgsConstructor
public class HookSystem {

Multimap<String, Action> actions = ArrayListMultimap.create();

Multimap<String, Filter> filters = ArrayListMultimap.create();

@Deprecated(since = "4.18.0", forRemoval = true)
public void register(final String name, final Function<ActionContext<Object>, Object> hookFunction) {
register(name, hookFunction, 10);
}

@Deprecated(since = "4.18.0", forRemoval = true)
public void register(final String name, final Function<ActionContext<Object>, Object> hookFunction, int priority) {
actions.put(name, new Action(name, priority, hookFunction));
}

public void registerAction(final String name, final Function<ActionContext<Object>, Object> hookFunction) {
public void registerAction(final String name, final ActionFunction hookFunction) {
registerAction(name, hookFunction, 10);
}

public void registerAction(final String name, final Function<ActionContext<Object>, Object> hookFunction, int priority) {
public void registerAction(final String name, final ActionFunction hookFunction, int priority) {
actions.put(name, new Action(name, priority, hookFunction));
}

public void registerFilter(final String name, final Function<FilterContext<Object>, List<Object>> hookFunction) {
public <T> void registerFilter(final String name, final FilterFunction<T> hookFunction) {
registerFilter(name, hookFunction, 10);
}

public void registerFilter(final String name, final Function<FilterContext<Object>, List<Object>> hookFunction, int priority) {
public <T> void registerFilter(final String name, final FilterFunction<T> hookFunction, int priority) {
filters.put(name, new Filter(name, priority, hookFunction));
}

Expand All @@ -79,7 +70,15 @@ public ActionContext<Object> execute(final String name, final Map<String, Object
var context = new ActionContext(new HashMap<>(arguments), new ArrayList<>());
actions.get(name).stream()
.sorted((h1, h2) -> Integer.compare(h1.priority(), h2.priority()))
.map(action -> action.function().apply(context))
.map((action) -> {
try {
return action.function().apply(context);
} catch (Exception e) {
log.error("error executing action");
}
return null;
})
.filter(value -> value != null)
.forEach(context.results()::add);

return context;
Expand All @@ -100,11 +99,15 @@ public <T> FilterContext<T> filter(final String name, final List<T> parameters)
);
filters.get(name).stream()
.sorted((h1, h2) -> Integer.compare(h1.priority(), h2.priority()))
.forEach(action -> {
var context = new FilterContext(new ArrayList<>(returnContext.values()));
var result = action.function().apply(context);
returnContext.values().clear();
returnContext.values().addAll(result);
.forEach((var action) -> {
try {
var context = new FilterContext(new ArrayList<>(returnContext.values()));
var result = action.function().apply(context);
returnContext.values().clear();
returnContext.values().addAll((List<T>)result);
} catch (Exception e) {
log.error("error on filter");
}
});

return returnContext;
Expand Down
48 changes: 48 additions & 0 deletions cms-api/src/main/java/com/github/thmarx/cms/api/hooks/Hooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.thmarx.cms.api.hooks;

/*-
* #%L
* cms-api
* %%
* 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%
*/

/**
*
* @author t.marx
*/
public enum Hooks {

NAVIGATION_PATH("navigation/%s/path"),
NAVIGATION_LIST("navigation/%s/list"),
;

private String pattern;

private Hooks (String pattern) {
this.pattern = pattern;
}

public String hook () {
return pattern;
}

public String hook (Object...variables) {
return pattern.formatted(variables);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
* @author t.marx
*/
public record NavNode (String name, String path, int depth, boolean current, List<NavNode> children) {
public NavNode (String name, String path) {
this(name, path, 1, false, Collections.emptyList());
}
public NavNode (String name, String path, int depth) {
this(name, path, depth, false, Collections.emptyList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import { HookSystemFeature, $features } from 'system/features.mjs';
const hooks = $features.get(HookSystemFeature).hookSystem()

export const $hooks = {
register : (name, fun, priority) => {
if (priority) {
hooks.register(name, fun, priority)
} else {
hooks.register(name, fun)
}
},
registerAction : (name, fun, priority) => {
if (priority) {
hooks.registerAction(name, fun, priority)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NavNode = Java.type("com.github.thmarx.cms.api.model.NavNode").class
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import { $hooks } from 'system/hooks.mjs';

$hooks.register(
$hooks.registerAction(
"test",
(context) => {
return 'hallo'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ menu:
position: 31
---

## Sub Navigation
## Sub Navigation

## relative linking

test a [relative link to home](..).

test a [relative link to parent](.).
14 changes: 14 additions & 0 deletions cms-server/hosts/features/content/navigation/subfolder/other.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Other Sub Navigation
template: navigation.html
menu:
position: 31
---

## Sub Navigation

## relative linking

test a [relative link to home](..).

test a [relative link to parent](.).
8 changes: 8 additions & 0 deletions cms-server/hosts/features/extensions/menu.extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { $hooks } from 'system/hooks.mjs';
import { NavNode } from 'system/navigation.mjs';

$hooks.registerFilter("navigation/top/list", (context) => {
var nodes = context.values()
nodes.add(2, new NavNode("Hello-Extension", "/hello-extension"))
return nodes
})
2 changes: 1 addition & 1 deletion cms-server/hosts/features/extensions/theme.extension.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $hooks } from 'system/hooks.mjs';

$hooks.register(
$hooks.registerAction(
"theme/header",
(context) => {
return `
Expand Down
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions cms-server/themes/test/templates/libs/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<script th:src="${links.createUrl('/theme/assets/prism-1.29.0/prism.js')}" defer></script>
<link rel="stylesheet" th:href="${links.createUrl('/theme/assets/prism-1.29.0/prism.css')}" defer />

<th:block th:each="header : ${hooks.call('theme/header').results()}">
<th:block th:each="header : ${hooks.execute('theme/header').results()}">
<th:block th:utext="${header}"></th:block>
</th:block>

Expand All @@ -34,7 +34,7 @@
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarScroll">
<ul th:with="nodes=${navigation.list('/')}" class="navbar-nav me-auto my-2 my-lg-0 navbar-nav-scroll"
<ul th:with="nodes=${navigation.named('top').list('/')}" class="navbar-nav me-auto my-2 my-lg-0 navbar-nav-scroll"
style="--bs-scroll-height: 100px;">
<li th:each="node : ${nodes}" th:if="${node.path} != '/' AND ${node.path} != '/?preview=true'" class="nav-item">
<a th:attr="aria-current=${node.current ? 'page' : ''}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,17 @@
import com.github.thmarx.cms.api.feature.features.ContentParserFeature;
import com.github.thmarx.cms.api.feature.features.HookSystemFeature;
import com.github.thmarx.cms.api.feature.features.MarkdownRendererFeature;
import com.github.thmarx.cms.api.hooks.HookContext;
import com.github.thmarx.cms.api.hooks.HookSystem;
import com.github.thmarx.cms.api.hooks.Hooks;
import com.github.thmarx.cms.api.request.RequestContext;
import com.github.thmarx.cms.api.utils.PathUtil;
import com.github.thmarx.cms.template.functions.AbstractCurrentNodeFunction;
import com.github.thmarx.cms.api.utils.NodeUtil;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import lombok.extern.slf4j.Slf4j;

/**
Expand Down Expand Up @@ -106,7 +102,7 @@ public List<NavNode> path() {
navNodes = navNodes.reversed();

if (name != null) {
var hookContext = hookSystem.filter("navigation/" + name + "/path", navNodes);
var hookContext = hookSystem.filter(Hooks.NAVIGATION_PATH.hook(name), navNodes);
navNodes = hookContext.values();
}

Expand Down Expand Up @@ -146,7 +142,7 @@ private List<NavNode> getNodes(final String start, final int depth) {
navNodes = getNodesFromBase(currentNode.getParent(), start.substring(2), depth);
}
if (name != null) {
var hookContext = hookSystem.filter("navigation/" + name + "/list", navNodes);
var hookContext = hookSystem.filter(Hooks.NAVIGATION_LIST.hook(name), navNodes);
navNodes = hookContext.values();
}
return navNodes;
Expand Down
Loading

0 comments on commit e6cc871

Please sign in to comment.