Module:Command

From Modded Wiki
Revision as of 12:12, 15 February 2014 by minecraft>Majr (Created page with "local p = {} function p.cmd( f ) local args = f:getParent().args local commandName local command = {} local cat = '' if args[1]:find( '%s' ) then commandName = args...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Command/doc

local p = {}
function p.cmd( f )
	local args = f:getParent().args
	local commandName
	local command = {}
	local cat = ''
	
	
	if args[1]:find( '%s' ) then
		commandName = args[1]
		cat = '[[Category:Invalid command name]]'
	else
		args[1] = args[1]:lower()
		commandName = '[[Commands#' .. args[1] .. '|' .. args[1] .. ']]'
		
		local fullCommand
		for _, v in ipairs( args ) do
			if v == '...' then
				fullCommand = true
			end
		end
		local syntax
		if fullCommand then
			-- mw.loadData is read-only
			syntax = require( 'Module:Command/Syntax' )
			command = syntax[syntax[args[1]]] or syntax[args[1]] or {}
			if type( command ) == 'string' then
				command = { command }
			end
		end
		
		for i, v in ipairs( args ) do
			if i ~= 1 then
				if v == '?' or v == 'options' then
					if not fullCommand then
						if not syntax then
							syntax = mw.loadData( 'Module:Command/Syntax' )
						end
						local name = syntax[syntax[args[1]]] or syntax[args[1]]
						if name and name[i - 1] then
							command[i - 1] = name[i - 1]
						end
					end
				elseif v ~= '...' then
					command[i - 1] = v
				end
			end
		end
	end
	
	local slash = '/'
	if args['/'] == '0' or args.slash == '0' then
		slash = ''
	end
	
	local param
	if args.long == '1' then
		param = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
	else
		param = 'class="nowrap"'
	end
	
	command = slash .. commandName .. ' ' .. table.concat( command, ' ' )
	
	return '<code ' .. param .. '>' .. command:gsub( '<', '&lt;' ) .. '</code>' .. cat
end
return p