Editing Module:Data to Table

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
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 56: Line 56:
end
end


if ne(groupName) and groupInfo ~= nil then
if ne(groupName) and groupInfo then
 
local info = parseInfo(groupInfo, frame)
local info = parseInfo(groupInfo, frame)
local entries = {}
local entries = {}


-- Collect entries
for _, entry in lib.spairs(
for _, entry in lib.spairs(
info,
info,
Line 71: Line 69:
end
end
) do
) do
if ne(entry.page) then
if ne(entry.page) then
entries[#entries + 1] = entry.page
entries[#entries + 1] = entry.page
end
end
end
end


-- Only add the group if it actually contains entries.
groups[groupName] = entries
if #entries > 0 then
groups[groupName] = entries
end
end
end


Line 88: Line 81:


return groups
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 154: Line 94:


if tostring(frame.args.json) == '1' then
if tostring(frame.args.json) == '1' then
-- --------------------------------------------------------
-- Grouped JSON mode
-- --------------------------------------------------------


local groups = getJsonGroups(frame)
local groups = getJsonGroups(frame)


-- If groups were supplied, output the complete JSON object.
-- If groups were supplied, output the complete grouped object.
if next(groups) ~= nil then
if next(groups) ~= nil then


local jsonText = p.FormatJSONGroups(groups)
local jsonText = mw.text.jsonEncode(groups)


return frame:extensionTag(
local output = frame:extensionTag(
'syntaxhighlight',
'syntaxhighlight',
jsonText,
jsonText,
Line 174: Line 110:
}
}
)
)
end


-- Copy button
if frame.args.copy then
output = require('Module:CopyText')._main({
output,
frame.args.copy,
'Copy JSON'
})
end


-- --------------------------------------------------------
return output
-- Simple JSON array mode
end
-- --------------------------------------------------------


-- Otherwise, retain simple JSON-array mode.
if frame.args.info then
if frame.args.info then


Line 190: Line 133:
info,
info,
function(tVal, a, b)
function(tVal, a, b)
local aPage = tostring(tVal[a].page or a)
return (tVal[a].page or a) < (tVal[b].page or b)
local bPage = tostring(tVal[b].page or b)
 
return aPage < bPage
end
end
) do
) do
if ne(entry.page) then
if ne(entry.page) then
jsonEntries[#jsonEntries + 1] = entry.page
jsonEntries[#jsonEntries + 1] = entry.page
end
end
end
end


local jsonText = mw.text.jsonEncode(jsonEntries)
local jsonText = mw.text.jsonEncode(jsonEntries)


return frame:extensionTag(
local output = frame:extensionTag(
'syntaxhighlight',
'syntaxhighlight',
jsonText,
jsonText,
Line 213: Line 151:
}
}
)
)
-- Copy button
if frame.args.copy then
output = require('Module:CopyText')._main({
output,
frame.args.copy,
'Copy JSON'
})
end
return output
end
end


Line 225: Line 174:
-- Parse DPL data
-- Parse DPL data
if frame.args.info then
if frame.args.info then
info = parseInfo(frame.args.info, frame)
info = parseInfo(frame.args.info, frame)
else
else
return ''
return ''
end
end


-- Parse keys
-- Parse keys
Line 237: Line 183:
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 188:
rawKeys = p.ParseKeys(frame.args.unquoted)
rawKeys = p.ParseKeys(frame.args.unquoted)
end
end


-- Block formatting
-- Block formatting
Line 249: Line 193:
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
-- Constant value mode
Line 261: Line 203:


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 214:
-- 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 297: Line 232:
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 '')
(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 252:
value = clean(constantValue)
value = clean(constantValue)
else
else
value = '\'' ..
value = '\'' .. clean(constantValue) .. '\''
clean(constantValue) ..
'\''
end
end


Line 337: Line 263:
)
)


 
-- No data keys: output page as a simple string
-- ----------------------------------------------------
-- No data keys
-- ----------------------------------------------------
 
elseif #dataKeys == 0 then
elseif #dataKeys == 0 then


Line 349: Line 271:
'\',\n'
'\',\n'
)
)
-- ----------------------------------------------------
-- Normal nested table mode
-- ----------------------------------------------------


else
else
Line 363: Line 280:
local valid = false
local valid = false
local values = mw.html.create()
local values = mw.html.create()


-- Entry name
-- Entry name
Line 371: Line 287:
'\'] = {'
'\'] = {'
)
)


-- 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 307:
)
)
)
)


elseif type(K) == 'table' then
elseif type(K) == 'table' then
Line 408: Line 321:
'},'
'},'
)
)
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 336:
'\',\n'
'\',\n'
)
)
end
end
end
end
end
end
end
end


result:wikitext('}')
result:wikitext('}')




-- ============================================================
-- Wrap output in syntaxhighlight
-- LUA SYNTAX HIGHLIGHT
-- ============================================================
 
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 360:




-- ============================================================
-- Copy button
-- 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 = require('Module:CopyText')._main({
output,
output,
Line 471: Line 367:
'Copy Table'
'Copy Table'
})
})
end
end


return output
return output
Line 484: Line 378:


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 386:


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 406:
table.concat(value, '\', \'') ..
table.concat(value, '\', \'') ..
' }'
' }'


elseif unquoted and value ~= '' then
elseif unquoted and value ~= '' then
Line 524: Line 411:
-- Raw Lua value
-- Raw Lua value
value = clean(value)
value = clean(value)


else
else
Line 554: Line 440:


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 465:


end
end


return container
return container
Line 592: Line 475:


function p.ParseKeys(keys)
function p.ParseKeys(keys)
keys = lib.split(keys, ';;')
keys = lib.split(keys, ';;')


Please note that all contributions to The Petit Planet Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see Petit Planet Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template
Below are commonly used wikitext from MediaWiki:Edittools. Click on any to insert them in the edit box.
File Pages==Summary====Licensing=={{Fairuse}}{{File|}}
Dynamic Page List¦²«»²{¦}²«»«/»«!----»
Array in DPL¹^2{}^2^2{¹}^2
Parser Functions{{#if:||}}
{{#ifeq:|||}}
{{#expr:}}
{{#switch:||#default=}}
{{#replace:||}}
{{#titleparts:}}
{{#tag:|}}
Markup{{}}|[][[]][[Category:]]#REDIRECT [[]] [[Category:Redirect Pages]]<code></code><includeonly></includeonly><noinclude></noinclude><nowiki></nowiki><!----><br>§
Notices{{Stub|}}{{Stub Dialogue}}{{Upcoming}}{{Under Construction}}{{Placeholder|}}
Magic Words{{!}}{{formatnum:}}{{lc:}}{{uc:}}{{PAGENAME}}{{FULLPAGENAME}}{{ROOTPAGENAME}}{{BASEPAGENAME}}{{SUBPAGENAME}}{{DISPLAYTITLE:|noreplace}}__TOC____NOTOC____EXPECTUNUSEDCATEGORY____HIDDENCAT__
HTML Entities&mdash;&nbsp;&comma;&shy;