1 -------------------------------------------------------------------------------
2 -- Doclet to format source code according to LuaDoc standard tags. This doclet
3 -- (re)write .lua files adding missing standard tags. Texts are formatted to
4 -- 80 columns and function parameters are added based on code analysis.
6 -- @release $Id: formatter.lua,v 1.5 2007/04/18 14:28:39 tomas Exp $
7 -------------------------------------------------------------------------------
9 local util = require "luadoc.util"
10 local assert, ipairs, pairs, type = assert, ipairs, pairs, type
11 local string = require"string"
12 local table = require"table"
14 module "luadoc.doclet.formatter"
20 -------------------------------------------------------------------------------
21 -- Assembly the output filename for an input file.
22 -- TODO: change the name of this function
23 function out_file (filename)
25 h = options.output_dir..h
29 -------------------------------------------------------------------------------
30 -- Generate a new lua file for each input lua file. If the user does not
31 -- specify a different output directory input files will be rewritten.
32 -- @param doc documentation table
38 for i, file_doc in ipairs(doc.files) do
39 -- assembly the filename
40 local filename = out_file(file_doc.name)
41 luadoc.logger:info(string.format("generating file `%s'", filename))
43 -- TODO: confirm file overwrite
44 local f = posix.open(filename, "w")
45 assert(f, string.format("could not open `%s' for writing", filename))
47 for _, block in ipairs(file_doc.doc) do
49 -- write reorganized comments
50 f:write(string.rep("-", 80).."\n")
53 f:write(util.comment(util.wrap(block.description, 77)))
56 if block.class == "function" then
58 table.foreachi(block.param, function (_, param_name)
59 f:write(util.comment(util.wrap(string.format("@param %s %s", param_name, block.param[param_name] or todo), 77)))
64 if type(block.ret) == "table" then
65 table.foreachi(block.ret, function (_, ret)
66 f:write(util.comment(util.wrap(string.format("@return %s", ret), 77)).."\n")
69 f:write(util.comment(util.wrap(string.format("@return %s", block.ret or todo), 77)).."\n")
77 for _, line in ipairs(block.code) do