Module:Helper module

From Old School Near-Reality Wiki
Revision as of 19:58, 3 October 2022 by Jacmob (talk | contribs)
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Helper module/doc. [edit] [history] [purge]
Module:Helper module's function main is invoked by Template:Helper module.
Module:Helper module is invoked by Template:Helper module.

Provides documentation for functions of modules that contain functions useful for other modules. This template should be part of a comment, and should be at the top of the source code, before any <pre> or <nowiki> tags. There is no limit to the number of functions this template can list.

This template is called by Near-Reality:Lua/Helper modules to help produce a dynamic table.

{{Helper module
|name = Name of the module (no namespace prefix)
|fname1 = Name of first function as it is called in modules
|ftype1 = Argument types of parameters
|fuse1 = Purpose of the function
...
|fname# = Name of tenth function as it is called in modules
|ftype# = Argument types of parameters
|fuse# = Purpose of the function
}}

-- <nowiki>
-- Helps [[Near-Reality:Lua/Helper modules]] format its table with dynamic documentation
-- See [[Template:Helper module]] for documentation and usage
local p = {}

function p.main(frame)
	local args = frame:getParent().args
	local function_list = {}
	-- Let there be no limit to number of parameters
	local i = 1
	while args['fname'..i] do
		local funcname = args['fname'..i] or ''
		local functype = args['ftype'..i] or ''
		local funcuse = args['fuse'..i] or ''
		function_list[i] = {
				fname = funcname,
				ftype = functype,
				fdesc = funcuse
		}
		i = i + 1
	end
	local title = mw.title.getCurrentTitle()
	if title.namespace == 828 and (title.text == args.name or title.text == args.name..'/doc') then
		local t = mw.html.create('table')
		t	:addClass('wikitable')
			:tag('tr')
				:tag('th'):wikitext('Module'):done()
				:tag('th'):wikitext('Function'):done()
				:tag('th'):wikitext('Type'):done()
				:tag('th'):wikitext('Use'):done()
			:done()
			:wikitext(p._main(args.name,function_list))
		local category = ''
		if not (title.isSubpage and title.subpageText == 'doc') then
			category = '[[Category:Helper_modules]]'
		end
		return 'This module is a helper module to be used by other modules; it may not be designed to be invoked directly. See [[RuneScape:Lua/Helper modules]] for a full list and more information.\n' .. tostring(t) .. category
	else
		return p._main(args.name,function_list)
	end
end

function p._main(modn,func_list)
	local ret_row = mw.html.create('tr')
				:tag('td')
					-- Name will group together with all functions once
					:attr('rowspan',#func_list)
					:wikitext('[[Module:'..modn..'|'..modn..']]')
				:done()
				:tag('td')
					:tag('code')
						:wikitext(func_list[1].fname)
					:done()
				:done()
				:tag('td')
					:wikitext(func_list[1].ftype)
				:done()
				:tag('td')
					:wikitext(func_list[1].fdesc)
				:done()
			:done()
	local ret = tostring(ret_row)
	for i=2,#func_list,1 do
		local next_row = mw.html.create('tr')
				:tag('td')
					:tag('code')
						:wikitext(func_list[i].fname)
					:done()
				:done()
				:tag('td')
					:wikitext(func_list[i].ftype)
				:done()
				:tag('td')
					:wikitext(func_list[i].fdesc)
				:done()
			:done()
		ret = ret..tostring(next_row)
	end
	return ret
end

return p