Module:Data to Table: Difference between revisions
ReisuDesign (talk | contribs) No edit summary |
ReisuDesign (talk | contribs) No edit summary |
||
| Line 19: | Line 19: | ||
:gsub('\\', '\\\\') | :gsub('\\', '\\\\') | ||
:gsub('\'', '\\\'') | :gsub('\'', '\\\'') | ||
end | |||
-- Parse DPL data | |||
local function parseInfo(info, frame) | |||
if not info then | |||
return {} | |||
end | |||
if ne(frame.args.cleanTT) then | |||
info = CleanTT(info) | |||
end | |||
return lib.parseTemplateFormat( | |||
info, | |||
';;', | |||
'$$$', | |||
'==' | |||
) | |||
end | |||
-- Get grouped JSON data | |||
local function getJsonGroups(frame) | |||
local groups = {} | |||
local i = 1 | |||
while true do | |||
local groupName = frame.args['group' .. i] | |||
local groupInfo = frame.args['group' .. i .. '_info'] | |||
-- Stop when neither argument exists. | |||
if groupName == nil and groupInfo == nil then | |||
break | |||
end | |||
if ne(groupName) and groupInfo then | |||
local info = parseInfo(groupInfo, frame) | |||
local entries = {} | |||
for _, entry in lib.spairs( | |||
info, | |||
function(tVal, a, b) | |||
return (tVal[a].page or a) < (tVal[b].page or b) | |||
end | |||
) do | |||
if ne(entry.page) then | |||
entries[#entries + 1] = entry.page | |||
end | |||
end | |||
groups[groupName] = entries | |||
end | |||
i = i + 1 | |||
end | |||
return groups | |||
end | end | ||
| Line 26: | Line 85: | ||
local dataKeys = {} | local dataKeys = {} | ||
local rawKeys = {} | local rawKeys = {} | ||
-- ============================================================ | |||
-- JSON MODE | |||
-- ============================================================ | |||
if tostring(frame.args.json) == '1' then | |||
local groups = getJsonGroups(frame) | |||
-- If groups were supplied, output the complete grouped object. | |||
if next(groups) ~= nil then | |||
local jsonText = mw.text.jsonEncode(groups) | |||
local output = frame:extensionTag( | |||
'syntaxhighlight', | |||
jsonText, | |||
{ | |||
lang = 'json', | |||
copy = '' | |||
} | |||
) | |||
-- Copy button | |||
if frame.args.copy then | |||
output = require('Module:CopyText')._main({ | |||
output, | |||
frame.args.copy, | |||
'Copy JSON' | |||
}) | |||
end | |||
return output | |||
end | |||
-- Otherwise, retain simple JSON-array mode. | |||
if frame.args.info then | |||
info = parseInfo(frame.args.info, frame) | |||
local jsonEntries = {} | |||
for _, entry in lib.spairs( | |||
info, | |||
function(tVal, a, b) | |||
return (tVal[a].page or a) < (tVal[b].page or b) | |||
end | |||
) do | |||
if ne(entry.page) then | |||
jsonEntries[#jsonEntries + 1] = entry.page | |||
end | |||
end | |||
local jsonText = mw.text.jsonEncode(jsonEntries) | |||
local output = frame:extensionTag( | |||
'syntaxhighlight', | |||
jsonText, | |||
{ | |||
lang = 'json', | |||
copy = '' | |||
} | |||
) | |||
-- Copy button | |||
if frame.args.copy then | |||
output = require('Module:CopyText')._main({ | |||
output, | |||
frame.args.copy, | |||
'Copy JSON' | |||
}) | |||
end | |||
return output | |||
end | |||
return '' | |||
end | |||
-- ============================================================ | |||
-- NORMAL LUA MODE | |||
-- ============================================================ | |||
-- Parse DPL data | -- Parse DPL data | ||
if frame.args.info then | if frame.args.info then | ||
info = parseInfo(frame.args.info, frame) | |||
else | else | ||
return '' | return '' | ||
| Line 60: | Line 193: | ||
local sortkey = frame.args.sortkey or 'page' | local sortkey = frame.args.sortkey or 'page' | ||
local subtable = frame.args.subtable | local subtable = frame.args.subtable | ||
-- Constant value mode | -- Constant value mode | ||
| Line 90: | Line 220: | ||
end | end | ||
-- Build result | -- Build result | ||