Module:TableBuilder
Jump to navigation
Jump to search
Documentation for this module may be created at Module:TableBuilder/doc
--------------------------------------------------------------------------------
-- Builds wikitext tables from a lua table of rows and columns
-- The data source is specified in source and is in the format Module:Foo/Bar
-- Module:Foo/Bar should be a table with entries formatted like
-- ["data 1"] = {{"1", "2"}, {"3", "4"}}
-- where "1" and "2" are the first row and "3" and "4" are the second row, etc.
-- if topHeader is "1", the topmost row will be formatted as a header
-- if leftHeader is "1", the leftmost column will be formatted as a header
-- interface is meant to be called with {{PAGENAME}} and expects an argument
-- of the form Country Name (Scenario Name)
--------------------------------------------------------------------------------
local returnValue = {}
function returnValue.countryInterface(frame)
local dataPreprocessor = require("Module:DataPreprocessor")
local processedDataTable = dataPreprocessor.process(frame)
local source = processedDataTable[1]
local entry = processedDataTable[2]
return frame:preprocess("==City Statistics==\n"..returnValue.buildWikitext("Module:Data/Tables/"..source, entry))
end
function returnValue.scenarioInterface(frame)
local dataPreprocessor = require("Module:DataPreprocessor")
local entry = frame.args[1]
return frame:preprocess("==Nation Statistics==\n"..returnValue.buildWikitext("Module:Data/Scenarios/Tables", entry))
end
function returnValue.buildWikitext(source, entry)
local dataSource = mw.loadData(source)
if dataSource == nil then
error("Data source not found")
return
end
local dataEntry = dataSource[entry]
if dataEntry == nil then
return "N/A"
end
local wikitext = ""
for tableIndex, tableData in ipairs(dataEntry) do
local wikitextData = tableData["data"]
if wikitextData == nil then
error("Table data not found")
end
local highlightTopHeader = tableData["topHeader"]
local highlightLeftHeader = tableData["leftHeader"]
-- if topHeader or leftHeader aren't defined, nil is treated as false
wikitext = wikitext..'{| class="article-table mw-collapsible sortable"\n\n|+'..tableData["title"]
for rowIndex, row in ipairs(wikitextData) do
wikitext = wikitext.."\n\n|-"
for columnIndex, column in ipairs(row) do
if type(column) == "string" then
if (highlightTopHeader and rowIndex == 1) or (highlightLeftHeader and columnIndex == 1) then
wikitext = wikitext.."\n\n!"..column
else
wikitext = wikitext.."\n\n|"..column
end
end
end
end
wikitext = wikitext.."\n\n|}"
wikitext = wikitext.."\n\n''Last Updated "..tableData["lastUpdated"].."''"
wikitext = wikitext.."\n\n[[Category:Pages that use the "..source.." data table]]\n\n"
end
return wikitext
end
return returnValue