use of com.laytonsmith.core.exceptions.CRE.CREThrowable in project CommandHelper by EngineHub.
the class DocGen method functions.
/**
* Returns the documentation for a single function.
*
* @param type The type of output to use. May be one of: html, wiki, text
* @param platform The platform we're using
* @param staged Is this for the staged wiki?
* @return
* @throws ConfigCompileException
*/
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
public static String functions(MarkupType type, api.Platforms platform, boolean staged) throws ConfigCompileException {
Set<FunctionBase> functions = FunctionList.getFunctionList(platform);
HashMap<Class, ArrayList<FunctionBase>> functionlist = new HashMap<Class, ArrayList<FunctionBase>>();
StringBuilder out = new StringBuilder();
for (FunctionBase f : functions) {
// Sort the functions into classes
Class apiClass = (f.getClass().getEnclosingClass() != null ? f.getClass().getEnclosingClass() : null);
ArrayList<FunctionBase> fl = functionlist.get(apiClass);
if (fl == null) {
fl = new ArrayList<FunctionBase>();
functionlist.put(apiClass, fl);
}
fl.add(f);
}
if (type == MarkupType.HTML) {
out.append("Command Helper uses a language called MethodScript, which greatly extend the capabilities of the plugin, " + "and make the plugin a fully " + "<a href=\"http://en.wikipedia.org/wiki/Turing_Complete\">Turing Complete</a> language. " + "There are several functions defined, and they are grouped into \"classes\". \n");
} else if (type == MarkupType.WIKI) {
out.append("Command Helper uses a language called MethodScript, which greatly extend the capabilities of the plugin, " + "and make the plugin a fully " + "[http://en.wikipedia.org/wiki/Turing_Complete Turing Complete] language. " + "There are several functions defined, and they are grouped into \"classes\". \n");
out.append("<p>Each function has its own page for documentation, where you can view examples for how to use a" + " particular function.\n");
} else if (type == MarkupType.TEXT) {
out.append("Command Helper uses a language called MethodScript, which greatly extend the capabilities of the plugin, " + "and make the plugin a fully " + "Turing Complete language [http://en.wikipedia.org/wiki/Turing_Complete].\n" + "There are several functions defined, and they are grouped into \"classes\".\n");
}
List<Map.Entry<Class, ArrayList<FunctionBase>>> entrySet = new ArrayList<Map.Entry<Class, ArrayList<FunctionBase>>>(functionlist.entrySet());
Collections.sort(entrySet, new Comparator<Map.Entry<Class, ArrayList<FunctionBase>>>() {
@Override
public int compare(Map.Entry<Class, ArrayList<FunctionBase>> o1, Map.Entry<Class, ArrayList<FunctionBase>> o2) {
return o1.getKey().getName().compareTo(o2.getKey().getName());
}
});
int total = 0;
int workingExamples = 0;
for (Map.Entry<Class, ArrayList<FunctionBase>> entry : entrySet) {
Class apiClass = entry.getKey();
String className = apiClass.getName().split("\\.")[apiClass.getName().split("\\.").length - 1];
if (className.equals("Sandbox")) {
// Skip Sandbox functions
continue;
}
String classDocs = null;
try {
Method m = apiClass.getMethod("docs", (Class[]) null);
Object o = null;
if ((m.getModifiers() & Modifier.STATIC) == 0) {
try {
o = apiClass.newInstance();
} catch (InstantiationException ex) {
}
}
classDocs = (String) m.invoke(o, (Object[]) null);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex) {
} catch (Exception e) {
e.printStackTrace(StreamUtils.GetSystemErr());
StreamUtils.GetSystemErr().println("Continuing however.");
}
StringBuilder intro = new StringBuilder();
if (type == MarkupType.HTML) {
if (className != null) {
intro.append("<h1>").append(className).append("</h1>" + "\n");
intro.append(classDocs == null ? "" : classDocs).append("\n");
} else {
intro.append("<h1>Other Functions</h1>" + "\n");
}
intro.append("<table>" + "\n");
} else if (type == MarkupType.WIKI) {
if (className != null) {
intro.append("===").append(className).append("===" + "\n");
intro.append(classDocs == null ? "" : classDocs).append("\n");
} else {
intro.append("===Other Functions===" + "\n");
}
intro.append("{| width=\"100%\" cellspacing=\"1\" cellpadding=\"1\" border=\"1\" class=\"wikitable\"\n" + "|-\n" + "! scope=\"col\" width=\"6%\" | Function Name\n" + "! scope=\"col\" width=\"5%\" | Returns\n" + "! scope=\"col\" width=\"10%\" | Arguments\n" + "! scope=\"col\" width=\"10%\" | Throws\n" + "! scope=\"col\" width=\"61%\" | Description\n" + "! scope=\"col\" width=\"3%\" | Since\n" + "! scope=\"col\" width=\"5%\" | Restricted" + "\n");
} else if (type == MarkupType.TEXT) {
intro.append("\n").append(className).append("\n");
intro.append("**********************************************************************************************" + "\n");
if (className != null) {
intro.append(classDocs == null ? "" : classDocs).append("\n");
} else {
intro.append("Other Functions" + "\n");
}
intro.append("**********************************************************************************************" + "\n");
}
List<FunctionBase> documentableFunctions = new ArrayList<FunctionBase>();
for (FunctionBase f : entry.getValue()) {
if (f.appearInDocumentation()) {
documentableFunctions.add(f);
}
}
if (!documentableFunctions.isEmpty()) {
out.append(intro.toString() + "\n");
}
Collections.sort(documentableFunctions, new Comparator<FunctionBase>() {
@Override
public int compare(FunctionBase o1, FunctionBase o2) {
return o1.getName().compareTo(o2.getName());
}
});
for (FunctionBase f : documentableFunctions) {
total++;
String doc = f.docs();
String restricted = (f instanceof Function && ((Function) f).isRestricted()) ? "<div style=\"background-color: red; font-weight: bold; text-align: center;\">Yes</div>" : "<div style=\"background-color: green; font-weight: bold; text-align: center;\">No</div>";
StringBuilder thrown = new StringBuilder();
if (f instanceof Function && ((Function) f).thrown() != null) {
List<Class<? extends CREThrowable>> thrownList = Arrays.asList(((Function) f).thrown());
for (int i = 0; i < thrownList.size(); i++) {
String t = ((Class<? extends CREThrowable>) thrownList.get(i)).getAnnotation(typeof.class).value();
if (type == MarkupType.HTML || type == MarkupType.TEXT) {
if (i != 0) {
thrown.append((type == MarkupType.HTML ? "<br />\n" : " | "));
}
thrown.append(t);
} else {
if (i != 0) {
thrown.append("<br />\n");
}
thrown.append("[[CommandHelper/Exceptions#").append(t).append("|").append(t).append("]]");
}
}
}
String since = (f instanceof Documentation ? ((Documentation) f).since().toString() : "0.0.0");
DocInfo di = new DocInfo(doc);
boolean hasExample = false;
if (f instanceof Function && ((Function) f).examples() != null && ((Function) f).examples().length > 0) {
hasExample = true;
workingExamples++;
}
if (di.ret == null || di.args == null || di.desc == null) {
out.append(f.getName() + "'s documentation is not correctly formatted. Please check it and try again.\n");
}
if (type == MarkupType.HTML) {
out.append("<tr><td>" + di.ret + "</td><td>" + di.args + "</td><td>" + thrown.toString() + "</td><td>" + di.desc + "</td><td>" + since + "</td><td>" + restricted + "</td></tr>\n");
} else if (type == MarkupType.WIKI) {
// Turn args into a prettified version
out.append("|- id=\"" + f.getName() + "\"\n" + "! scope=\"row\" | [[CommandHelper/" + (staged ? "Staged/" : "") + "API/" + f.getName() + "|" + f.getName() + "]]()\n" + "| " + di.ret + "\n" + "| " + di.args + "\n" + "| " + thrown.toString() + "\n" + "| " + (di.topDesc != null ? di.topDesc + " [[CommandHelper/" + (staged ? "Staged/" : "") + "API/" + f.getName() + "#Description|See More...]]" : di.desc) + (hasExample ? "<br />([[CommandHelper/" + (staged ? "Staged/" : "") + "API/" + f.getName() + "#Examples|Examples...]])" : "") + "\n" + "| " + since + "\n" + "| " + restricted + "\n");
} else if (type == MarkupType.TEXT) {
out.append(di.ret + " " + f.getName() + "(" + di.args + ")" + " {" + thrown.toString() + "}\n\t" + di.desc + "\n\t" + since + ((f instanceof Function ? ((Function) f).isRestricted() : false) ? "\n\tThis function is restricted" : "\n\tThis function is not restricted\n"));
}
}
if (!documentableFunctions.isEmpty()) {
if (type == MarkupType.HTML) {
out.append("</table>\n");
} else if (type == MarkupType.WIKI) {
out.append("|}\n{{Back to top}}\n");
} else if (type == MarkupType.TEXT) {
out.append("\n");
}
}
}
if (type == MarkupType.HTML) {
out.append("" + "<h2>Errors in documentation</h2>\n" + "<em>Please note that this documentation is generated automatically," + " if you notice an error in the documentation, please file a bug report for the" + " plugin itself!</em>" + "<div style='text-size:small; text-decoration:italics; color:grey'>There are " + total + " functions in this API page</div>\n");
} else if (type == MarkupType.WIKI) {
out.append("" + "===Errors in documentation===\n" + "''Please note that this documentation is generated automatically," + " if you notice an error in the documentation, please file a bug report for the" + " plugin itself!'' For information on undocumented functions, see [[CommandHelper/Sandbox|this page]]" + "<div style='font-size:xx-small; font-style:italic; color:grey'>There are " + total + " functions in this API page, " + workingExamples + " of which" + " have examples.</div>\n\n{{Back to top}}\n{{LearningTrail}}\n");
}
return out.toString();
}
use of com.laytonsmith.core.exceptions.CRE.CREThrowable in project CommandHelper by EngineHub.
the class SiteDeploy method deployAPI.
private void deployAPI() {
generateQueue.submit(new Runnable() {
@Override
public void run() {
try {
Set<Class<? extends Function>> functionClasses = new TreeSet<>(new Comparator<Class<? extends Function>>() {
@Override
public int compare(Class<? extends Function> o1, Class<? extends Function> o2) {
Function f1 = ReflectionUtils.instantiateUnsafe(o1);
Function f2 = ReflectionUtils.instantiateUnsafe(o2);
return f1.getName().compareTo(f2.getName());
}
});
functionClasses.addAll(ClassDiscovery.getDefaultInstance().loadClassesWithAnnotationThatExtend(api.class, Function.class));
// A map of where it maps the enclosing class to the list of function rows, which contains a list of
// table cells.
Map<Class<?>, List<List<String>>> data = new TreeMap<>(new Comparator<Class<?>>() {
@Override
public int compare(Class<?> o1, Class<?> o2) {
return o1.getCanonicalName().compareTo(o2.getCanonicalName());
}
});
List<String> hiddenFunctions = new ArrayList<>();
for (Class<? extends Function> functionClass : functionClasses) {
if (!data.containsKey(functionClass.getEnclosingClass())) {
data.put(functionClass.getEnclosingClass(), new ArrayList<List<String>>());
}
List<List<String>> d = data.get(functionClass.getEnclosingClass());
List<String> c = new ArrayList<>();
// function name, returns, arguments, throws, description, restricted
final Function f;
try {
f = ReflectionUtils.instantiateUnsafe(functionClass);
} catch (ReflectionUtils.ReflectionException ex) {
throw new RuntimeException("While trying to construct " + functionClass + ", got the following", ex);
}
final DocGen.DocInfo di = new DocGen.DocInfo(f.docs());
// If the function is hidden, we don't want to put it on the main page by default. Regardless,
// we do want to generate the function page, it will just remain unlinked.
generateQueue.submit(new Runnable() {
@Override
public void run() {
generateFunctionDocs(f, di);
}
});
if (f.since().equals(CHVersion.V0_0_0)) {
// Don't add these
continue;
}
if (f.getClass().getAnnotation(hide.class) != null) {
hiddenFunctions.add(f.getName());
}
c.add("[[API/functions/" + f.getName() + "|" + f.getName() + "]]()");
c.add(di.ret);
c.add(di.args);
List<String> exc = new ArrayList<>();
if (f.thrown() != null) {
for (Class<? extends CREThrowable> e : f.thrown()) {
CREThrowable ct = ReflectionUtils.instantiateUnsafe(e);
exc.add("{{object|" + ct.getName() + "}}");
}
}
c.add(StringUtils.Join(exc, "<br>"));
StringBuilder desc = new StringBuilder();
desc.append(HTMLUtils.escapeHTML(di.desc));
if (di.extendedDesc != null) {
desc.append(" [[functions/").append(f.getName()).append("|See more...]]<br>\n");
}
try {
if (f.examples() != null && f.examples().length > 0) {
desc.append("<br>([[API/functions/").append(f.getName()).append("#Examples|Examples...]])\n");
}
} catch (ConfigCompileException | NoClassDefFoundError ex) {
Logger.getLogger(SiteDeploy.class.getName()).log(Level.SEVERE, null, ex);
}
c.add(desc.toString());
c.add("<span class=\"api_" + (f.isRestricted() ? "yes" : "no") + "\">" + (f.isRestricted() ? "Yes" : "No") + "</span>");
d.add(c);
}
// data is now constructed.
StringBuilder b = new StringBuilder();
b.append("<ul id=\"TOC\">");
for (Class<?> clazz : data.keySet()) {
b.append("<li><a href=\"#").append(clazz.getSimpleName()).append("\">").append(clazz.getSimpleName()).append("</a></li>");
}
b.append("</ul>\n");
for (Map.Entry<Class<?>, List<List<String>>> e : data.entrySet()) {
Class<?> clazz = e.getKey();
List<List<String>> clazzData = e.getValue();
if (clazzData.isEmpty()) {
// if all the class's functions are hidden with @hide.
continue;
}
try {
b.append("== ").append(clazz.getSimpleName()).append(" ==\n");
String docs = (String) ReflectionUtils.invokeMethod(clazz, null, "docs");
b.append("<div>").append(docs).append("</div>\n\n");
b.append("{|\n|-\n");
b.append("! scope=\"col\" width=\"6%\" | Function Name\n" + "! scope=\"col\" width=\"5%\" | Returns\n" + "! scope=\"col\" width=\"10%\" | Arguments\n" + "! scope=\"col\" width=\"10%\" | Throws\n" + "! scope=\"col\" width=\"64%\" | Description\n" + "! scope=\"col\" width=\"5%\" | <span class=\"abbr\" title=\"Restricted\">Res</span>\n");
for (List<String> row : clazzData) {
b.append("|-");
if (hiddenFunctions.contains(row.get(0))) {
b.append(" class=\"hiddenFunction\"");
}
b.append("\n");
for (String cell : row) {
b.append("| ").append(cell).append("\n");
}
}
b.append("|}\n");
b.append("<p><a href=\"#TOC\">Back to top</a></p>\n");
} catch (Error ex) {
Logger.getLogger(SiteDeploy.class.getName()).log(Level.SEVERE, "While processing " + clazz + " got:", ex);
}
}
b.append("<div><a href=\"#\" id=\"showHidden\">Show hidden</a></div>");
b.append("<script type=\"text/javascript\">\n" + "pageRender.then(function() {\n" + "$('#showHidden').click(function(event){\n" + "$('.hiddenFunction').removeClass('hiddenFunction');\n" + "$('#showHidden').remove();\n" + "event.preventDefault();\nreturn false;\n" + "});\n" + "});\n" + "</script>");
writePage("API", b.toString(), "API.html", Arrays.asList(new String[] { "API", "functions" }), "A list of all " + Implementation.GetServerType().getBranding() + " functions");
currentGenerateTask.addAndGet(1);
} catch (Error ex) {
ex.printStackTrace(System.err);
}
}
});
totalGenerateTasks.addAndGet(1);
}
Aggregations