diff options
author | 2020-03-29 22:42:54 -0400 | |
---|---|---|
committer | 2020-03-29 22:42:54 -0400 | |
commit | 99299f2de25c74503e0e1dcf6f43ea9f218c3371 (patch) | |
tree | cc96682bb89db8c76024956bb5326070384c5ef9 /modules | |
parent | 5dfb7d73e4e7ae23c0964d03ebc0453bea5f2455 (diff) |
Code reformatting.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/lua/tadoc.lua | 140 |
1 files changed, 74 insertions, 66 deletions
diff --git a/modules/lua/tadoc.lua b/modules/lua/tadoc.lua index 76b25e8b..45ebd76c 100644 --- a/modules/lua/tadoc.lua +++ b/modules/lua/tadoc.lua @@ -32,10 +32,10 @@ end -- @return string local function sanitize_markdown(s) return s:gsub('%[([^%]\r\n]+)%]%b[]', '%1') -- [foo][] - :gsub('%[([^%]\r\n]+)%]%b()', '%1') -- [foo](bar) - :gsub('\r?\n\r?\n%[([^%]\r\n]+)%]:[^\r\n]+', '') -- [foo]: bar - :gsub('\r?\n%[([^%]\r\n]+)%]:[^\r\n]+', '') -- [foo]: bar - :gsub('&([%a]+);', {quot = '"', apos = "'"}) + :gsub('%[([^%]\r\n]+)%]%b()', '%1') -- [foo](bar) + :gsub('\r?\n\r?\n%[([^%]\r\n]+)%]:[^\r\n]+', '') -- [foo]: bar + :gsub('\r?\n%[([^%]\r\n]+)%]:[^\r\n]+', '') -- [foo]: bar + :gsub('&([%a]+);', {quot = '"', apos = "'"}) end -- Writes a function or field apidoc. @@ -45,18 +45,19 @@ end local function write_apidoc(file, m, b) -- Function or field name. local name = b.name - if not name:find('[%.:]') then name = m.name..'.'..name end + if not name:find('[%.:]') then name = string.format('%s.%s', m.name, name) end -- Block documentation for the function or field. local doc = {} -- Function arguments or field type. local class = b.class local header = name if class == 'function' then - header = header..(b.param and '('..table.concat(b.param, ', ')..')' or '') + header = header .. + (b.param and string.format('(%s)', table.concat(b.param, ', ')) or '') elseif class == 'field' and b.description:find('^%s*%b()') then - header = header..' '..b.description:match('^%s*(%b())') + header = string.format('%s %s', header, b.description:match('^%s*(%b())')) elseif class == 'module' or class == 'table' then - header = header..' ('..class..')' + header = string.format('%s (%s)', header, class) end doc[#doc + 1] = header -- Function or field description. @@ -71,51 +72,54 @@ local function write_apidoc(file, m, b) -- Strip consistent leading whitespace. local indent indent, description = description:match('^(%s*)(.*)$') - if indent ~= '' then description = description:gsub('\n'..indent, '\n') end + if indent ~= '' then + description = description:gsub('\n' .. indent, '\n') + end end doc[#doc + 1] = sanitize_markdown(description) -- Function parameters (@param). if class == 'function' and b.param then for _, p in ipairs(b.param) do if b.param[p] and #b.param[p] > 0 then - doc[#doc + 1] = '@param '..p..' '..sanitize_markdown(b.param[p]) + doc[#doc + 1] = string.format( + '@param %s %s', p, sanitize_markdown(b.param[p])) end end end -- Function usage (@usage). if class == 'function' and b.usage then if type(b.usage) == 'string' then - doc[#doc + 1] = '@usage '..b.usage + doc[#doc + 1] = '@usage ' .. b.usage else - for _, u in ipairs(b.usage) do doc[#doc + 1] = '@usage '..u end + for _, u in ipairs(b.usage) do doc[#doc + 1] = '@usage ' .. u end end end -- Function returns (@return). if class == 'function' and b.ret then if type(b.ret) == 'string' then - doc[#doc + 1] = '@return '..b.ret + doc[#doc + 1] = '@return ' .. b.ret else - for _, u in ipairs(b.ret) do doc[#doc + 1] = '@return '..u end + for _, u in ipairs(b.ret) do doc[#doc + 1] = '@return ' .. u end end end -- See also (@see). if b.see then if type(b.see) == 'string' then - doc[#doc + 1] = '@see '..b.see + doc[#doc + 1] = '@see ' .. b.see else - for _, s in ipairs(b.see) do doc[#doc + 1] = '@see '..s end + for _, s in ipairs(b.see) do doc[#doc + 1] = '@see ' .. s end end end -- Format the block documentation. doc = table.concat(doc, '\n'):gsub('\\n', '\\\\n'):gsub('\n', '\\n') - file[#file + 1] = name:match('[^%.:]+$')..' '..doc + file[#file + 1] = string.format('%s %s', name:match('[^%.:]+$'), doc) end -- Returns the absolute path of the given relative path. -- @param string path String relative path. -- @return absolute path local function abspath(path) - path = lfs.currentdir()..'/'..path + path = string.format('%s/%s', lfs.currentdir(), path) path = path:gsub('%f[^/]%./', '') -- clean up './' while path:find('[^/]+/%.%./') do path = path:gsub('[^/]+/%.%./', '', 1) -- clean up '../' @@ -133,49 +137,48 @@ function M.start(doc) local modules, files = doc.modules, doc.files -- Map doc objects to file names so a module can be mapped to its filename. - for i = 1, #files do - local filename = files[i] + for _, filename in ipairs(files) do local doc = files[filename].doc files[doc] = abspath(filename) end -- Add a module's fields to its LuaDoc. - for i = 1, #files do - local module_doc = files[files[i]].doc[1] + for _, filename in ipairs(files) do + local module_doc = files[filename].doc[1] if module_doc and module_doc.class == 'module' and modules[module_doc.name] then modules[module_doc.name].fields = module_doc.field elseif module_doc then - print('[WARN] '..files[i]..' has no module declaration') + print(string.format('[WARN] %s has no module declaration', filename)) end end -- Convert module functions in the Lua luadoc into LuaDoc modules. local lua_luadoc = files['../modules/lua/lua.luadoc'] if lua_luadoc and #files == 1 then - for i = 1, #lua_luadoc.functions do - local f = lua_luadoc.functions[lua_luadoc.functions[i]] - local module_name = f.name:match('^([^%.:]+)[%.:]') or '_G' + for _, function_name in ipairs(lua_luadoc.functions) do + local func = lua_luadoc.functions[function_name] + local module_name = func.name:match('^([^%.:]+)[%.:]') or '_G' if not modules[module_name] then modules[#modules + 1] = module_name modules[module_name] = { - name = module_name, functions = {}, doc = {{code = f.code}} + name = module_name, functions = {}, doc = {{code = func.code}} } files[modules[module_name].doc] = abspath(files[1]) -- For functions like file:read(), 'file' is not a module; fake it. - if f.name:find(':') then modules[module_name].fake = true end + if func.name:find(':') then modules[module_name].fake = true end end local module = modules[module_name] - module.description = 'Lua '..module.name..' module.' - module.functions[#module.functions + 1] = f.name - module.functions[f.name] = f + module.description = string.format('Lua %s module.', module.name) + module.functions[#module.functions + 1] = func.name + module.functions[func.name] = func end - for i = 1, #lua_luadoc.tables do - local t = lua_luadoc.tables[lua_luadoc.tables[i]] - local module = modules[t.name or '_G'] + for _, table_name in ipairs(lua_luadoc.tables) do + local table = lua_luadoc.tables[table_name] + local module = modules[table.name or '_G'] if not module.fields then module.fields = {} end local fields = module.fields - for k, v in pairs(t.field) do + for k, v in pairs(table.field) do if not tonumber(k) then fields[#fields + 1], fields[k] = k, v end end end @@ -183,8 +186,8 @@ function M.start(doc) -- Process LuaDoc and write the tags and api files. local ctags, apidoc = {}, {} - for i = 1, #modules do - local m = modules[modules[i]] + for _, module_name in ipairs(modules) do + local m = modules[module_name] local filename = files[m.doc] if not m.fake then -- Tag and document the module. @@ -192,77 +195,82 @@ function M.start(doc) if m.name:find('%.') then -- Tag the last part of the module as a table of the first part. local parent, child = m.name:match('^(.-)%.([^%.]+)$') - write_tag(ctags, child, filename, m.doc[1].code[1], 'm', - 'class:'..parent) + write_tag( + ctags, child, filename, m.doc[1].code[1], 'm', 'class:' .. parent) end m.class = 'module' write_apidoc(apidoc, {name = '_G'}, m) end -- Tag and document the functions. - for j = 1, #m.functions do - local module_name, name = m.functions[j]:match('^(.-)[%.:]?([^.:]+)$') + for _, function_name in ipairs(m.functions) do + local module_name, name = function_name:match('^(.-)[%.:]?([^.:]+)$') if module_name == '' then module_name = m.name end - local func = m.functions[m.functions[j]] - write_tag(ctags, name, filename, func.code[1], 'f', 'class:'..module_name) + local func = m.functions[function_name] + write_tag( + ctags, name, filename, func.code[1], 'f', 'class:' .. module_name) write_apidoc(apidoc, m, func) end if m.tables then -- Document the tables. - for j = 1, #m.tables do - local table_name, table = m.tables[j], m.tables[m.tables[j]] + for _, table_name in ipairs(m.tables) do + local table = m.tables[table_name] local module_name = m.name if table_name:find('^_G%.') then module_name, table_name = table_name:match('^_G%.(.-)%.?([^%.]+)$') if not module_name then - print('[ERROR] Cannot determine module name for '..table.name) + print('[ERROR] Cannot determine module name for ' .. table.name) elseif module_name == '' then module_name = '_G' -- _G.keys or _G.snippets end end - write_tag(ctags, table_name, filename, table.code[1], 't', - 'class:'..module_name) + write_tag( + ctags, table_name, filename, table.code[1], 't', + 'class:' .. module_name) write_apidoc(apidoc, m, table) if table.field then -- Tag and document the table's fields. - table_name = module_name..'.'..table_name - for k = 1, #table.field do - write_tag(ctags, table.field[k], filename, table.code[1], 'F', - 'class:'..table_name) + table_name = string.format('%s.%s', module_name, table_name) + for _, field_name in ipairs(table.field) do + write_tag( + ctags, field_name, filename, table.code[1], 'F', + 'class:' .. table_name) write_apidoc(apidoc, {name = table_name}, { - name = table.field[k], - description = table.field[table.field[k]], - class = 'table' - }) + name = field_name, + description = table.field[field_name], + class = 'table' + }) end end end end if m.fields then -- Tag and document the fields. - for j = 1, #m.fields do - local field_name, field = m.fields[j], m.fields[m.fields[j]] + for _, field_name in ipairs(m.fields) do + local field = m.fields[field_name] local module_name = m.name if field_name:find('^_G%.') then module_name, field_name = field_name:match('^_G%.(.-)%.?([^%.]+)$') if not module_name then - print('[ERROR] Cannot determine module name for '..field.name) + print('[ERROR] Cannot determine module name for ' .. field.name) end end - write_tag(ctags, field_name, filename, m.doc[1].code[1], 'F', - 'class:'..module_name) + write_tag( + ctags, field_name, filename, m.doc[1].code[1], 'F', + 'class:' .. module_name) write_apidoc(apidoc, {name = field_name}, { - name = module_name..'.'..field_name, description = field, - class = 'field' - }) + name = string.format('%s.%s', module_name, field_name), + description = field, + class = 'field' + }) end end end table.sort(ctags) table.sort(apidoc) - local f = io.open(M.options.output_dir..'/tags', 'wb') + local f = io.open(M.options.output_dir .. '/tags', 'wb') f:write(table.concat(ctags, '\n')) f:close() - f = io.open(M.options.output_dir..'/api', 'wb') + f = io.open(M.options.output_dir .. '/api', 'wb') f:write(table.concat(apidoc, '\n')) f:close() |