Skip to content

Commit

Permalink
writing docs for java functions
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Oct 13, 2024
1 parent 0d620f4 commit 990cb59
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
18 changes: 18 additions & 0 deletions java-src/io/github/erdos/stencil/functions/BasicFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)`";
}
},

/**
Expand All @@ -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.";
}
},

/**
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions java-src/io/github/erdos/stencil/functions/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public interface Function {
* @return function identifier
*/
String getName();

default String getDocumentation() {
return "Documentation is not available";
}
}
28 changes: 19 additions & 9 deletions scripts/generate-fun-docs.clj.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 990cb59

Please sign in to comment.