Module:CargoQuery: Difference between revisions

no edit summary
mNo edit summary
No edit summary
 
(9 intermediate revisions by 2 users not shown)
Line 14: Line 14:
local intro = frame:preprocess(args.intro or '')
local intro = frame:preprocess(args.intro or '')
local outro = frame:preprocess(args.outro or '')
local outro = frame:preprocess(args.outro or '')
local template = frame:preprocess(args.template or 'single query result')
local delimiter = args.delimiter or ''
local delimiter = args.delimiter or ''
Line 20: Line 21:
result = p.query(args)
result = p.query(args)
if (result == args.default or result == '') then
return result
end
local tbl = {}
local tbl = {}
if format == "table" then
if format == "table" then
Line 27: Line 30:
for i, row in ipairs(result) do
for i, row in ipairs(result) do
row.index = i
row.index = i
tbl[#tbl+1] = frame:expandTemplate{ title = args.template, args = row }
tbl[#tbl+1] = frame:expandTemplate{ title = template, args = row }
end
end
if colcount ~= nil then
if colcount ~= nil then
Line 178: Line 181:
template = frame:preprocess(args['template'] or '')
template = frame:preprocess(args['template'] or '')
if template == nil or template == '' then
if template == nil or template == '' then
     error('A template must be specified!')
     template='single query result'
end
end
delimiter = frame:preprocess(args['delimiter'] or '')
delimiter = frame:preprocess(args['delimiter'] or '')
Line 197: Line 200:
     end
     end
end
end
if result == args.default or result == '' then
return result
end
resulttbl = util_table.mergeArrays(nil, unpack(result))
resulttbl = util_table.mergeArrays(nil, unpack(result))
local tbl = {}
local tbl = {}
Line 211: Line 218:
end
end


function getColNames(row)
function h.getColNames(row)
cols = {}
cols = {}
for colname, _ in pairs(row)do
for colname, _ in pairs(row)do
Line 223: Line 230:
     local tbl = mw.html.create('table')
     local tbl = mw.html.create('table')
         :addClass('wikitable')
         :addClass('wikitable')
     colNames = getColNames(result[1])
     colNames = h.getColNames(result[1])
     util_html.printHeader(tbl, colNames)
     util_html.printHeader(tbl, colNames)
     util_html.printRowsByList(tbl, result, colNames)
     util_html.printRowsByList(tbl, result, colNames)