1
0

APIDump: Added support for inheritance

This commit is contained in:
madmaxoft 2013-09-13 10:22:04 +02:00
parent 80bdd2284b
commit 1f1216d56d

@ -205,7 +205,7 @@ function DumpAPIHtml()
]]); ]]);
for i, cls in ipairs(API) do for i, cls in ipairs(API) do
f:write("<li><a href=\"" .. cls.Name .. ".html\">" .. cls.Name .. "</a></li>\n"); f:write("<li><a href=\"" .. cls.Name .. ".html\">" .. cls.Name .. "</a></li>\n");
WriteHtmlClass(cls); WriteHtmlClass(cls, API);
end end
f:write("</ul></body></html>"); f:write("</ul></body></html>");
f:close(); f:close();
@ -223,6 +223,7 @@ function ReadDescriptions(a_API)
local APIDesc = g_APIDesc.Classes[cls.Name]; local APIDesc = g_APIDesc.Classes[cls.Name];
if (APIDesc ~= nil) then if (APIDesc ~= nil) then
cls.Desc = APIDesc.Desc; cls.Desc = APIDesc.Desc;
cls.Inherits = APIDesc.Inherits;
if (APIDesc.Functions ~= nil) then if (APIDesc.Functions ~= nil) then
-- Assign function descriptions: -- Assign function descriptions:
@ -267,7 +268,7 @@ end
function WriteHtmlClass(a_ClassAPI) function WriteHtmlClass(a_ClassAPI, a_AllAPI)
local cf, err = io.open("API/" .. a_ClassAPI.Name .. ".html", "w"); local cf, err = io.open("API/" .. a_ClassAPI.Name .. ".html", "w");
if (cf == nil) then if (cf == nil) then
return; return;
@ -275,10 +276,49 @@ function WriteHtmlClass(a_ClassAPI)
local function LinkifyString(a_String) local function LinkifyString(a_String)
-- TODO: Make a link out of anything with the special linkifying syntax {{link|title}} -- TODO: Make a link out of anything with the special linkifying syntax {{link|title}}
-- a_String:gsub("{{([^|]*)|[^}]*}}", "<a href=\"%1\">%2</a>"); -- a_String:gsub("{{([^|]*)|([^}])*}}", "<a href=\"%1\">%2</a>");
return a_String; return a_String;
end end
-- Returns the ClassAPI for the inherited class, or nil if not found
local function FindInheritedClassAPI(a_AllAPI, a_InheritedClassName)
if (a_InheritedClassName == nil) then
return nil;
end
for i, cls in ipairs(a_AllAPI) do
if (cls.Name == a_InheritedClassName) then
return cls;
end
end
return nil;
end
-- Writes a table containing all functions in the specified list, with an optional "inherited from" header when a_InheritedName is valid
local function WriteFunctions(a_Functions, a_InheritedName)
if (#a_Functions == 0) then
return;
end
if (a_InheritedName ~= nil) then
cf:write("<h2>Functions inherited from " .. a_InheritedName .. "</h2>");
end
cf:write("<table><tr><th>Name</th><th>Parameters</th><th>Return value</th><th>Notes</th></tr>\n");
for i, func in ipairs(a_Functions) do
cf:write("<tr><td>" .. func.Name .. "</td>");
cf:write("<td>" .. LinkifyString(func.Params or "").. "</td>");
cf:write("<td>" .. LinkifyString(func.Return or "").. "</td>");
cf:write("<td>" .. LinkifyString(func.Notes or "") .. "</td></tr>\n");
end
cf:write("</table>\n");
end
-- Build an array of inherited classes chain:
local InheritanceChain = {};
local CurrInheritance = FindInheritedClassAPI(a_AllAPI, a_ClassAPI.Inherits);
while (CurrInheritance ~= nil) do
table.insert(InheritanceChain, CurrInheritance);
CurrInheritance = FindInheritedClassAPI(a_AllAPI, CurrInheritance.Inherits);
end
cf:write([[<html><head><title>MCServer API - ]] .. a_ClassAPI.Name .. [[</title> cf:write([[<html><head><title>MCServer API - ]] .. a_ClassAPI.Name .. [[</title>
<link rel="stylesheet" type="text/css" href="main.css" /> <link rel="stylesheet" type="text/css" href="main.css" />
</head><body> </head><body>
@ -287,45 +327,44 @@ function WriteHtmlClass(a_ClassAPI)
]]); ]]);
-- Write the table of contents: -- Write the table of contents:
if (#a_ClassAPI.Constants > 0) then if (a_ClassAPI.Inherits ~= nil) then
cf:write("<li><a href=\"#constants\">Constants</a></li>\n"); cf:write("<li><a href=\"#inherits\">Inheritance</a></li>\n");
end
if (#a_ClassAPI.Functions > 0) then
cf:write("<li><a href=\"#functions\">Functions</a></li>\n");
end end
cf:write("<li><a href=\"#constants\">Constants</a></li>\n");
cf:write("<li><a href=\"#functions\">Functions</a></li>\n");
cf:write("</ul>"); cf:write("</ul>");
-- Write the class description: -- Write the class description:
cf:write("<a name=\"desc\"><h1>" .. a_ClassAPI.Name .. "</h1></a>\n"); cf:write("<a name=\"desc\"><h1>" .. a_ClassAPI.Name .. " class</h1></a>\n");
if (a_ClassAPI.Desc ~= nil) then if (a_ClassAPI.Desc ~= nil) then
cf:write("<p>"); cf:write("<p>");
cf:write(a_ClassAPI.Desc); cf:write(a_ClassAPI.Desc);
cf:write("</p>\n"); cf:write("</p>\n");
end; end;
-- Write the constants: -- Write the inheritance, if available:
if (#a_ClassAPI.Constants > 0) then if (a_ClassAPI.Inherits ~= nil) then
cf:write("<a name=\"constants\"><h1>Constants</h1></a>\n"); cf:write("<a name=\"inherits\"><h1>Inheritance</h1></a>\n");
cf:write("<table><tr><th>Name</th><th>Value</th><th>Notes</th></tr>\n"); for i, cls in ipairs(InheritanceChain) do
for i, cons in ipairs(a_ClassAPI.Constants) do cf:write("<li><a href=\"" .. cls.Name .. ".html\">" .. cls.Name .. "</a></li>");
cf:write("<tr><td>" .. cons.Name .. "</td>");
cf:write("<td>" .. cons.Value .. "</td>");
cf:write("<td>" .. LinkifyString(cons.Notes or "") .. "</td></tr>\n");
end end
cf:write("</table>\n");
end end
-- Write the functions: -- Write the constants:
if (#a_ClassAPI.Functions > 0) then cf:write("<a name=\"constants\"><h1>Constants</h1></a>\n");
cf:write("<a name=\"functions\"><h1>Functions</h1></a>\n"); cf:write("<table><tr><th>Name</th><th>Value</th><th>Notes</th></tr>\n");
cf:write("<table><tr><th>Name</th><th>Parameters</th><th>Return value</th><th>Notes</th></tr>\n"); for i, cons in ipairs(a_ClassAPI.Constants) do
for i, func in ipairs(a_ClassAPI.Functions) do cf:write("<tr><td>" .. cons.Name .. "</td>");
cf:write("<tr><td>" .. func.Name .. "</td>"); cf:write("<td>" .. cons.Value .. "</td>");
cf:write("<td>" .. LinkifyString(func.Params or "").. "</td>"); cf:write("<td>" .. LinkifyString(cons.Notes or "") .. "</td></tr>\n");
cf:write("<td>" .. LinkifyString(func.Return or "").. "</td>"); end
cf:write("<td>" .. LinkifyString(func.Notes or "") .. "</td></tr>\n"); cf:write("</table>\n");
end
cf:write("</table>\n"); -- Write the functions, including the inherited ones:
cf:write("<a name=\"functions\"><h1>Functions</h1></a>\n");
WriteFunctions(a_ClassAPI.Functions, nil);
for i, cls in ipairs(InheritanceChain) do
WriteFunctions(cls.Functions, cls.Name);
end end
cf:write("</body></html>"); cf:write("</body></html>");