Editing Module:Data to Table
Appearance
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
| Latest revision | Your text | ||
| Line 19: | Line 19: | ||
:gsub('\\', '\\\\') | :gsub('\\', '\\\\') | ||
:gsub('\'', '\\\'') | :gsub('\'', '\\\'') | ||
end | end | ||
| Line 148: | Line 26: | ||
local dataKeys = {} | local dataKeys = {} | ||
local rawKeys = {} | local rawKeys = {} | ||
-- Parse DPL data | -- Parse DPL data | ||
if frame.args.info then | if frame.args.info then | ||
if ne(frame.args.cleanTT) then | |||
frame.args.info = CleanTT(frame.args.info) | |||
end | |||
info = | info = lib.parseTemplateFormat( | ||
frame.args.info, | |||
';;', | |||
'$$$', | |||
'==' | |||
) | |||
else | else | ||
return '' | return '' | ||
end | end | ||
-- Parse keys | -- Parse keys | ||
| Line 237: | Line 47: | ||
dataKeys = p.ParseKeys(frame.args.keys) | dataKeys = p.ParseKeys(frame.args.keys) | ||
end | end | ||
-- Parse unquoted/raw keys | -- Parse unquoted/raw keys | ||
| Line 243: | Line 52: | ||
rawKeys = p.ParseKeys(frame.args.unquoted) | rawKeys = p.ParseKeys(frame.args.unquoted) | ||
end | end | ||
-- Block formatting | -- Block formatting | ||
| Line 249: | Line 57: | ||
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 | ||
-- JSON mode | |||
local jsonMode = tostring(frame.args.json) == '1' | |||
-- Constant value mode | -- Constant value mode | ||
| Line 261: | Line 70: | ||
if constantValue ~= nil then | if constantValue ~= nil then | ||
for _, rawKey in ipairs(rawKeys) do | for _, rawKey in ipairs(rawKeys) do | ||
if rawKey == 'value' then | if rawKey == 'value' then | ||
valueUnquoted = true | valueUnquoted = true | ||
break | break | ||
end | end | ||
end | end | ||
end | end | ||
| Line 276: | Line 81: | ||
-- 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 | |||
return false | |||
end | |||
-- ============================================================ | |||
-- JSON OUTPUT | |||
-- ============================================================ | |||
if jsonMode then | |||
local jsonEntries = {} | |||
-- Sort entries using the same sorting logic as normal output | |||
local sortedEntries = {} | |||
for _, entry in lib.spairs( | |||
info, | |||
function(tVal, a, b) | |||
if sortkey == 'INPUT' then | |||
return a < b | |||
else | |||
return (tVal[a][sortkey] or '') < (tVal[b][sortkey] or '') | |||
end | |||
end | |||
) do | |||
if ne(entry.page) then | |||
jsonEntries[#jsonEntries + 1] = entry.page | |||
end | |||
end | |||
-- Encode the array as valid JSON | |||
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 | end | ||
return | return output | ||
end | end | ||
-- ============================================================ | |||
-- LUA OUTPUT | |||
-- ============================================================ | |||
-- Build result | -- Build result | ||
| Line 297: | Line 153: | ||
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 '') < | 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) | ||
-- Constant value mode: | |||
-- |value=day produces ['Page'] = 'day' | |||
-- Constant value mode | |||
-- | |||
if constantValue ~= nil and #dataKeys == 0 then | if constantValue ~= nil and #dataKeys == 0 then | ||
| Line 324: | Line 173: | ||
value = clean(constantValue) | value = clean(constantValue) | ||
else | else | ||
value = '\'' .. | value = '\'' .. clean(constantValue) .. '\'' | ||
end | end | ||
| Line 337: | Line 184: | ||
) | ) | ||
-- No data keys: output page as a simple string | |||
-- No data keys | |||
elseif #dataKeys == 0 then | elseif #dataKeys == 0 then | ||
| Line 349: | Line 192: | ||
'\',\n' | '\',\n' | ||
) | ) | ||
else | else | ||
| Line 363: | Line 201: | ||
local valid = false | local valid = false | ||
local values = mw.html.create() | local values = mw.html.create() | ||
-- Entry name | -- Entry name | ||
| Line 371: | Line 208: | ||
'\'] = {' | '\'] = {' | ||
) | ) | ||
-- Build entry | -- Build entry | ||
local function parseLevels(K, level) | local function parseLevels(K, level) | ||
level = level or 1 | level = level or 1 | ||
| Line 393: | Line 228: | ||
) | ) | ||
) | ) | ||
elseif type(K) == 'table' then | elseif type(K) == 'table' then | ||
| Line 408: | Line 242: | ||
'},' | '},' | ||
) | ) | ||
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 429: | Line 257: | ||
'\',\n' | '\',\n' | ||
) | ) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
result:wikitext('}') | result:wikitext('}') | ||
-- | -- Wrap output in syntaxhighlight | ||
local luaText = tostring(result) | local luaText = tostring(result) | ||
-- Decode HTML entities introduced by mw.html | -- Decode HTML entities introduced by mw.html | ||
luaText = mw.text.decode(luaText) | luaText = mw.text.decode(luaText) | ||
local output = frame:extensionTag( | local output = frame:extensionTag( | ||
| Line 459: | Line 281: | ||
-- | -- Copy button | ||
if frame.args.copy then | if frame.args.copy then | ||
output = require('Module:CopyText')._main({ | output = require('Module:CopyText')._main({ | ||
output, | output, | ||
| Line 471: | Line 288: | ||
'Copy Table' | 'Copy Table' | ||
}) | }) | ||
end | end | ||
return output | return output | ||
| Line 484: | Line 299: | ||
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 | -- Default missing values | ||
| Line 494: | Line 307: | ||
if key == 'number' then | if key == 'number' then | ||
value = '0' | value = '0' | ||
unquoted = true | unquoted = true | ||
else | else | ||
value = '' | value = '' | ||
unquoted = false | unquoted = false | ||
end | end | ||
end | end | ||
| Line 518: | Line 327: | ||
table.concat(value, '\', \'') .. | table.concat(value, '\', \'') .. | ||
' }' | ' }' | ||
elseif unquoted and value ~= '' then | elseif unquoted and value ~= '' then | ||
| Line 524: | Line 332: | ||
-- Raw Lua value | -- Raw Lua value | ||
value = clean(value) | value = clean(value) | ||
else | else | ||
| Line 554: | Line 361: | ||
first[level] = false | first[level] = false | ||
if level > 2 then | if level > 2 then | ||
first[2] = false | first[2] = false | ||
end | end | ||
if level > 1 then | if level > 1 then | ||
| Line 581: | Line 386: | ||
end | end | ||
return container | return container | ||
| Line 592: | Line 396: | ||
function p.ParseKeys(keys) | function p.ParseKeys(keys) | ||
keys = lib.split(keys, ';;') | keys = lib.split(keys, ';;') | ||