Module:Command: Difference between revisions

From Modded Wiki
Jump to navigation Jump to search
Correctly support optional parameters
Smartly handle spaces and dataTags
Line 3: Line 3:
local args = f:getParent().args
local args = f:getParent().args
local syntax = mw.loadData( 'Module:Command/Syntax' )
local syntax = mw.loadData( 'Module:Command/Syntax' )
local command
local fullCommand
local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower()
local command = {}
args[1] = mw.text.trim( args[1] )
for i, v in ipairs( args ) do
if args[1]:find( '%s' ) or not syntax[args[1]] then
if not fullCommand and v == '...' then
command = { args[1]:match( '^([^%s]-)%s(.+)$' ) }
fullCommand = true
elseif i > 1 or v ~= commandName then
if #command == 0 then
-- Don't encode sub-commands
command = { args[1], args[2] }
if not v:find( ' ' ) then
v = mw.text.encode( v )
end
table.insert( command, mw.text.trim( v ) )
end
end
end
end
if not command then
if #command == 1 and ( not args[2] or args[2] == '...' ) and command[1]:find( '%s' ) then
command = { args[1] }
local startBrace, endBrace = command[1]:find( '{.+}' )
if startBrace then
command[1] = command[1]:sub( 1, startBrace ) ..
command[1]:sub( startBrace + 1, endBrace - 1 ):gsub( '%s', ' ' ) ..
command[1]:sub( endBrace )
end
command = mw.text.split( command[1]:match( '^[^%s]+%s(.+)' ), '%s+' )
end
end
command[1] = command[1]:lower()
if args[2] and syntax[args[1]] then
if ( fullCommand or command[1] ) and syntax[commandName] then
local fullCommand
local param = 0
for _, v in ipairs( args ) do
function parseParams( defaultParams, sub )
if v == '...' then
fullCommand = true
end
end
local arg = 1
function parseParams( params, sub )
local section = {}
local section = {}
local hasValue
local hasValue
for i, v in ipairs( params ) do
for i, v in ipairs( defaultParams ) do
if type( v ) == 'table' then
if type( v ) == 'table' then
local subSection, subHasValue = parseParams( v, true )
local subSection, subHasValue = parseParams( v, true )
Line 38: Line 42:
table.insert( section, subSection )
table.insert( section, subSection )
else
else
arg = arg + 1
param = param + 1
if command[param] then
-- Skip "..." arg
if args[arg] and args[arg] == '...' then
arg = arg + 1
end
if args[arg] then
hasValue = true
hasValue = true
args[arg] = mw.text.trim( args[arg] )
if command[param] ~= '' and command[param] ~= '?' then
if args[arg] ~= '' and args[arg] ~= '?' then
table.insert( section, command[param] )
args[arg] = args[arg]:gsub( '<', '&lt;' )
table.insert( section, args[arg] )
end
end
end
end
Line 60: Line 56:
section = table.concat( section, ' ' )
section = table.concat( section, ' ' )
if sub and not hasValue then
if sub then
if fullCommand then
if not hasValue then
section = '(' .. section .. ')'
if fullCommand then
else
section = '(' .. section .. ')'
section = nil
else
section = nil
end
end
end
return section, hasValue
else
return section
end
end
return section, hasValue
end
end
command[2] = parseParams( syntax[syntax[command[1]]] or syntax[command[1]] )
command = { parseParams( syntax[syntax[commandName]] or syntax[commandName] ) }
end
end
if args.link then
if args.link then
if args.link:lower() ~= 'none' then
if args.link:lower() ~= 'none' then
command[1] = '[[' .. args.link .. '|' .. command[1] .. ']]'
commandName = '[[' .. args.link .. '|' .. commandName .. ']]'
end
end
else
else
command[1] = '[[Commands#' .. command[1] .. '|' .. command[1] .. ']]'
commandName = '[[Commands#' .. commandName .. '|' .. commandName .. ']]'
end
end
table.insert( command, 1, commandName )
local slash = '/'
local slash = '/'
Line 87: Line 88:
end
end
local attr
if args.sub then
if args.long == '1' then
return slash .. table.concat( command, ' ' ):gsub( '%s', '&#32;' )
attr = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
else
else
attr = 'class="nowrap"'
local attr
if args.long == '1' then
attr = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
else
attr = 'class="nowrap"'
end
return '<code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ):gsub( '&#32;', ' ' ) .. '</code>'
end
end
return '<code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ) .. '</code>'
end
end
return p
return p

Revision as of 08:05, 21 February 2014

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

local p = {}
function p.cmd( f )
	local args = f:getParent().args
	local syntax = mw.loadData( 'Module:Command/Syntax' )
	local fullCommand
	local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower()
	local command = {}
	
	for i, v in ipairs( args ) do
		if not fullCommand and v == '...' then
			fullCommand = true
		elseif i > 1 or v ~= commandName then
			-- Don't encode sub-commands
			if not v:find( '&#32;' ) then
				v = mw.text.encode( v )
			end
			table.insert( command, mw.text.trim( v ) )
		end
	end
	if #command == 1 and ( not args[2] or args[2] == '...' ) and command[1]:find( '%s' ) then
		local startBrace, endBrace = command[1]:find( '{.+}' )
		if startBrace then
			command[1] = command[1]:sub( 1, startBrace ) ..
				command[1]:sub( startBrace + 1, endBrace - 1 ):gsub( '%s', '&#32;' ) ..
				command[1]:sub( endBrace )
		end
		
		command = mw.text.split( command[1]:match( '^[^%s]+%s(.+)' ), '%s+' )
	end
	
	if ( fullCommand or command[1] ) and syntax[commandName] then
		local param = 0
		function parseParams( defaultParams, sub )
			local section = {}
			local hasValue
			for i, v in ipairs( defaultParams ) do
				if type( v ) == 'table' then
					local subSection, subHasValue = parseParams( v, true )
					if subHasValue then
						hasValue = true
					end
					table.insert( section, subSection )
				else
					param = param + 1
					if command[param] then
						hasValue = true
						if command[param] ~= '' and command[param] ~= '?' then
							table.insert( section, command[param] )
						end
					end
					if not section[i] then
						table.insert( section, v )
					end
				end
			end
			section = table.concat( section, ' ' )
			
			if sub then
				if not hasValue then
					if fullCommand then
						section = '(' .. section .. ')'
					else
						section = nil
					end
				end
				
				return section, hasValue
			else
				return section
			end
		end
		
		command = { parseParams( syntax[syntax[commandName]] or syntax[commandName] ) }
	end
	
	if args.link then
		if args.link:lower() ~= 'none' then
			commandName = '[[' .. args.link .. '|' .. commandName .. ']]'
		end
	else
		commandName = '[[Commands#' .. commandName .. '|' .. commandName .. ']]'
	end
	table.insert( command, 1, commandName )
	
	local slash = '/'
	if args['/'] == '0' or args.slash == '0' then
		slash = ''
	end
	
	if args.sub then
		return slash .. table.concat( command, ' ' ):gsub( '%s', '&#32;' )
	else
		local attr
		if args.long == '1' then
			attr = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
		else
			attr = 'class="nowrap"'
		end
		
		return '<code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ):gsub( '&#32;', ' ' ) .. '</code>'
	end
end
return p