|
| 1 | +/* |
| 2 | + * Copyright 2026 znai maintainers |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.testingisdocumenting.znai.extensions.javascript; |
| 18 | + |
| 19 | +import org.testingisdocumenting.znai.core.ComponentsRegistry; |
| 20 | +import org.testingisdocumenting.znai.extensions.PluginParams; |
| 21 | +import org.testingisdocumenting.znai.extensions.PluginResult; |
| 22 | +import org.testingisdocumenting.znai.extensions.include.IncludePlugin; |
| 23 | +import org.testingisdocumenting.znai.parser.ParserHandler; |
| 24 | +import org.testingisdocumenting.znai.search.SearchScore; |
| 25 | +import org.testingisdocumenting.znai.search.SearchText; |
| 26 | +import org.testingisdocumenting.znai.utils.CollectionUtils; |
| 27 | + |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.StringJoiner; |
| 33 | + |
| 34 | +public class JavascriptFunctionIncludePlugin implements IncludePlugin { |
| 35 | + private String functionName; |
| 36 | + private Map<String, Object> args; |
| 37 | + |
| 38 | + @Override |
| 39 | + public String id() { |
| 40 | + return "javascript-function"; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public IncludePlugin create() { |
| 45 | + return new JavascriptFunctionIncludePlugin(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public PluginResult process(ComponentsRegistry componentsRegistry, |
| 50 | + ParserHandler parserHandler, |
| 51 | + Path markupPath, |
| 52 | + PluginParams pluginParams) { |
| 53 | + functionName = pluginParams.getFreeParam(); |
| 54 | + if (functionName == null || functionName.isEmpty()) { |
| 55 | + throw new IllegalArgumentException("javascript function name must be provided as a free param," + |
| 56 | + " e.g. :include-javascript-function: myFunction"); |
| 57 | + } |
| 58 | + |
| 59 | + args = pluginParams.getOpts().toMap(); |
| 60 | + |
| 61 | + return PluginResult.docElement("JavascriptFunction", CollectionUtils.createMap( |
| 62 | + "functionName", functionName, |
| 63 | + "args", args)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public List<SearchText> textForSearch() { |
| 68 | + StringJoiner joiner = new StringJoiner(" "); |
| 69 | + joiner.add(functionName); |
| 70 | + collectSearchableValues(args, joiner); |
| 71 | + |
| 72 | + return List.of(SearchScore.STANDARD.text(joiner.toString())); |
| 73 | + } |
| 74 | + |
| 75 | + private static void collectSearchableValues(Object value, StringJoiner joiner) { |
| 76 | + if (value == null) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (value instanceof Map) { |
| 81 | + ((Map<?, ?>) value).values().forEach(v -> collectSearchableValues(v, joiner)); |
| 82 | + } else if (value instanceof Collection) { |
| 83 | + ((Collection<?>) value).forEach(v -> collectSearchableValues(v, joiner)); |
| 84 | + } else { |
| 85 | + joiner.add(value.toString()); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments