(igsub) |
m (merge) |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
+ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
function p.getParametersCount( f ) |
function p.getParametersCount( f ) |
||
Line 16: | Line 36: | ||
end |
end |
||
return parametersCount |
return parametersCount |
||
⚫ | |||
+ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | end |
||
+ | |||
+ | |||
+ | function p.merge( ... ) |
||
+ | local result = {} |
||
+ | for _, table in ipairs( arg ) do |
||
+ | for k, v in pairs( table ) do |
||
+ | result[k] = v |
||
+ | end |
||
+ | end |
||
+ | return result |
||
end |
end |
||
Line 37: | Line 92: | ||
end |
end |
||
return nil |
return nil |
||
⚫ | |||
− | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 65: | Line 101: | ||
if type( j ) == 'string' then |
if type( j ) == 'string' then |
||
g[i] = mw.text.trim( j ) ~= '' and ( toRemove and mw.text.trim( j, toRemove ) or mw.text.trim( j ) ) or keepEmptyParams and '' |
g[i] = mw.text.trim( j ) ~= '' and ( toRemove and mw.text.trim( j, toRemove ) or mw.text.trim( j ) ) or keepEmptyParams and '' |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
else |
||
g[i] = j |
g[i] = j |
Revision as of 16:38, 15 December 2019
The above documentation is transcluded from Module:Table/doc. (edit | history)
local p = {}
-- Get parameters of the template and the article from the frame (usually f)
function p.getArgs( f )
local args = {}
if f.args then
-- Add template parameters
for i, j in pairs( f.args ) do
args[i] = j
end
-- Add transclusion parameters (parent), overwrite template parameters
for k, l in pairs( f:getParent().args ) do
args[k] = l
end
else
args = f
end
return args
end
function p.getParametersCount( f )
local args = p.trimAll( p.getArgs( f ) )
local parametersCount = 0
if args.numerical then
local i = 1
while args[i] do
parametersCount = parametersCount + 1
i = i + 1
end
else
for i, j in pairs( args ) do
parametersCount = parametersCount + 1
end
end
return parametersCount
end
-- Do string.gsub to all values in a table
function p.gsub( f, pattern, repl )
local g = {}
for i, j in pairs( f ) do
if type( j ) == 'string' then
g[i] = string.gsub( j, pattern, repl )
else
g[i] = j
end
end
return g
end
-- Do string.gsub to all indexes in a table
function p.igsub( f, pattern, repl )
local g = {}
for i, j in pairs( f ) do
g[string.gsub( i, pattern, repl )] = j
end
return g
end
function p.merge( ... )
local result = {}
for _, table in ipairs( arg ) do
for k, v in pairs( table ) do
result[k] = v
end
end
return result
end
function p.search( f )
local args = p.trimAll( p.getArgs( f ) )
if args.index then
for i, j in pairs( args ) do
if args.object == j then
return i
end
end
else
local k = 1
repeat
if args.object == args[k] then
return k
end
k = k + 1
until not args[k]
end
return nil
end
-- Do mw.text.trim to all values in a table
function p.trimAll( f, toRemove, keepEmptyParams )
local g = {}
for i, j in pairs( f ) do
if type( j ) == 'string' then
g[i] = mw.text.trim( j ) ~= '' and ( toRemove and mw.text.trim( j, toRemove ) or mw.text.trim( j ) ) or keepEmptyParams and ''
else
g[i] = j
end
end
return g
end
return p