Module:Data tables: Difference between revisions
Jump to navigation
Jump to search
common func call fix? |
revert to string append Tag: Manual revert |
||
(9 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
local podTable = require('Module:Data tables/pods').podTable | local podTable = require('Module:Data tables/pods').podTable | ||
local engineTable = require('Module:Data tables/engines').engineTable | local engineTable = require('Module:Data tables/engines').engineTable | ||
local tankTable = require('Module:Data tables/tanks').tankTable | |||
local parachuteTable = require('Module:Data tables/parachutes').parachuteTable | |||
-- Accepts <category> and <family> parameters to build the relevant part table | -- Accepts <category> and <family> parameters to build the relevant part table | ||
Line 18: | Line 20: | ||
elseif category == "engines" then | elseif category == "engines" then | ||
newTable = engineTable(args) | newTable = engineTable(args) | ||
elseif category == "fueltank" then | |||
newTable = tankTable(args) | |||
elseif family == "parachutes" then | |||
newTable = parachuteTable(args) | |||
else | else | ||
newTable = p._commonTable(args) | newTable = p._commonTable(args) | ||
Line 31: | Line 37: | ||
local category = args[1] | local category = args[1] | ||
local family = args[2] | local family = args[2] | ||
local cells = "" | local cells = "" | ||
-- load the full parts list | -- load the full parts list | ||
json = mw.loadJsonData(" | json = mw.loadJsonData("GameData:Collections/parts") | ||
-- each applicable part's data is appended to the table cells | -- each applicable part's data is appended to the table cells | ||
for k, v in pairs(json[category][family]) do | for k, v in pairs(json[category][family]) do | ||
local partJson = mw.loadJsonData(" | local partJson = mw.loadJsonData("GameData:"..k) | ||
local tempCell = _commonCells(partJson) | local tempCell = _commonCells(partJson) | ||
cells = tempCell..cells | cells = tempCell..cells | ||
Line 47: | Line 50: | ||
-- append the cells to the headers and cap off the table wiki text | -- append the cells to the headers and cap off the table wiki text | ||
local tempTable = | local tempTable = _commonHeaders()..cells.."|}" | ||
return tempTable | return tempTable | ||
end | end |
Latest revision as of 04:50, 7 March 2025
Documentation for this module may be created at Module:Data tables/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
_commonCells = require("Module:Data tables/common cells").commonCells
_commonHeaders = require("Module:Data tables/common cells").commonHeaders
local podTable = require('Module:Data tables/pods').podTable
local engineTable = require('Module:Data tables/engines').engineTable
local tankTable = require('Module:Data tables/tanks').tankTable
local parachuteTable = require('Module:Data tables/parachutes').parachuteTable
-- Accepts <category> and <family> parameters to build the relevant part table
function p.buildPartsTable(frame)
local args = getArgs(frame)
local category = args[1]
local family = args[2]
local newTable = ""
-- iterate to decide which table format needs to be used for the table build
if category == "pods" and family == "pod" then
newTable = podTable(args)
elseif category == "engines" then
newTable = engineTable(args)
elseif category == "fueltank" then
newTable = tankTable(args)
elseif family == "parachutes" then
newTable = parachuteTable(args)
else
newTable = p._commonTable(args)
end
-- preprocessing applies the wikitext
return frame:preprocess(newTable)
end
-- Builds a table that only uses the "common" data fields
-- Generally for structural/utility parts with no extra modules
function p._commonTable(args)
local category = args[1]
local family = args[2]
local cells = ""
-- load the full parts list
json = mw.loadJsonData("GameData:Collections/parts")
-- each applicable part's data is appended to the table cells
for k, v in pairs(json[category][family]) do
local partJson = mw.loadJsonData("GameData:"..k)
local tempCell = _commonCells(partJson)
cells = tempCell..cells
end
-- append the cells to the headers and cap off the table wiki text
local tempTable = _commonHeaders()..cells.."|}"
return tempTable
end
--------------------------------------------------------------------------------
---- ALL CODE BELOW TO BE REPLACED BY ALL CODE ABOVE, ONCE FULLY FUNCTIONAL ----
--------------------------------------------------------------------------------
-- args:
-- tablePage: Name of the tables subpage "Template:Data tables/<tablePage>". Ex: "parts", "tech tree"
-- tableName: Name of the table after the "#". Ex: "command-pods", "ground-landing-legs", "tier1"
function p.getTable(frame)
local args = getArgs(frame)
local tablePage = args[1]
local tableName = escapeTableName(args[2])
local tableContent = p._getTable(tablePage, tableName)
return frame:preprocess(tableContent)
-- return table.concat{frame:preprocess(partTable), "[[Category:Parts]]"}
end
function p._getTable(tablePage, TableName)
local tablePageContent = mw.title.new("Template:Data tables/"..tablePage):getContent()
local regex = '#'..TableName..'.-{|.-|}'
local tableWithTag = tablePageContent:match(regex)
local tableContent = tableWithTag:match('{|.-|}')
return tableContent
end
function escapeTableName(name)
return name:gsub("-","--")
end
function p.tableToObjects(inputTable)
local result = {}
local columns = {}
local header = inputTable:match("\n!(.-)\n|")
local body = inputTable:match("\n|%-(.-)\n|}")
columns = split(header, "\n!")
rows = split(body, "\n|-")
for i, row in ipairs(rows) do
local object = {}
local cells = split(row, "\n|")
cells = {unpack(cells, 2, #cells)}
for i, cell in ipairs(cells) do
local column = columns[i]
column = column:gsub("^%s*(.-)%s*$", "%1")
if (column == null) then
column = "missing"
end
object[column] = cell
end
table.insert(result, object)
end
return result
end
return p