Improved the Lua documentation

- Lua documentation now needs a string giving the arguments list.
- When generating documentation first prints the command list and then prints
  the documentation for each command.
- Supports fixed max line length (set to 80)
- Uses four blank spaces as padding (easily replacable)
This commit is contained in:
Jonas Strandstedt
2014-12-03 14:13:53 +01:00
parent fc6fbf1587
commit a5e122b822
7 changed files with 113 additions and 58 deletions

View File

@@ -382,43 +382,50 @@ void ScriptEngine::addBaseLibrary() {
{
"printDebug",
&luascriptfunctions::printDebug,
"printDebug(*): Logs the passed value to the installed LogManager with a "
"*",
"Logs the passed value to the installed LogManager with a "
"LogLevel of 'Debug'"
},
{
"printInfo",
&luascriptfunctions::printInfo,
"printInfo(*): Logs the passed value to the installed LogManager with a "
"*",
"Logs the passed value to the installed LogManager with a "
" LogLevel of 'Info'"
},
{
"printWarning",
&luascriptfunctions::printWarning,
"printWarning(*): Logs the passed value to the installed LogManager with "
"*",
"Logs the passed value to the installed LogManager with "
"a LogLevel of 'Warning'"
},
{
"printError",
&luascriptfunctions::printError,
"printError(*): Logs the passed value to the installed LogManager with a "
"*",
"Logs the passed value to the installed LogManager with a "
"LogLevel of 'Error'"
},
{
"printFatal",
&luascriptfunctions::printFatal,
"printFatal(*): Logs the passed value to the installed LogManager with a "
"*",
"Logs the passed value to the installed LogManager with a "
"LogLevel of 'Fatal'"
},
{
"absPath",
&luascriptfunctions::absolutePath,
"absPath(string): Returns the absolute path to the passed path, resolving"
"string",
"Returns the absolute path to the passed path, resolving"
" path tokens as well as resolving relative paths"
},
{
"setPathToken",
&luascriptfunctions::setPathToken,
"setPathToken(string, string): Registers a new path token provided by the"
"string, string",
"Registers a new path token provided by the"
" first argument to the path provided in the second argument"
}
}
@@ -493,41 +500,70 @@ bool ScriptEngine::writeDocumentation(const std::string& filename, const std::st
LDEBUG("Writing Lua documentation of type '" << type <<
"' to file '" << filename << "'");
std::ofstream file(filename);
if (file.good()) {
auto concatenate = [](std::string library, std::string function) {
std::string total = "openspace.";
if (!library.empty())
total += std::move(library) + ".";
total += std::move(function);
return std::move(total);
};
// First iterate over all libraries and functions to find the longest
// combination so that can be used as the 'width' parameter for the output
// stream
size_t maxLength = 0;
for (auto library : _registeredLibraries) {
for (auto function : library.functions) {
std::string functionName = concatenate(library.name, function.name);
maxLength = std::max(maxLength, functionName.size());
}
}
maxLength += AdditionalSpace;
// Now write out the functions
for (auto library : _registeredLibraries) {
for (auto function : library.functions) {
std::string functionName = concatenate(library.name, function.name);
file << std::setw(maxLength) << std::left << functionName;
file << std::setw(0) << function.helpText << std::endl;
}
}
return true;
}
else {
if (!file.good()) {
LERROR("Could not open file '" << filename << "' for writing documentation");
return false;
}
auto concatenate = [](std::string library, std::string function) {
std::string total = "openspace.";
if (!library.empty())
total += std::move(library) + ".";
total += std::move(function);
return std::move(total);
};
// Settings
const unsigned int lineWidth = 80;
static const std::string whitespace = " \t";
static const std::string padding = " ";
const bool commandListArguments = true;
file << "Available commands:\n";
// Now write out the functions
for (auto library : _registeredLibraries) {
for (auto function : library.functions) {
std::string functionName = concatenate(library.name, function.name);
file << padding << functionName;
if (commandListArguments)
file << "(" << function.argumentText << ")";
file << std::endl;
}
}
file << std::endl;
// Now write out the functions definitions
for (auto library : _registeredLibraries) {
for (auto function : library.functions) {
std::string functionName = concatenate(library.name, function.name);
file << functionName << "(" << function.argumentText << "):" << std::endl;
std::string remainingHelptext = function.helpText;
while (!remainingHelptext.empty()) {
const auto length = remainingHelptext.length();
const auto paddingLength = padding.length();
if ((length + paddingLength) > lineWidth) {
auto lastSpace = remainingHelptext.find_last_of(whitespace, lineWidth - 1 - paddingLength);
if (lastSpace == remainingHelptext.npos)
lastSpace = lineWidth;
file << padding << remainingHelptext.substr(0, lastSpace) << std::endl;
auto firstNotSpace = remainingHelptext.find_first_not_of(whitespace, lastSpace);
if (firstNotSpace == remainingHelptext.npos)
firstNotSpace = lastSpace;
remainingHelptext = remainingHelptext.substr(firstNotSpace);
}
else {
file << padding << remainingHelptext << std::endl;
remainingHelptext = "";
}
}
file << std::endl;
}
}
return true;
}
else {
LERROR("Undefined type '" << type << "' for Lua documentation");