Skip to content

Instantly share code, notes, and snippets.

@TheUltDev
Created July 26, 2013 22:57
Show Gist options
  • Save TheUltDev/6092805 to your computer and use it in GitHub Desktop.
Save TheUltDev/6092805 to your computer and use it in GitHub Desktop.
function parseLuaDoc()
function parseParameters(str)
local params = {}
local pattern = "%-%-%s@param%s(.-)%s(.-)\n"
i = 1
while(true)do
local iStart, iEnd, sName, sDesc = string.find(str, pattern, i)
if(not iStart)then break end
sOptional = (string.find(sDesc, "optional;")) and true or false
table.insert(params, {name=sName, value=sDesc, optional=sOptional})
i = iEnd + 1
end
return params
end
local tbl = {}
local pattern = "%-%-%-%s(.-)%.+\n+" .. -- title
"%-%-%s(.-)\n+" .. -- description
"%-%-%s@class%s(%w+)\n+" .. -- class
"(.-)" .. -- parameters
"%-%-%s@return%s(%w+)%s(.-)\n+" .. -- return type & desc
"([^%=%(\n]+)" -- function name
local file = assert(io.open("C:\\Program Files (x86)\\XenoBot\\Data\\XenoLuaLib.lua", "r"))
local contents = file:read("*all")
file:close()
i = 1
while(true)do
local iStart, iEnd, sTitle, sDesc, sClass, sParameters, sRetType, sRet, sFuncName = string.find(contents, pattern, i)
if(not iStart)then break end
aParameters = {}
if(sParameters ~= '')then
aParameters = parseParameters(sParameters)
end
sFuncName = string.gsub(sFuncName, "function ", "", 1):gsub("^%s*(.-)%s*$", "%1")
table.insert(tbl, {
class = sClass,
title = sTitle,
name = sFuncName,
params = aParameters,
desc = sDesc,
retType = sRetType,
retDesc = sRet
})
i = iEnd + 1
end
return tbl
end
function exportLuaDoc(data)
OVERVIEW_HEADER = [[ [QUOTE]
[TABLE="class: grid, width: 100%, align: center"]
[TR]
[TD][B][COLOR=#000000][SIZE=2]Function[/SIZE][/COLOR][/B][/TD]
[TD][B][COLOR=#000000][SIZE=2]Description[/SIZE][/COLOR][/B][/TD]
[/TR]
]]
OVERVIEW_TEMPLATE = [[
[TR]
[TD][B][COLOR="#008000"]%s[/COLOR][/B]([COLOR="#FF8C00"]%s[/COLOR])[/TD]
[TD]%s[/TD]
[/TR]
]]
OVERVIEW_FOOTER = [[ [/TABLE] [/QUOTE] ]]
local output = ""
for i = 1, #data do
local func = data[i]
local param = ""
if(#func.params > 0)then
local tmp = {}
for i = 1, #func.params do
table.insert(tmp, func.params[i].name)
end
param = table.concat(tmp, ",")
end
output = output .. string.format(OVERVIEW_TEMPLATE, func.name, param, func.desc)
end
local file = assert(io.open(os.getenv('USERPROFILE') .. "\\Documents\\Xenobot\\XenoLuaDoc.txt", "w+"))
file:write(OVERVIEW_HEADER, output, OVERVIEW_FOOTER)
file:flush()
file:close()
end
exportLuaDoc(parseLuaDoc())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment