Module:Command: Difference between revisions

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..."
 
Correctly support optional parameters
Line 2: Line 2:
function p.cmd( f )
function p.cmd( f )
local args = f:getParent().args
local args = f:getParent().args
local commandName
local syntax = mw.loadData( 'Module:Command/Syntax' )
local command = {}
local command
local cat = ''
args[1] = mw.text.trim( args[1] )
if args[1]:find( '%s' ) or not syntax[args[1]] then
command = { args[1]:match( '^([^%s]-)%s(.+)$' ) }
if #command == 0 then
command = { args[1], args[2] }
end
end
if not command then
command = { args[1] }
end
command[1] = command[1]:lower()
if args[1]:find( '%s' ) then
if args[2] and syntax[args[1]] then
commandName = args[1]
cat = '[[Category:Invalid command name]]'
else
args[1] = args[1]:lower()
commandName = '[[Commands#' .. args[1] .. '|' .. args[1] .. ']]'
local fullCommand
local fullCommand
for _, v in ipairs( args ) do
for _, v in ipairs( args ) do
if v == '...' then
if v == '...' then
fullCommand = true
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
end
end
for i, v in ipairs( args ) do
local arg = 1
if i ~= 1 then
function parseParams( params, sub )
if v == '?' or v == 'options' then
local section = {}
if not fullCommand then
local hasValue
if not syntax then
for i, v in ipairs( params ) do
syntax = mw.loadData( 'Module:Command/Syntax' )
if type( v ) == 'table' then
end
local subSection, subHasValue = parseParams( v, true )
local name = syntax[syntax[args[1]]] or syntax[args[1]]
if subHasValue then
if name and name[i - 1] then
hasValue = true
command[i - 1] = name[i - 1]
end
table.insert( section, subSection )
else
arg = arg + 1
-- Skip "..." arg
if args[arg] and args[arg] == '...' then
arg = arg + 1
end
if args[arg] then
hasValue = true
args[arg] = mw.text.trim( args[arg] )
if args[arg] ~= '' and args[arg] ~= '?' then
args[arg] = args[arg]:gsub( '<', '&lt;' )
table.insert( section, args[arg] )
end
end
end
end
elseif v ~= '...' then
if not section[i] then
command[i - 1] = v
table.insert( section, v )
end
end
end
end
end
section = table.concat( section, ' ' )
if sub and not hasValue then
if fullCommand then
section = '(' .. section .. ')'
else
section = nil
end
end
return section, hasValue
end
command[2] = parseParams( syntax[syntax[command[1]]] or syntax[command[1]] )
end
if args.link then
if args.link:lower() ~= 'none' then
command[1] = '[[' .. args.link .. '|' .. command[1] .. ']]'
end
end
else
command[1] = '[[Commands#' .. command[1] .. '|' .. command[1] .. ']]'
end
end
Line 54: Line 87:
end
end
local param
local attr
if args.long == '1' then
if args.long == '1' then
param = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
attr = 'style="display:block;padding:0.8em 1em;margin-bottom:0.4em;word-wrap:break-word"'
else
else
param = 'class="nowrap"'
attr = 'class="nowrap"'
end
end
command = slash .. commandName .. ' ' .. table.concat( command, ' ' )
return '<code ' .. attr .. '>' .. slash .. table.concat( command, ' ' ) .. '</code>'
return '<code ' .. param .. '>' .. command:gsub( '<', '&lt;' ) .. '</code>' .. cat
end
end
return p
return p