* luci/contrib: added support for multiple modules per file in luadoc

This commit is contained in:
Jo-Philipp Wich 2008-08-09 16:01:31 +00:00
parent bdb4bbde13
commit e4f0c97551

View file

@ -151,7 +151,7 @@ end
-- @param block block with comment field -- @param block block with comment field
-- @return block parameter -- @return block parameter
local function parse_comment (block, first_line) local function parse_comment (block, first_line, modulename)
-- get the first non-empty line of code -- get the first non-empty line of code
local code = table.foreachi(block.code, function(_, line) local code = table.foreachi(block.code, function(_, line)
@ -215,7 +215,11 @@ local function parse_comment (block, first_line)
block.summary = parse_summary(block.description) block.summary = parse_summary(block.description)
assert(string.sub(block.description, 1, 1) ~= " ", string.format("`%s'", block.description)) assert(string.sub(block.description, 1, 1) ~= " ", string.format("`%s'", block.description))
return block if block.name and block.class == "module" then
modulename = block.name
end
return block, modulename
end end
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -241,7 +245,7 @@ local function parse_block (f, line, modulename, first)
line, block.code, modulename = parse_code(f, line, modulename) line, block.code, modulename = parse_code(f, line, modulename)
-- parse information in block comment -- parse information in block comment
block = parse_comment(block, first) block, modulename = parse_comment(block, first, modulename)
return line, block, modulename return line, block, modulename
else else
@ -252,7 +256,7 @@ local function parse_block (f, line, modulename, first)
-- reached end of file -- reached end of file
-- parse information in block comment -- parse information in block comment
block = parse_comment(block, first) block, modulename = parse_comment(block, first, modulename)
return line, block, modulename return line, block, modulename
end end
@ -263,24 +267,37 @@ end
-- @param doc table with documentation -- @param doc table with documentation
-- @return table with documentation -- @return table with documentation
function parse_file (filepath, doc) function parse_file (filepath, doc, handle, prev_line, prev_block, prev_modname)
local blocks = {} local blocks = { prev_block }
local modulename = nil local modulename = prev_modname
-- read each line -- read each line
local f = io.open(filepath, "r") local f = handle or io.open(filepath, "r")
local i = 1 local i = 1
local line = f:read() local line = prev_line or f:read()
local first = true local first = true
while line ~= nil do while line ~= nil do
if string.find(line, "^[\t ]*%-%-%-") then if string.find(line, "^[\t ]*%-%-%-") then
-- reached a luadoc block -- reached a luadoc block
local block local block, newmodname
line, block, modulename = parse_block(f, line, modulename, first) line, block, newmodname = parse_block(f, line, modulename, first)
if modulename and newmodname and newmodname ~= modulename then
doc = parse_file( nil, doc, f, line, block, newmodname )
else
table.insert(blocks, block) table.insert(blocks, block)
modulename = newmodname
end
else else
-- look for a module definition -- look for a module definition
modulename = check_module(line, modulename) local newmodname = check_module(line, modulename)
if modulename and newmodname and newmodname ~= modulename then
parse_file( nil, doc, f )
else
modulename = newmodname
end
-- TODO: keep beginning of file somewhere -- TODO: keep beginning of file somewhere
@ -289,7 +306,12 @@ function parse_file (filepath, doc)
first = false first = false
i = i + 1 i = i + 1
end end
if not handle then
f:close() f:close()
end
if filepath then
-- store blocks in file hierarchy -- store blocks in file hierarchy
assert(doc.files[filepath] == nil, string.format("doc for file `%s' already defined", filepath)) assert(doc.files[filepath] == nil, string.format("doc for file `%s' already defined", filepath))
table.insert(doc.files, filepath) table.insert(doc.files, filepath)
@ -309,10 +331,12 @@ function parse_file (filepath, doc)
doc.files[filepath].release = first.release doc.files[filepath].release = first.release
doc.files[filepath].summary = first.summary doc.files[filepath].summary = first.summary
end end
end
-- if module definition is found, store in module hierarchy -- if module definition is found, store in module hierarchy
if modulename ~= nil then if modulename ~= nil then
if modulename == "..." then if modulename == "..." then
assert( filepath, "Can't determine name for virtual module from filepatch" )
modulename = string.gsub (filepath, "%.lua$", "") modulename = string.gsub (filepath, "%.lua$", "")
modulename = string.gsub (modulename, "/", ".") modulename = string.gsub (modulename, "/", ".")
end end
@ -381,6 +405,7 @@ function parse_file (filepath, doc)
end end
end end
if filepath then
-- make functions table -- make functions table
doc.files[filepath].functions = {} doc.files[filepath].functions = {}
for f in class_iterator(blocks, "function")() do for f in class_iterator(blocks, "function")() do
@ -398,6 +423,7 @@ function parse_file (filepath, doc)
doc.files[filepath].tables[t.name] = t doc.files[filepath].tables[t.name] = t
end end
end end
end
return doc return doc
end end