Module:CargoQuery: Difference between revisions

no edit summary
(change meta separator thru arguments)
No edit summary
 
(27 intermediate revisions by 2 users not shown)
Line 11: Line 11:
function p.main(frame)
function p.main(frame)
local args = h.merge()
local args = h.merge()
local intro = frame:preprocess(args.intro or '')
local outro = frame:preprocess(args.outro or '')
local template = frame:preprocess(args.template or 'single query result')
local delimiter = args.delimiter or ''
local colcount = args.columns or nil
local format = args.format or nil
result = p.query(args)
result = p.query(args)
 
if (result == args.default or result == '') then
        local tbl = {}
return result
for i, row in ipairs(result) do
end
row.index = i
local tbl = {}
tbl[#tbl+1] = frame:expandTemplate{ title = args.template, args = row }
if format == "table" then
return (intro .. tostring(h.makeTable(result)) .. outro)
else
for i, row in ipairs(result) do
row.index = i
tbl[#tbl+1] = frame:expandTemplate{ title = template, args = row }
end
if colcount ~= nil then
intro = (intro .. '<div style="-webkit-column-count:' ..
    colcount .. '; -moz-column-count:' ..
colcount .. '; column-count:' .. colcount .. ';">')
outro = '</div>' ..outro
end
if format == "ul" then
intro = intro .. "<ul><li>"
outro = "</ul>" .. outro
delimiter = "<li>"
end
return intro .. table.concat(tbl, delimiter) .. outro
end
end
local intro = frame:preprocess(args.intro or '')
local outro = frame:preprocess(args.outro or '')
return intro .. table.concat(tbl,args.delimiter or '') .. outro
end
end


Line 158: 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 174: Line 197:
     tempresult = p.query(b)
     tempresult = p.query(b)
     if tempresult ~= nil and tempresult ~= '' then
     if tempresult ~= nil and tempresult ~= '' then
         result[#result+1] = p.query(b)
         result[#result+1] = tempresult
     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 189: Line 216:
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
function h.getColNames(row)
cols = {}
for colname, _ in pairs(row)do
table.insert(cols, colname)
end
return cols
end
function h.makeTable(result)
local util_html = require'Module:HTML Utility'
    local tbl = mw.html.create('table')
        :addClass('wikitable')
    colNames = h.getColNames(result[1])
    util_html.printHeader(tbl, colNames)
    util_html.printRowsByList(tbl, result, colNames)
    return tbl
end
end


return p
return p