模块:DataWrap
General Support(讨论 | 贡献)2021年4月11日 (日) 20:16的版本
此模块的文档可以在模块:DataWrap/doc创建
local p = {} function extractArrayInObject(object) local result = {} for key, value in pairs(object) do if (type(value) == 'table') then for index, content in pairs(value) do result[key .. '.' .. index] = content end else result[key] = value end end return result end function dataWrap(mode, frame, type) local data = mw.text.jsonDecode(frame.args[1]) local template = frame.args[2] if (frame.args['reverse']) then data = data.reverse() end local result = '' if (mode == 'objects') then if (frame.args['count']) then maxIndex = math.min(tonumber(count), #data) else maxIndex = #data end for i = 1, maxIndex do result = result .. frame:expandTemplate{title = template, args = extractArrayInObject(data[i])} end elseif (mode == 'dictionaries') then for key, value in pairs(data) do result = result .. frame:expandTemplate{title = template, args = {key, value}} end end return result end function p.objects(frame) return dataWrap('objects', frame) end function p.dictionaries(frame) return dataWrap('dictionaries', frame) end function p.objectsGroupBy(frame) local key = frame.args[1] local data = mw.text.jsonDecode(frame.args[2]) local template = frame.args[3] if (frame.args['reverse']) then data = data.reverse() end local tag = 'ul' local class = 'listContainer' if (frame.args['tag']) then tag = frame.args['tag'] end if (frame.args['class']) then class = frame.args['class'] end local result = '' local section = '' local lastKeyValue = '' for index, record in ipairs(data) do local itemKeyValue = record[key] if (itemKeyValue ~= lastKeyValue) then if (lastKeyValue ~= '') then section = '<h2 class="positionStickyTop borderNone">' .. lastKeyValue .. '</h2><' .. tag ..' class="' .. class .. '">' .. section .. '</' .. tag .. '>' result = result .. frame:extensionTag{name = 'section', content = section, args = {}} end section = '' end section = section .. frame:expandTemplate{title = template, args = extractArrayInObject(record)} lastKeyValue = itemKeyValue end section = '<h2 class="positionStickyTop borderNone">' .. lastKeyValue .. '</h2><' .. tag ..' class="' .. class .. '">' .. section .. '</' .. tag .. '>' return result .. frame:extensionTag{name = 'section', content = section, args = {}} end function p.objectsGroupByMonth(frame) local data = mw.text.jsonDecode(frame.args[1]) local template = frame.args[2] if (frame.args['reverse']) then data = data.reverse() end local tag = 'ul' local class = 'listContainer' if (frame.args['tag']) then tag = frame.args['tag'] end if (frame.args['class']) then class = frame.args['class'] end local result = '' local section = '' local lastItemMonth = '' for index, record in ipairs(data) do local itemMonth = string.sub(tostring(record.date), 1, 6) if (itemMonth ~= lastItemMonth) then if (lastItemMonth ~= '') then section = '<h2 class="positionStickyTop borderNone">' .. string.sub(lastItemMonth, 1, 4) .. '年' .. tostring(tonumber(string.sub(lastItemMonth, 5, 6))) .. '月' .. '</h2><' .. tag ..' class="' .. class .. '">' .. section .. '</' .. tag .. '>' result = result .. frame:extensionTag{name = 'section', content = section, args = {}} end section = '' end section = section .. frame:expandTemplate{title = template, args = extractArrayInObject(record)} lastItemMonth = itemMonth end section = '<h2 class="positionStickyTop borderNone">' .. string.sub(lastItemMonth, 1, 4) .. '年' .. tostring(tonumber(string.sub(lastItemMonth, 5, 6))) .. '月' .. '</h2><' .. tag ..' class="' .. class .. '">' .. section .. '</' .. tag .. '>' return result .. frame:extensionTag{name = 'section', content = section, args = {}} end return p