Permalink
Browse files

Add help command to interpreter, which is like the api cmdline tool

The api tool gave help information about functions, and now this
functionality is available directly within the interpreter. The
documentation itself has also been substantially improved, with support
for removing html tags, doing some wiki formatting, and formatting
tables in a more console friendly way. The underlying code is accessible
publicly through the Interpreter class, and may be used elsewhere in the
future.
  • Loading branch information...
LadyCailin committed Jan 30, 2019
1 parent 2efc14f commit 113705d5363fd3a8743f81d4d6939268ed7655f3
@@ -36,6 +36,15 @@ public static String unescapeHTML(String html) {
.replace(""", "\"")
.replace(">", ">")
.replace("&lt;", "<")
.replace("&nbsp;", " ")
.replace("&lpar;", "(")
.replace("&rpar;", ")")
.replace("&lcub;", "{")
.replace("&rcub;", "}")
.replace("&lsqb;", "[")
.replace("&rsqb;", "]")
.replace("&equals;", "=")
.replace("&num;", "#")
.replace("&amp;", "&");
}
}
@@ -26,8 +26,6 @@
import com.laytonsmith.core.exceptions.ConfigCompileGroupException;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import com.laytonsmith.core.extensions.ExtensionManager;
import com.laytonsmith.core.functions.ExampleScript;
import com.laytonsmith.core.functions.Function;
import com.laytonsmith.core.functions.FunctionBase;
import com.laytonsmith.core.functions.FunctionList;
import com.laytonsmith.core.functions.Scheduling;
@@ -645,42 +643,7 @@ public static void main(String[] args) throws Exception {
System.err.println("Could not launch browser");
}
} else {
DocGen.DocInfo di = new DocGen.DocInfo(f.docs());
String ret = di.ret.replaceAll("</?[a-z].*?>", "");
String args2 = di.args.replaceAll("</?[a-z].*?>", "");
String desc = (di.desc + (di.extendedDesc != null ? "\n\n" + di.extendedDesc : "")).replaceAll("</?[a-z].*?>", "");
StreamUtils.GetSystemOut().println(StringUtils.Join(new String[]{
function,
"Returns " + ret,
"Expects " + args2,
desc
}, " // "));
if(examples) {
System.out.println("\nExamples:\n");
ExampleScript[] ex = null;
if(f instanceof Function) {
ex = ((Function) f).examples();
}
if(ex == null || ex.length == 0) {
StreamUtils.GetSystemOut().println("This function doesn't have any examples :(");
} else {
for(int i = 0; i < ex.length; i++) {
if(i > 0) {
System.out.println("\n\n");
}
ExampleScript e = ex[i];
StreamUtils.GetSystemOut().println(TermColors.BRIGHT_WHITE + TermColors.BOLD
+ TermColors.UNDERLINE
+ "Example " + (i + 1)
+ TermColors.RESET);
StreamUtils.GetSystemOut().println(e.getDescription() + "\n");
StreamUtils.GetSystemOut().println(TermColors.UNDERLINE + "Code" + TermColors.RESET
+ "\n" + e.getScript() + "\n");
StreamUtils.GetSystemOut().println(TermColors.UNDERLINE + "Output" + TermColors.RESET
+ "\n" + e.getOutput());
}
}
}
StreamUtils.GetSystemOut().println(Interpreter.formatDocsForCmdline(f.getName(), examples));
}
System.exit(0);
} else if(mode == SYNTAX_MODE) {
@@ -95,6 +95,8 @@

/**
* Returns an array of example scripts, which are used for documentation purposes.
* <p>
* If there are no examples, null or empty array should be returned.
*
* @return
* @throws com.laytonsmith.core.exceptions.ConfigCompileException If the script could not be compiled
@@ -915,7 +915,8 @@ public String getName() {

@Override
public String docs() {
return "void {[...]} An operation that does nothing. Any arguments passed in are ignored entirely.";
return "void {[...]} An operation that does nothing. Any arguments passed in are ignored entirely, though"
+ " they will be evaluated first.";
}

@Override
@@ -94,22 +94,51 @@ public String docs() {
return "mixed {param, [args, ...]} Returns information about the runtime in a usable"
+ " format. Depending on the information returned, it may be useable directly,"
+ " or it may be more of a referential format. ---- The following items can be retrieved:"
+ "<table><tr><th>param</th><th>args</th><th>returns/description</th></tr>"
+ "<tr><td>label</td><td></td><td>Return the label that the script is currently running under</td></tr>"
+ "<tr><td>command</td><td></td><td>Returns the command that was used to fire off this script (if applicable)</td></tr>"
+ "<tr><td>varlist</td><td>[name]</td><td>Returns a list of currently in scope variables. If name"
+ " is provided, the currently set value is instead returned.</td></tr>"
+ "<tr><td>line_num</td><td></td><td>The current line number</td></tr>"
+ "<tr><td>file</td><td></td><td>The absolute path to the current file</td></tr>"
+ "<tr><td>col</td><td></td><td>The current column number</td></tr>"
+ "<tr><td>datasources</td><td></td><td>An array of data source protocols available</td></tr>"
+ "<tr><td>enum</td><td>[enum name]</td><td>An array of enum names, or if one is provided, a list of all"
+ " the values in that enum</td>"
+ "<tr><td>keywords</td><td>[keyword name]</td><td>Lists the keywords, if no parameter is provided, otherwise"
+ " provides the documentation for the specified keyword</td>"
+ "</tr>"
+ "</table>";
//+ "<tr><td></td><td></td><td></td></tr>"
+ "{|\n"
+ "|-\n"
+ "! param\n"
+ "! args\n"
+ "! returns/description\n"
+ "|-\n"
+ "| label\n"
+ "| \n"
+ "| Return the label that the script is currently running under\n"
+ "|-\n"
+ "| command\n"
+ "|\n"
+ "| Returns the command that was used to fire off this script (if applicable)\n"
+ "|-\n"
+ "| varlist\n"
+ "| [name]\n"
+ "| Returns a list of currently in scope variables. If name"
+ " is provided, the currently set value is instead returned.\n"
+ "|-\n"
+ "| line_num\n"
+ "|\n"
+ "| The current line number\n"
+ "|-\n"
+ "| file\n"
+ "|\n"
+ "| The absolute path to the current file\n"
+ "|-\n"
+ "| col\n"
+ "|\n"
+ "| The current column number\n"
+ "|-\n"
+ "| datasources\n"
+ "|\n"
+ "| An array of data source protocols available\n"
+ "|-\n"
+ "| enum\n"
+ "| [enum name]\n"
+ "| An array of enum names, or if one is provided, a list of all"
+ " the values in that enum\n"
+ "|-\n"
+ "| keywords\n"
+ "| [keyword name]\n"
+ "| Lists the keywords, if no parameter is provided, otherwise"
+ " provides the documentation for the specified keyword\n"
+ "|}";
}

@Override
@@ -586,14 +586,14 @@ public Version since() {
return new ExampleScript[]{
new ExampleScript("Getting headers from a website", "http_request('http://www.google.com', array(\n"
+ "\tsuccess: closure(@response,\n"
+ "\t\tmsg(@response['headers']['Server'][0])\n"
+ "\t\tmsg(@response['headers']['Server'][0]);\n"
+ "\t)\n"
+ "));\n", "gws"),
new ExampleScript("Using a cookie jar", "@cookiejar = array()\n"
+ "http_request('http://www.google.com', array(\n"
+ "\tcookiejar: @cookiejar, success: closure(@resp,\n"
+ "\t\tmsg(@cookiejar)\n"
+ "\t)\n"
+ "\tcookiejar: @cookiejar, success: closure(@resp) {\n"
+ "\t\tmsg(@cookiejar);\n"
+ "\t}\n"
+ "));\n", "<cookie jar would now have cookies in it>"),
new ExampleScript("Sending some json to the server",
"http_request('http://example.com', array(\n"
Oops, something went wrong.

0 comments on commit 113705d

Please sign in to comment.