Implements {{Command}}
.
local p = {}
function p.cmd( f )
local args = f
if f == mw.getCurrentFrame() then
args = f:getParent().args
end
local commandName = args[1]:match( '^%s*/?([^%s]+)' ):lower()
local params = {}
local command = {}
--getparams
for i, v in ipairs( args ) do
if i > 1 or v:match( '^%s*/?(.+)' ):lower() ~= commandName then
table.insert( params, mw.text.trim( v ) )
end
end
--escape space and split args
if #params == 1 and ( not args[2] or args[2] == '...' ) and params[1]:find( '%s' ) then
params[1] = params[1]:match( '^[^%s]+%s(.+)' )
end
command = params
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
local attr = ''
if args.long then
attr = 'style="display: flex; padding: 0.8em 1em; margin-bottom: 0.4em; word-warp: break-word;"'
end
local result = table.concat( command, ' ' ):gsub( '〈space〉', ' ' ):gsub('〈lsqb〉', '%['):gsub('〈rsqb〉', '%]'):gsub('〈apos〉',"'"):gsub('〈quot〉','"')
-- Don't encode if told not to or if there is a sub-command
if args.escape ~= '0' then
result = result:gsub( '<', '<' ):gsub( '<(!%-%- Command %-%->)<','<%1<'):gsub( '</code><(!%-%- /Command %-%->)','</code><%1')
end
result = '<!-- Command --><code ' .. attr .. '><span>' .. slash .. result .. '</span></code><!-- /Command -->'
return result
end
return p