Module:Data to Table: Difference between revisions
ReisuDesign (talk | contribs) No edit summary |
ReisuDesign (talk | contribs) No edit summary |
||
| (9 intermediate revisions by the same user not shown) | |||
| Line 14: | Line 14: | ||
-- Escape characters | -- Escape characters for Lua strings | ||
local function clean(txt) | local function clean(txt) | ||
txt | return txt | ||
:gsub('\ | :gsub('\\', '\\\\') | ||
:gsub('\'', '\\\'') | :gsub('\'', '\\\'') | ||
return | 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 ~= nil then | |||
local info = parseInfo(groupInfo, frame) | |||
local entries = {} | |||
-- Collect entries | |||
for _, entry in lib.spairs( | |||
info, | |||
function(tVal, a, b) | |||
local aPage = tostring(tVal[a].page or a) | |||
local bPage = tostring(tVal[b].page or b) | |||
return aPage < bPage | |||
end | |||
) do | |||
if ne(entry.page) then | |||
entries[#entries + 1] = entry.page | |||
end | |||
end | |||
-- Only add the group if it actually contains entries. | |||
if #entries > 0 then | |||
groups[groupName] = entries | |||
end | |||
end | |||
i = i + 1 | |||
end | |||
return groups | |||
end | |||
-- Format grouped data as readable JSON | |||
function p.FormatJSONGroups(groups) | |||
local result = {} | |||
table.insert(result, '{') | |||
-- Get group names | |||
local groupNames = {} | |||
for name in pairs(groups) do | |||
groupNames[#groupNames + 1] = name | |||
end | |||
table.sort(groupNames) | |||
-- Build each group | |||
for i, name in ipairs(groupNames) do | |||
local entries = groups[name] | |||
local groupComma = (i < #groupNames) and ',' or '' | |||
table.insert( | |||
result, | |||
'\t' .. | |||
mw.text.jsonEncode(name) .. | |||
': [' | |||
) | |||
for j, entry in ipairs(entries) do | |||
local entryComma = (j < #entries) and ',' or '' | |||
table.insert( | |||
result, | |||
'\t\t' .. | |||
mw.text.jsonEncode(entry) .. | |||
entryComma | |||
) | |||
end | |||
table.insert( | |||
result, | |||
'\t]' .. groupComma | |||
) | |||
end | |||
table.insert(result, '}') | |||
return table.concat(result, '\n') | |||
end | end | ||
| Line 28: | Line 148: | ||
local dataKeys = {} | local dataKeys = {} | ||
local rawKeys = {} | local rawKeys = {} | ||
-- ============================================================ | |||
-- JSON MODE | |||
-- ============================================================ | |||
if tostring(frame.args.json) == '1' then | |||
-- -------------------------------------------------------- | |||
-- Grouped JSON mode | |||
-- -------------------------------------------------------- | |||
local groups = getJsonGroups(frame) | |||
-- If groups were supplied, output the complete JSON object. | |||
if next(groups) ~= nil then | |||
local jsonText = p.FormatJSONGroups(groups) | |||
return frame:extensionTag( | |||
'syntaxhighlight', | |||
jsonText, | |||
{ | |||
lang = 'json', | |||
copy = '' | |||
} | |||
) | |||
end | |||
-- -------------------------------------------------------- | |||
-- 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) | |||
local aPage = tostring(tVal[a].page or a) | |||
local bPage = tostring(tVal[b].page or b) | |||
return aPage < bPage | |||
end | |||
) do | |||
if ne(entry.page) then | |||
jsonEntries[#jsonEntries + 1] = entry.page | |||
end | |||
end | |||
local jsonText = mw.text.jsonEncode(jsonEntries) | |||
return frame:extensionTag( | |||
'syntaxhighlight', | |||
jsonText, | |||
{ | |||
lang = 'json', | |||
copy = '' | |||
} | |||
) | |||
end | |||
return '' | |||
end | |||
-- ============================================================ | |||
-- NORMAL LUA MODE | |||
-- ============================================================ | |||
-- Parse DPL data | -- Parse DPL data | ||
if frame.args.info then | if frame.args.info then | ||
info = | info = parseInfo(frame.args.info, frame) | ||
else | else | ||
return '' | return '' | ||
end | end | ||
-- Parse keys | -- Parse keys | ||
| Line 49: | Line 237: | ||
dataKeys = p.ParseKeys(frame.args.keys) | dataKeys = p.ParseKeys(frame.args.keys) | ||
end | end | ||
-- Parse unquoted/raw keys | -- Parse unquoted/raw keys | ||
| Line 54: | Line 243: | ||
rawKeys = p.ParseKeys(frame.args.unquoted) | rawKeys = p.ParseKeys(frame.args.unquoted) | ||
end | end | ||
-- Block formatting | -- Block formatting | ||
| Line 59: | Line 249: | ||
block = true | block = true | ||
end | end | ||
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 | |||
-- If |value=day is supplied, every page gets that value. | |||
local constantValue = frame.args.value | |||
local valueUnquoted = false | |||
if constantValue ~= nil then | |||
for _, rawKey in ipairs(rawKeys) do | |||
if rawKey == 'value' then | |||
valueUnquoted = true | |||
break | |||
end | |||
end | |||
end | |||
-- Check whether a key should be output as raw Lua | -- Check whether a key should be output as raw Lua | ||
local function isUnquotedKey(key) | local function isUnquotedKey(key) | ||
for _, rawKey in ipairs(rawKeys) do | for _, rawKey in ipairs(rawKeys) do | ||
if rawKey == key then | if rawKey == key then | ||
return true | return true | ||
end | end | ||
end | end | ||
| Line 77: | Line 290: | ||
-- Build result | -- Build result | ||
local result = mw.html.create( | local result = mw.html.create() | ||
:wikitext('return {\n') | :wikitext('return {\n') | ||
for _, entry in lib.spairs( | for _, entry in lib.spairs( | ||
info, | info, | ||
function(tVal, a, b) | function(tVal, a, b) | ||
if sortkey == 'INPUT' then | if sortkey == 'INPUT' then | ||
return a < b | return a < b | ||
else | else | ||
return (tVal[a][sortkey] or '') < (tVal[b][sortkey] or '') | return (tVal[a][sortkey] or '') < | ||
(tVal[b][sortkey] or '') | |||
end | end | ||
end | end | ||
) do | ) do | ||
if ne(entry.page) then | if ne(entry.page) then | ||
entry.page = clean(entry.page) | entry.page = clean(entry.page) | ||
-- No data keys | |||
-- ---------------------------------------------------- | |||
-- Constant value mode | |||
-- ---------------------------------------------------- | |||
if constantValue ~= nil and #dataKeys == 0 then | |||
local value | |||
if valueUnquoted then | |||
value = clean(constantValue) | |||
else | |||
value = '\'' .. | |||
clean(constantValue) .. | |||
'\'' | |||
end | |||
result:wikitext( | |||
'\t[\'', | |||
entry.page, | |||
'\'] = ', | |||
value, | |||
',\n' | |||
) | |||
-- ---------------------------------------------------- | |||
-- No data keys | |||
-- ---------------------------------------------------- | |||
elseif #dataKeys == 0 then | |||
result:wikitext( | result:wikitext( | ||
| Line 109: | Line 349: | ||
'\',\n' | '\',\n' | ||
) | ) | ||
-- ---------------------------------------------------- | |||
-- Normal nested table mode | |||
-- ---------------------------------------------------- | |||
else | else | ||
| Line 118: | Line 363: | ||
local valid = false | local valid = false | ||
local values = mw.html.create() | local values = mw.html.create() | ||
-- Entry name | -- Entry name | ||
| Line 125: | Line 371: | ||
'\'] = {' | '\'] = {' | ||
) | ) | ||
-- Build entry | -- Build entry | ||
local function parseLevels(K, level) | local function parseLevels(K, level) | ||
level = level or 1 | level = level or 1 | ||
if type(K) == 'string' | if type(K) == 'string' then | ||
valid = true | valid = true | ||
-- Use the value if present. | |||
-- BuildEntry handles missing values. | |||
values:node( | values:node( | ||
p.BuildEntry( | p.BuildEntry( | ||
| Line 143: | Line 393: | ||
) | ) | ||
) | ) | ||
elseif type(K) == 'table' then | elseif type(K) == 'table' then | ||
| Line 153: | Line 404: | ||
values:wikitext( | values:wikitext( | ||
'\n', | |||
string.rep('\t', level - 1), | |||
'},' | '},' | ||
) | ) | ||
end | end | ||
end | end | ||
parseLevels(dataKeys, 1) | parseLevels(dataKeys, 1) | ||
-- Return table if there is valid data | |||
-- Return table if there is valid data | |||
if valid then | if valid then | ||
values:wikitext('\n') | values:wikitext('\n') | ||
result:node(values) | result:node(values) | ||
else | else | ||
result:wikitext( | result:wikitext( | ||
'\t\'', | '\t\'', | ||
| Line 174: | Line 429: | ||
'\',\n' | '\',\n' | ||
) | ) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
result:wikitext('}') | result:wikitext('}') | ||
-- | |||
-- ============================================================ | |||
-- LUA SYNTAX HIGHLIGHT | |||
-- ============================================================ | |||
local luaText = tostring(result) | |||
-- Decode HTML entities introduced by mw.html | |||
luaText = mw.text.decode(luaText) | |||
local output = frame:extensionTag( | |||
'syntaxhighlight', | |||
luaText, | |||
{ | |||
lang = 'lua', | |||
copy = '' | |||
} | |||
) | |||
-- ============================================================ | |||
-- COPY BUTTON | |||
-- ============================================================ | |||
-- Keep Module:CopyText for normal Lua output. | |||
if frame.args.copy then | if frame.args.copy then | ||
output = require('Module:CopyText')._main({ | |||
output, | |||
frame.args.copy, | frame.args.copy, | ||
'Copy Table' | 'Copy Table' | ||
}) | }) | ||
end | end | ||
return | |||
return output | |||
end | end | ||
-- | -- ================================================================ | ||
-- FORMAT DATA | |||
-- ================================================================ | |||
function p.BuildEntry(value, key, level, subtable, unquoted) | function p.BuildEntry(value, key, level, subtable, unquoted) | ||
level = level or 1 | level = level or 1 | ||
local container = mw.html.create() | local container = mw.html.create() | ||
-- Default missing values | |||
if | if value == nil or value == '' then | ||
if key == 'number' then | |||
value = '0' | |||
unquoted = true | |||
else | |||
value = '' | |||
unquoted = false | |||
end | |||
end | |||
-- Format value | |||
if subtable and value ~= '' and value:find(subtable) then | |||
value = lib.split( | |||
clean(value), | |||
subtable | |||
) | |||
value = '{ \'' .. | |||
table.concat(value, '\', \'') .. | |||
' }' | |||
elseif unquoted and value ~= '' then | |||
-- Raw Lua value | |||
value = clean(value) | |||
else | |||
-- | -- String value | ||
value = '\'' .. | |||
clean(value) .. | |||
'\'' | |||
end | |||
-- Format key as ['key'] | |||
local formattedKey = | |||
'[\'' .. clean(key) .. '\']' | |||
-- First item on this level | |||
if first[level] then | |||
container:wikitext( | |||
'\n', | |||
string.rep('\t', level), | |||
formattedKey, | |||
' = ', | |||
value, | |||
',' | |||
) | |||
first[level] = false | |||
if level > 2 then | |||
first[2] = false | |||
end | |||
if level > 1 then | |||
first[1] = false | |||
end | end | ||
-- Subsequent item on this level | |||
else | |||
container:wikitext( | |||
'\n', | |||
string.rep('\t', level), | |||
formattedKey, | |||
' = ', | |||
value, | |||
',' | |||
) | |||
previous[level] = false | |||
end | end | ||
return container | return container | ||
| Line 279: | Line 587: | ||
-- | -- ================================================================ | ||
-- PARSE DATA KEYS INTO SPECIFIED LEVELS | |||
-- ================================================================ | |||
function p.ParseKeys(keys) | function p.ParseKeys(keys) | ||
keys = lib.split(keys, ';;') | keys = lib.split(keys, ';;') | ||