From 990cb597e8f033671f21b10099b9854d5b1f63d3 Mon Sep 17 00:00:00 2001 From: janos erdos Date: Sun, 13 Oct 2024 17:46:15 +0200 Subject: [PATCH] writing docs for java functions --- .../stencil/functions/BasicFunctions.java | 18 ++++++++++++ .../erdos/stencil/functions/Function.java | 4 +++ scripts/generate-fun-docs.clj.sh | 28 +++++++++++++------ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/java-src/io/github/erdos/stencil/functions/BasicFunctions.java b/java-src/io/github/erdos/stencil/functions/BasicFunctions.java index c76c9c19..af44192c 100644 --- a/java-src/io/github/erdos/stencil/functions/BasicFunctions.java +++ b/java-src/io/github/erdos/stencil/functions/BasicFunctions.java @@ -35,6 +35,12 @@ else if (expr != null && expr.equals(value)) return null; } } + + @Override + public String getDocumentation() { + return "Select a value based on the first argument.\n" + + "Usage: `switch(expression, case-1, value-1, case-2, value-2, ..., optional-default-value)`"; + } }, /** @@ -50,6 +56,12 @@ public Object call(Object... arguments) { return arg; return null; } + + @Override + public String getDocumentation() { + return "Returns the first non-null or non-empty parameter.\n\n" + + "Accepts any number of arguments. Ignores null values, empty string and empty collections."; + } }, /** @@ -67,6 +79,12 @@ public Object call(Object... arguments) { || ((x instanceof Collection) && ((Collection) x).isEmpty()) || ((x instanceof Iterable) && !((Iterable) x).iterator().hasNext()); } + + @Override + public String getDocumentation() { + return "Returns true iff input is null, empty string or empty collection.\n\n" + + "Expects exactly 1 parameter."; + } }; @Override diff --git a/java-src/io/github/erdos/stencil/functions/Function.java b/java-src/io/github/erdos/stencil/functions/Function.java index 1cd00821..fe6e938a 100644 --- a/java-src/io/github/erdos/stencil/functions/Function.java +++ b/java-src/io/github/erdos/stencil/functions/Function.java @@ -26,4 +26,8 @@ public interface Function { * @return function identifier */ String getName(); + + default String getDocumentation() { + return "Documentation is not available"; + } } diff --git a/scripts/generate-fun-docs.clj.sh b/scripts/generate-fun-docs.clj.sh index 8a8fed72..246c8835 100755 --- a/scripts/generate-fun-docs.clj.sh +++ b/scripts/generate-fun-docs.clj.sh @@ -5,24 +5,34 @@ test ; set -e && cd "$(dirname "$0")/.." && clojure -M -i scripts/generate-fun-d ;; file is a regular CLJ script from now on -(println "HALI") - (require 'stencil.functions) +(defn get-java-functions [] + (for [f (.listFunctions (new io.github.erdos.stencil.functions.FunctionEvaluator))] + {:name (.getName f) + :docs (.getDocumentation f)})) + +(defn get-clj-functions [] + (for [[k v] (methods stencil.functions/call-fn)] + {:name k + :docs (:stencil.functions/docs (meta v))})) + +(def all-functions + (sort-by :name (concat (get-java-functions) (get-clj-functions)))) + (println "# Functions") (println) (println "You can call functions from within the template files and embed the call result easily by writing `{%=functionName(arg1, arg2, arg3, ...)%}` expression in the document template.") (println "This page contains a short description of the functions implemented in Stencil.") (println) -(doseq [k (sort (keys (methods stencil.functions/call-fn)))] - (printf "- [%s](#%s)\n" k k)) +;; Table of Contents +(doseq [f all-functions] + (printf "- [%s](#%s)\n" (:name f) (:name f))) (println) -(doseq [[k f] (sort (methods stencil.functions/call-fn))] - (printf "## %s\n\n" k) - (when-let [docs (:stencil.functions/docs (meta f))] - (println docs) - (println)) +(doseq [f all-functions] + (printf "## %s\n\n" (:name f)) + (println (:docs f)) (println))