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

Module:Data tables

From Kerbal Space Program 2 Wiki
Revision as of 00:10, 13 September 2024 by KiwiShark (talk | contribs) (Import data table pull module with initial tweaks to work here)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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

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