Editing Module:Data to Table
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 14: | Line 14: | ||
-- Escape characters | -- Escape characters | ||
local function clean(txt) | local function clean(txt) | ||
txt = txt:gsub('&(%a+;)', '&%1') | |||
:gsub('\'', '\\'') | |||
:gsub('<', '<') | |||
:gsub('\'', '\\\'') | |||
return txt | return txt | ||
end | end | ||
| Line 148: | Line 28: | ||
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 49: | ||
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 54: | ||
rawKeys = p.ParseKeys(frame.args.unquoted) | rawKeys = p.ParseKeys(frame.args.unquoted) | ||
end | end | ||
-- Block formatting | -- Block formatting | ||
| Line 249: | Line 59: | ||
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 | ||
-- 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 290: | Line 77: | ||
-- Build result | -- Build result | ||
local result = mw.html.create() | local result = mw.html.create('pre') | ||
:css({ | |||
['min-height'] = '25px', | |||
['line-height'] = '16px' | |||
}) | |||
:wikitext('return {\n') | :wikitext('return {\n') | ||
local items = {} | |||
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 '') < | 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: output page as a simple string | |||
if #dataKeys == 0 then | |||
-- No data keys | |||
result:wikitext( | result:wikitext( | ||
| Line 349: | Line 109: | ||
'\',\n' | '\',\n' | ||
) | ) | ||
else | else | ||
| Line 363: | Line 118: | ||
local valid = false | local valid = false | ||
local values = mw.html.create() | local values = mw.html.create() | ||
-- Entry name | -- Entry name | ||
| Line 371: | Line 125: | ||
'\'] = {' | '\'] = {' | ||
) | ) | ||
-- 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' then | if type(K) == 'string' and entry[K] ~= nil then | ||
valid = true | valid = true | ||
values:node( | values:node( | ||
p.BuildEntry( | p.BuildEntry( | ||
| Line 393: | Line 143: | ||
) | ) | ||
) | ) | ||
elseif type(K) == 'table' then | elseif type(K) == 'table' then | ||
| Line 404: | Line 153: | ||
values:wikitext( | values:wikitext( | ||
'\n' | block | ||
and ('\n' .. string.rep('\t', level - 1)) | |||
or ' ', | |||
'},' | '},' | ||
) | ) | ||
end | end | ||
end | end | ||
parseLevels(dataKeys, 1) | parseLevels(dataKeys, 1) | ||
-- Return table if there is valid data, | |||
-- Return table if there is valid data | -- otherwise return the page as a plain string | ||
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 174: | ||
'\',\n' | '\',\n' | ||
) | ) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
result:wikitext('}') | result:wikitext('}') | ||
-- Copy button | |||
-- | |||
if frame.args.copy then | if frame.args.copy then | ||
result = require('Module:CopyText')._main({ | |||
tostring(result), | |||
frame.args.copy, | frame.args.copy, | ||
'Copy Table' | 'Copy Table' | ||
}) | }) | ||
end | end | ||
return result | |||
return | |||
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() | ||
-- Keep empty values | |||
if value ~= nil then | |||
-- Format value | |||
if subtable and value:find(subtable) then | |||
value = lib.split( | |||
clean(value), | |||
subtable | |||
) | |||
value = '{ \'' .. | |||
table.concat(value, '\', \'') .. | |||
' }' | |||
elseif unquoted then | |||
value | -- Raw Lua value | ||
value = clean(value) | |||
else | else | ||
value = '' | -- String value | ||
value = '\'' .. | |||
clean(value) .. | |||
'\'' | |||
end | 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 | |||
-- Subsequent item on this level | |||
else | |||
container:wikitext( | |||
'\n', | |||
string.rep('\t', level), | |||
formattedKey, | |||
' = ', | |||
value, | |||
',' | |||
) | |||
previous[level] = false | |||
end | end | ||
end | end | ||
return container | return container | ||
| Line 587: | Line 279: | ||
-- | -- Parse data keys into specified levels | ||
function p.ParseKeys(keys) | function p.ParseKeys(keys) | ||
keys = lib.split(keys, ';;') | keys = lib.split(keys, ';;') | ||