Module:CargoQuery: Difference between revisions

1 byte removed ,  02:54, 2 September 2020
m
no edit summary
mNo edit summary
mNo edit summary
Line 29: Line 29:


function p.query(args)
function p.query(args)
  local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
  local query = {}
local query = {}
for k, v in pairs(args) do
for k, v in pairs(args) do
if string.sub(k, 0, 2) == 'q?' then
if string.sub(k, 0, 2) == 'q?' then
local key = string.sub(k, 3)
local key = string.sub(k, 3)
query[PARAM_LOOKUP[key] or key] = v
query[PARAM_LOOKUP[key] or key] = v
elseif PARAM_LOOKUP[k] then
elseif PARAM_LOOKUP[k] then
query[PARAM_LOOKUP[k]] = v
query[PARAM_LOOKUP[k]] = v
Line 59: Line 59:
-- This function on Leaguepedia is part of Module:ArgsUtil but is copied here to avoid dependencies
-- This function on Leaguepedia is part of Module:ArgsUtil but is copied here to avoid dependencies
function h.merge(mergeParent)
function h.merge(mergeParent)
        mergeParent = mergeParent or true
    mergeParent = mergeParent or true
local f = mw.getCurrentFrame()
local f = mw.getCurrentFrame()
local origArgs = f.args
local origArgs = f.args
Line 65: Line 65:
local args = {}
local args = {}
if mergeParent then
if mergeParent then
local parentArgs = f:getParent().args
local parentArgs = f:getParent().args
    for k, v in pairs(parentArgs) do
    for k, v in pairs(parentArgs) do
v = mw.text.trim(tostring(v))
v = mw.text.trim(tostring(v))
if v ~= '' then
if v ~= '' then
args[k] = v
args[k] = v
end
end
    end
    end
end
end
Line 155: Line 155:


function p.compound(frame)
function p.compound(frame)
local args = h.merge(false)
local args = h.merge(false)
        local splitargs = {}
local splitargs = {}
for a, b in ipairs(args) do
for a, b in ipairs(args) do
    splitargs[a] = util_args.splitNamedArgs(frame:preprocess(b), '%s*;%s*')
    splitargs[a] = util_args.splitNamedArgs(frame:preprocess(b), '%s*;%s*')
end
end
 
template = frame:preprocess(args['template'] or '')
splitargs['default'] = ''
delimiter = frame:preprocess(args['delimiter'] or '')
 
if template == nil or template == '' then
local result = {}
    error('A template must be specified!')
for a, b in ipairs(splitargs) do
end
    tempresult = p.query(b)
splitargs['default'] = ''
    if tempresult ~= nil and tempresult ~= '' then
        result[#result+1] = p.query(b)
    end
end
 
resulttbl = util_table.mergeArrays(nil, unpack(result))
local tbl = {}
 
template = frame:preprocess(args['template'] or '')
delimiter = frame:preprocess(args['delimiter'] or '')
 
if template == nil or template == '' then
    error('A template must be specified!')
end


local result = {}
for a, b in ipairs(splitargs) do
    tempresult = p.query(b)
    if tempresult ~= nil and tempresult ~= '' then
        result[#result+1] = p.query(b)
    end
end
resulttbl = util_table.mergeArrays(nil, unpack(result))
local tbl = {}
for i, row in ipairs(resulttbl) do
for i, row in ipairs(resulttbl) do
row.index = i
row.index = i
tbl[#tbl+1] = frame:expandTemplate{ title = args['template'], args = row }
tbl[#tbl+1] = frame:expandTemplate{title = args['template'], args = row}
end
end
         if #tbl == 0 then
         if #tbl == 0 then
Line 191: Line 186:
local outro = frame:preprocess(args['outro'] or '')
local outro = frame:preprocess(args['outro'] or '')
return intro  .. table.concat(tbl, delimiter) .. outro
return intro  .. table.concat(tbl, delimiter) .. outro
end
end


return p
return p