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 21:50, 9 May 2020 (Created page with 'local p = {} local lang = mw.getContentLanguage() function p.wikitable(frame) local data = mw.ext.data.get(frame.args.page) local datatypes = {} local ht...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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

function p.wikitable(frame)
	local data = mw.ext.data.get(frame.args.page)
	
	local datatypes = {}
	
	local htmlTable = mw.html.create("table")
	htmlTable
		:tag("caption")
		:wikitext(data.description)
	
	local headerRow = htmlTable
		:tag("thead")
		: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
	
	local body = htmlTable:tag("tbody")
	for i, record in ipairs(data.data) do
		local row = body:tag("tr")
		for j, datapoint in ipairs(record) do
			local cell = row:tag("td")
			if cell then
				local formattedData = datapoint
				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("tfoot")
		:tag("tr")
		:tag("td")
		:attr("colspan", #data.schema.fields)
	footer:wikitext(data.license)
	footer:tag("br")
	footer:wikitext(data.sources)
	
	mw.logObject(data.license)
	return htmlTable
end

return p