Module:Command: Difference between revisions
Auto escape sub-commands |
No edit summary |
||
| Line 5: | Line 5: | ||
local fullCommand | local fullCommand | ||
local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower() | local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower() | ||
local params = {} | |||
local command = {} | local command = {} | ||
| Line 16: | Line 17: | ||
end | end | ||
table.insert( | table.insert( params, mw.text.trim( v ) ) | ||
end | end | ||
end | end | ||
if # | if #params == 1 and ( not args[2] or args[2] == '...' ) and params[1]:find( '%s' ) then | ||
local startPos, endPos = | local startPos, endPos = params[1]:find( '{.+}' ) | ||
if not startPos then | if not startPos then | ||
startPos, endPos = | startPos, endPos = params[1]:find( '<!%-%- Command %-%->.+<!%-%- /Command %-%->' ) | ||
end | end | ||
if startPos then | if startPos then | ||
params[1] = params[1]:sub( 1, startPos ) .. | |||
params[1]:sub( startPos + 1, endPos - 1 ):gsub( '%s', ' ' ) .. | |||
params[1]:sub( endPos ) | |||
end | end | ||
params = mw.text.split( params[1]:match( '^[^%s]+%s(.+)' ), '%s+' ) | |||
end | end | ||
if ( fullCommand or | if ( fullCommand or params[1] ) and syntax[commandName] then | ||
local param = 0 | local param = 0 | ||
function parseParams( defaultParams, sub ) | function parseParams( defaultParams, sub ) | ||
| Line 48: | Line 49: | ||
else | else | ||
param = param + 1 | param = param + 1 | ||
if | if params[param] then | ||
hasValue = true | hasValue = true | ||
if | if params[param] ~= '' and params[param] ~= '?' then | ||
table.insert( section, | table.insert( section, params[param] ) | ||
end | end | ||
end | end | ||
| Line 77: | Line 78: | ||
command = { parseParams( syntax[syntax[commandName]] or syntax[commandName] ) } | command = { parseParams( syntax[syntax[commandName]] or syntax[commandName] ) } | ||
-- Add any extra parameters not defined in the syntax | |||
if #params > param then | |||
for i, v in ipairs( params ) do | |||
if i > param then | |||
table.insert( command, v ) | |||
end | |||
end | |||
end | |||
else | |||
command = params | |||
end | end | ||