Skip to content

Instantly share code, notes, and snippets.

@jucor
Created January 16, 2013 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jucor/4547203 to your computer and use it in GitHub Desktop.
Save jucor/4547203 to your computer and use it in GitHub Desktop.
-- global helpers
function string:split(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
-- vars
local res = io.popen("find " .. table.concat({...}, ' ') .. " -type f")
local target = string.split(res:read '*a', "\n")
local atarget = {}
local tests = {
"%-%- (%w+): (.+)",
"%-%- (%w+) %- (.+)",
"%-%- Examples",
"%-%- (.+)",
"%-%- Returns (.-)%.",
"function (%w*)%:?([%_%w]+)"
}
local results = {}
local files = {}
-- close handle
res:close()
-- prevent unwanted files
for i,v in ipairs(target) do
if v:match '%w-%.lua$' and not v:match 'middleclass' and not v:match 'init.lua' then
table.insert(atarget, v)
end
end
-- parse files
for _,v in ipairs(atarget) do
local it = 1
local res = {}
results[v] = res
for a in io.lines(v) do
if a ~= '--' then
for it = 1, #tests do
local test = tests[it]
local b
if test then
b = {a:match(test)}
end
if it == 1 and b and b[1] then
table.insert(res, {
scope = b[1],
desc = b[2],
args = {},
argdes = {},
example = {},
ret = '',
class = '',
name = ''
})
elseif it == 2 and b and b[1] then
table.insert(res[#res].args, b[1])
table.insert(res[#res].argdes, b[2])
elseif it == 4 and b and b[1] then
for i,v in ipairs(b) do
table.insert(res[#res].example, v)
end
elseif it == 5 and b[1] then
res[#res].ret = b[1]
elseif it == 6 and (b[1] or b[2]) then
local t = res[#res]
t.class, t.name = unpack(b)
elseif b and b[1] then
for i,v in ipairs(b) do
table.insert(res[#res], v)
end
end
end
end
end
end
-- generate markdown
for k,vv in pairs(results) do
-- outputs class name and line rule, usage suggested
--local contents = "## " .. vv[#vv].class
--contents = contents .. "\n* * *\n\n"
for k,v in pairs(vv) do
if v.scope == 'Internal' then
contents = contents .. "### " .. (v.name or "Undefined") .. "(" .. table.concat(v.args or {}, ", ") .. ")\n"
else
contents = contents .. "### " .. (v.class or "Undefined") .. ":" .. (v.name or "Undefined") .. "(" .. table.concat(v.args or {}, ", ") .. ")\n"
end
contents = contents .. (v.desc or "Description not given.") .. "\n"
if v.args and #v.args > 0 then
contents = contents .. "\n"
for iii,vvv in ipairs(v.args) do
contents = contents .. "* " .. (vvv .. ' - ' .. v.argdes[iii]) .. "\n"
end
end
if v.example and #v.example > 0 then
contents = contents .. "\nExamples\n\n"
contents = contents .. (" " .. table.concat(v.example, "\n ")) .. "\n"
end
contents = contents .. "\n"
contents = contents .. ("Returns " .. (v.ret ~= "" and v.ret or "nil.")) .. "\n"
contents = contents .. "\n"
end
files[k] = contents
end
-- output markdown
os.execute 'mkdir doc'
for k,v in pairs(files) do
local file = io.open('doc/' .. k:match('(%w-)%.lua') .. '.mkd', "w")
file:write(v)
file:close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment