diff options
author | Remko Tronçon <git@el-tramo.be> | 2013-12-30 11:21:45 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2014-01-03 11:09:06 (GMT) |
commit | 26bb5aa9e2f520c3c943797e6143c32e5b16806b (patch) | |
tree | 0caa41938acf53da946847f8803c62e579525af9 /Sluift/GenerateDocumentation.lua | |
parent | 0b19dc7292b7672c9fbb711a411c392bc5b2bb34 (diff) | |
download | swift-26bb5aa9e2f520c3c943797e6143c32e5b16806b.zip swift-26bb5aa9e2f520c3c943797e6143c32e5b16806b.tar.bz2 |
Sluift: Add help support
Provide a 'help' function that takes a table/function, and prints help
for it. A structured representation can be retrieved through 'get_help'.
Change-Id: I2b3ce8992943ef30cee2604fba9200feed263fa5
Diffstat (limited to 'Sluift/GenerateDocumentation.lua')
-rw-r--r-- | Sluift/GenerateDocumentation.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Sluift/GenerateDocumentation.lua b/Sluift/GenerateDocumentation.lua new file mode 100644 index 0000000..69693ea --- /dev/null +++ b/Sluift/GenerateDocumentation.lua @@ -0,0 +1,90 @@ +--[[ + Copyright (c) 2013 Remko Tronçon + Licensed under the GNU General Public License. + See the COPYING file for more information. +--]] + +require "sluift" + +local function get_anchor(...) + return table.concat({...}, "-") +end + +local function convert_links(text) + result = text:gsub("(@{(%w*)})", "[`%2`](#%2)") + return result +end + +local function add_help(help, document, class, level) + -- Collect help of children + local methods = {} + for _, method in pairs(help.methods or {}) do + local description + local method_help = sluift.get_help(method.ref) + if method_help and method_help.description then + description = method_help.synopsis + end + methods[#methods+1] = { name = method.name, description = description, ref = method.ref } + end + local fields = sluift.copy(help.fields or {}) + + table.sort(methods, function (a, b) return (a.name or "") < (b.name or "") end) + table.sort(fields, function (a, b) return (a.name or "") < (b.name or "") end) + + local classes = {} + for _, class in pairs(help.classes or {}) do + classes[#classes+1] = { name = class, description = sluift.get_help(class).synopsis } + end + + table.insert(document, convert_links(help.description or "")) + for _, p in ipairs({ + {"Parameters", help.parameters}, {"Options", help.options}, {"Fields", fields}, {"Methods", methods}}) do + if p[2] and next(p[2]) ~= nil then + table.insert(document, "\n\n" .. level .. " " .. p[1] .. "\n") + for _, parameter in ipairs(p[2]) do + parameter_name = "`" .. parameter.name .. "`" + if p[1] == "Methods" then + parameter_name = "[" .. parameter_name .. "](#" .. get_anchor(class, parameter.name) .. ")" + end + if parameter.description then + table.insert(document, "- " .. parameter_name .. ": " .. convert_links(parameter.description)) + else + table.insert(document, "- " .. parameter_name) + end + end + if p[1] == "Methods" then + for _, method in ipairs(p[2]) do + table.insert(document, "\n#### <a name=\"" .. get_anchor(class, method.name) .. "\"></a> `" .. method.name .. "`\n") + if method.ref then + add_help(sluift.get_help(method.ref) or {}, document, class, level .. "#") + end + end + end + end + end +end + +local document = {} + +table.insert(document, [[ +# Sluift + +This document describes the API of the `sluift` module. + +The entry points of Sluift are in the `sluift` module, described below. + +## `sluift` module +]]) + +help = sluift.get_help(sluift) +add_help(help, document, "sluift", "###") +for _, class in pairs(help.classes) do + class_help = sluift.get_help(class) + if class_help then + table.insert(document, "\n## <a name=\"" .. class .. "\"></a> `" .. class .. "` class\n") + add_help(class_help, document, class, "###") + end +end + +document = table.concat(document, "\n") .. "\n" +print(document) |