* luci/contrib: added support for multiple modules per file in luadoc
This commit is contained in:
parent
bdb4bbde13
commit
e4f0c97551
1 changed files with 110 additions and 84 deletions
|
@ -151,7 +151,7 @@ end
|
|||
-- @param block block with comment field
|
||||
-- @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
|
||||
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)
|
||||
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
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
@ -241,7 +245,7 @@ local function parse_block (f, line, modulename, first)
|
|||
line, block.code, modulename = parse_code(f, line, modulename)
|
||||
|
||||
-- parse information in block comment
|
||||
block = parse_comment(block, first)
|
||||
block, modulename = parse_comment(block, first, modulename)
|
||||
|
||||
return line, block, modulename
|
||||
else
|
||||
|
@ -252,7 +256,7 @@ local function parse_block (f, line, modulename, first)
|
|||
-- reached end of file
|
||||
|
||||
-- parse information in block comment
|
||||
block = parse_comment(block, first)
|
||||
block, modulename = parse_comment(block, first, modulename)
|
||||
|
||||
return line, block, modulename
|
||||
end
|
||||
|
@ -263,24 +267,37 @@ end
|
|||
-- @param doc table with documentation
|
||||
-- @return table with documentation
|
||||
|
||||
function parse_file (filepath, doc)
|
||||
local blocks = {}
|
||||
local modulename = nil
|
||||
function parse_file (filepath, doc, handle, prev_line, prev_block, prev_modname)
|
||||
local blocks = { prev_block }
|
||||
local modulename = prev_modname
|
||||
|
||||
-- read each line
|
||||
local f = io.open(filepath, "r")
|
||||
local f = handle or io.open(filepath, "r")
|
||||
local i = 1
|
||||
local line = f:read()
|
||||
local line = prev_line or f:read()
|
||||
local first = true
|
||||
while line ~= nil do
|
||||
|
||||
if string.find(line, "^[\t ]*%-%-%-") then
|
||||
-- reached a luadoc block
|
||||
local block
|
||||
line, block, modulename = parse_block(f, line, modulename, first)
|
||||
local block, newmodname
|
||||
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)
|
||||
modulename = newmodname
|
||||
end
|
||||
else
|
||||
-- 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
|
||||
|
||||
|
@ -289,7 +306,12 @@ function parse_file (filepath, doc)
|
|||
first = false
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
if not handle then
|
||||
f:close()
|
||||
end
|
||||
|
||||
if filepath then
|
||||
-- store blocks in file hierarchy
|
||||
assert(doc.files[filepath] == nil, string.format("doc for file `%s' already defined", filepath))
|
||||
table.insert(doc.files, filepath)
|
||||
|
@ -309,10 +331,12 @@ function parse_file (filepath, doc)
|
|||
doc.files[filepath].release = first.release
|
||||
doc.files[filepath].summary = first.summary
|
||||
end
|
||||
end
|
||||
|
||||
-- if module definition is found, store in module hierarchy
|
||||
if modulename ~= nil then
|
||||
if modulename == "..." then
|
||||
assert( filepath, "Can't determine name for virtual module from filepatch" )
|
||||
modulename = string.gsub (filepath, "%.lua$", "")
|
||||
modulename = string.gsub (modulename, "/", ".")
|
||||
end
|
||||
|
@ -381,6 +405,7 @@ function parse_file (filepath, doc)
|
|||
end
|
||||
end
|
||||
|
||||
if filepath then
|
||||
-- make functions table
|
||||
doc.files[filepath].functions = {}
|
||||
for f in class_iterator(blocks, "function")() do
|
||||
|
@ -398,6 +423,7 @@ function parse_file (filepath, doc)
|
|||
doc.files[filepath].tables[t.name] = t
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return doc
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue