Module:Doc

From Halopedia, the Halo wiki

Documentation for this module may be created at ModuleDoc:Doc

local utils = require( 'Module:Utils' )

--------------------------------------------------------------------------------
------------------------------- PRIVATE FUNCTIONS ------------------------------
--------------------------------------------------------------------------------

function parse( content )
	local info = parseLines( content )
	local info = groupFuncs( info )
	local text = generateDocumentation( info )
	return text
end

function parseLines( content )
	content = mw.ustring.gsub( content, '[^%-%[]%[=-%[.-%]=-%]', '' )
	local lines = mw.text.split( content, '[\n\f\r\v]' )
	local info = {
		[ 'functions' ] = {},
	}
	local currDoc = {
		[ 'params' ] = {},
		[ 'desc' ] = ''
	}
	local inDocComment = false
	for i, line in ipairs( lines ) do
		line = mw.ustring.gsub( line, "'[^']-'", '')
		line = mw.ustring.gsub( line, '"[^"]-"', '')
		if not utils.empty( line ) then
			if inDocComment then
				if mw.ustring.find( line, '%]%]' ) then
					inDocComment = false
				end
				line = mw.ustring.gsub( line, '%]%].-', '')
				local tag = mw.ustring.match( line, '^%s-@%s-([^%s]+)')
				if tag then
					local content = mw.ustring.match( line, '^@%s-[^%s]+%s+([^%s].+)')
					handleTag(currDoc, tag, content)
				else
					currDoc.desc = currDoc.desc .. ' ' .. line
				end
			else
				local func = mw.ustring.match( line, '^%s-function%s+([^%(%s]+)' )
				if func then
					local fullname = mw.text.split( func, '%.')
					currDoc.name = fullname[#fullname]
					fullname[#fullname] = nil
					currDoc.owner = table.concat( fullname, '.' )
					table.insert( info.functions, currDoc )
					currDoc = {
						[ 'params' ] = {},
						[ 'desc' ] = ''
					}
				end
				local returns = mw.ustring.match( line, '^return%s+([^%s]+)')
				if returns then
					info.returns = returns
				end
				if mw.ustring.find( line, '--%[%[%[' ) then
					inDocComment = true
				end
			end
		end
	end
	return info
end

function handleTag( currDoc, tag, content )
	tag = mw.ustring.lower( tag )
	if tag == 'param' or tag == 'parameter' or tag == 'arg' or tag == 'argument' then
		local words = mw.text.split( content, "-" )
		local name = words[1] or '???'
		local type = ''
		if words[3] then
			type = words[2]
		end
		local desc = words[3] or words[2]
		table.insert( currDoc.params,  {
			[ 'name' ] = utils.strip( tostring( name ) ),
			[ 'type' ] = utils.strip( tostring( type ) ),
			[ 'desc' ] = utils.strip( tostring( desc ) )
		} )
	elseif tag == "return" or tag == "returns" then
		local words = mw.text.split( content, "-" )
		local type = ''
		if words[2] then