Jump to content

Module:Tabular data

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mxn (talk | contribs) at 22:16, 9 May 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Example usage:
-- =p.wikitable(mw.getCurrentFrame():newChild{ args = { "COVID-19 Cases in Santa Clara County, California.tab" } })

local p = {}
local lang = mw.getContentLanguage()

function p.wikitable(frame)
	local data = mw.ext.data.get(frame.args[1])
	
	local datatypes = {}
	
	local htmlTable = mw.html.create("table")
		:attr("class", "wikitable sortable")
	htmlTable
		:tag("caption")
		:wikitext(data.description)
	
	local headerRow = htmlTable
		:tag("tr")
	for i, field in ipairs(data.schema.fields) do
		headerRow
			:tag("th")
			:attr("scope", "col")
			:wikitext(field.title)
		datatypes[i] = field.type
	end
	
	for i, record in ipairs(data.data) do
		local row = htmlTable:tag("tr")
		for j = 1, #record do
			local cell = row:tag("td")
			if cell then
				local formattedData = record[j]
				if datatypes[j] == "number" then
					formattedData = lang:formatNum(formattedData)
					cell:attr("align", "right")
				end
				cell:wikitext(formattedData)
			else
				cell:wikitext(frame:expandTemplate {
					title = "n/a",
				})
			end
		end
	end
	
	local footer = htmlTable
		:tag("tr")
		:tag("td")
		:attr("colspan", #data.schema.fields)
	footer:wikitext(data.sources)
	footer:tag("br")
	
	local licenseText = mw.message.new("Jsonconfig-license",
		mw.ustring.format("[%s %s]", data.license.url, data.license.text))
	footer
		:tag("i")
		:wikitext(tostring(licenseText))
	
	return htmlTable
end

return p