Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Data tables: Difference between revisions

From Kerbal Space Program 2 Wiki
(new line test, no cells)
(part name now links to page)
 
(18 intermediate revisions by the same user not shown)
Line 2: Line 2:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


-- commonHeaders are used by all parts tables so they're called from here for
-- cleaner code per category/family table
local commonHeaders = "{| class=\"wikitable\"\n" ..
"!Image\n" ..
"!Name\n" ..
"!Size\n" ..
"!Mass (t)\n" ..
"!Max Temperature (K)\n" ..
"!Impact Tolerance (m/s)\n"
-- commonCells pair to commonHeaders, so they are independent from the
-- individual tables for the same reason (cleaner code)
function p._commonCells(partJson)
local commonCells = "|-\n" ..
"|".."[[File:"..partJson["file"].."|80px|center]]\n" ..
"|".."[["..partJson["name"].."]]\n" ..
"|".."{{Size|Size="..partJson["size"].."}}\n" ..
"|"..partJson["mass"].."\n" ..
"|"..partJson["max_temp"].."\n" ..
"|"..partJson["imp_tol"].."\n"
return commonCells
end
-- Accepts <category> and <family> parameters to build the relevant part table
function p.buildPartsTable(frame)
function p.buildPartsTable(frame)
local args = getArgs(frame)
local args = getArgs(frame)
Line 8: Line 32:
local newTable = ""
local newTable = ""
-- iterate to decide which table format needs to be used for the table build
if category == "command_modules" and family == "pods" then
if category == "command_modules" and family == "pods" then
newTable = p._podTable(args)
newTable = p._podTable(args)
elseif category == "engines" then
elseif category == "engines" then
newTable = p._engineTable(args)
newTable = p._engineTable(args)
else
newTable = p._commonTable(args)
end
end
-- preprocessing applies the wikitext
return frame:preprocess(newTable)
return frame:preprocess(newTable)
end
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]
-- adds thrust and isp values
local header = commonHeaders
local cells = ""
-- load the full parts list to iterate per given category and family
json = mw.loadJsonData("Data: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("Data:"..k)
local tempCell = p._commonCells(partJson)
cells = tempCell..cells
end
-- append the cells to the headers and cap off the table wiki text
local tempTable = header..cells.."|}"
return tempTable
end
-- Builds table formatted for engines
function p._engineTable(args)
function p._engineTable(args)
local category = args[1]
local category = args[1]
local family = args[2]
local family = args[2]
local tempTable = ""
local name = ""
-- adds thrust and isp values
local mass = ""
local header = commonHeaders ..
local max_temp = ""
local header = "{| class=\"wikitable\"\n" ..
"|+ style=\"caption-side: bottom\" | <sup>1</sup> The CR-7 R.A.P.I.E.R.'s air-breathing mode specs can be found in the jet engines table.\n" ..
"!Image\n" ..
"!Name\n" ..
"!Mass (t)\n" ..
"!Max Thrust: 1 atm (kN)\n" ..
"!Max Thrust: 1 atm (kN)\n" ..
"!Max Thrust: Vac. (kN)\n" ..
"!Max Thrust: Vac. (kN)\n" ..
"!ISP: 1 atm (s)\n" ..
"!ISP: 1 atm (s)\n" ..
"!ISP: Vac. (s)\n" ..
"!ISP: Vac. (s)\n"
"!Max Temp (K)\n" ..
local cells = ""
"!Size\n"
local cell = ""
-- load the full parts list to iterate per given category and family
json = mw.loadJsonData("Data:Collections/parts")
json = mw.loadJsonData("Data:Collections/parts")
-- 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("Data:"..k)
local partJson = mw.loadJsonData("Data:"..k)
cell = "|-" ..
local tempCell = p._commonCells(partJson) ..
"|".."[[File:"..partJson["file"].."|80px|center]]" ..
"|"..partJson["modules"]["engine"]["max_thrust"]["atm"].."\n" ..
"|"..partJson["name"] ..
"|"..partJson["modules"]["engine"]["max_thrust"]["vac"].."\n" ..
"|"..partJson["mass"] ..
"|"..partJson["modules"]["engine"]["isp"]["atm"].."\n" ..
"|"..partJson["modules"]["engine"]["max_thrust"]["atm"] ..
"|"..partJson["modules"]["engine"]["isp"]["vac"].."\n"
"|"..partJson["modules"]["engine"]["max_thrust"]["vac"] ..
cells = tempCell..cells
"|"..partJson["modules"]["engine"]["isp"]["atm"] ..
"|"..partJson["modules"]["engine"]["isp"]["vac"] ..
"|"..partJson["max_temp"] ..
"|".."{{Size|Size="..partJson["size"].."}}"
end
end
tempTable = header.."|}"
-- append the cells to the headers and cap off the table wiki text
local tempTable = header..cells.."|}"
return tempTable
return tempTable
end
end


 
-- Builds table formatted for command modules [NOT FUNCTIONAL YET]
function p._podTable(args)
function p._podTable(args)
local category = args[1]
local category = args[1]
Line 73: Line 121:
end
end


--------------------------------------------------------------------------------
---- ALL CODE BELOW TO BE REPLACED BY ALL CODE ABOVE, ONCE FULLY FUNCTIONAL ----
--------------------------------------------------------------------------------


-- args:  
-- args:  

Latest revision as of 18:28, 25 November 2024

Documentation for this module may be created at Module:Data tables/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

-- commonHeaders are used by all parts tables so they're called from here for 
-- cleaner code per category/family table
local commonHeaders = "{| class=\"wikitable\"\n" ..
		"!Image\n" ..
		"!Name\n" ..
		"!Size\n" ..
		"!Mass (t)\n" ..
		"!Max Temperature (K)\n" ..
		"!Impact Tolerance (m/s)\n"

-- commonCells pair to commonHeaders, so they are independent from the 
-- individual tables for the same reason (cleaner code)
function p._commonCells(partJson)
	local commonCells = "|-\n" ..
			"|".."[[File:"..partJson["file"].."|80px|center]]\n" ..
			"|".."[["..partJson["name"].."]]\n" ..
			"|".."{{Size|Size="..partJson["size"].."}}\n" ..
			"|"..partJson["mass"].."\n" ..
			"|"..partJson["max_temp"].."\n" ..
			"|"..partJson["imp_tol"].."\n"
	return commonCells
end

-- 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 == "command_modules" and family == "pods" then
		newTable = p._podTable(args)
	elseif category == "engines" then
		newTable = p._engineTable(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]
	
	-- adds thrust and isp values
	local header = commonHeaders
	local cells = ""
	
	-- load the full parts list to iterate per given category and family
	json = mw.loadJsonData("Data: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("Data:"..k)
		local tempCell = p._commonCells(partJson)
		cells = tempCell..cells
	end
	
	-- append the cells to the headers and cap off the table wiki text
	local tempTable = header..cells.."|}"
	return tempTable	
end


-- Builds table formatted for engines
function p._engineTable(args)
	local category = args[1]
	local family = args[2]
	
	-- adds thrust and isp values
	local header = commonHeaders ..
		"!Max Thrust: 1 atm (kN)\n" ..
		"!Max Thrust: Vac. (kN)\n" ..
		"!ISP: 1 atm (s)\n" ..
		"!ISP: Vac. (s)\n"
	local cells = ""
	
	-- load the full parts list to iterate per given category and family
	json = mw.loadJsonData("Data: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("Data:"..k)
		local tempCell = p._commonCells(partJson) ..
			"|"..partJson["modules"]["engine"]["max_thrust"]["atm"].."\n" ..
			"|"..partJson["modules"]["engine"]["max_thrust"]["vac"].."\n" ..
			"|"..partJson["modules"]["engine"]["isp"]["atm"].."\n" ..
			"|"..partJson["modules"]["engine"]["isp"]["vac"].."\n"
		cells = tempCell..cells
	end
	
	-- append the cells to the headers and cap off the table wiki text
	local tempTable = header..cells.."|}"
	return tempTable
end

-- Builds table formatted for command modules [NOT FUNCTIONAL YET]
function p._podTable(args)
	local category = args[1]
	local family = args[2]
	local tempTable = ""
	
	json = mw.loadJsonData("Data:Collections/parts")
	
	for k, v in pairs(json[category][family]) do
		local partJson = mw.loadJsonData("Data:"..k)
		local name = partJson["name"]
		local mass = partJson["mass"]
		local max_temp = partJson["max_temp"]
	end
	tempTable = name.." "..mass.." "..max_temp
	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
MediaWiki Appliance - Powered by TurnKey Linux