Jump to content

Module:Political party/testtable

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 11:15, 19 November 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local function isColorValid(color)
	if not color or color == "" then
		mw.log("here")
		return
	end

	-- Convert to lowercase.
	color = color:lower()
	
	-- Check if color is using an HTML color name.
	local HTMLcolor = mw.loadData('Module:Color contrast/colors')
	local color_name = HTMLcolor[color]
	if color_name then
		return
	end

	-- Remove leading # if there is one.
	color = string.gsub(color, "^#", "")

	local cs = mw.text.split(color, '')
	if #cs == 6 or #cs == 3 then
		return
	end
	
	mw.log(color)
	return "" -- TODO: return an error.
end

-- Example of having all the data - color and names - in one table. Requires one page to be edited instead of two when adding a new party.
function p.tables(frame)
	-- Initialise and populate variables
	local args = frame.args
	local index = args.letter
	
	-- Load data from submodule
	local data = mw.loadData('Module:Political party/' .. index)

	-- helper function
	local function table_row(party_name, party_info)
		local res = mw.html.create('')
		res:tag('td')
			:wikitext(party_name)
		res:tag('td')
			:css('background-color', party_info.color)
			:wikitext(party_info.color)
		res:tag('td')
			:wikitext(party_info.abbrev)
		res:tag('td')
			:wikitext(party_info.shortname)
		return tostring(res)
	end

	-- build table
	local root = mw.html.create('table')
	root:addClass('wikitable sortable')
		:css('background-color', 'transparent')
		:css('font-size', '90%')
		:css('line-height', '100%')
		:cssText(style)
	local row = root:tag('tr')
	row:tag('th')
		:wikitext('Political party name')
	row:tag('th')
		:addClass('unsortable')
		:wikitext('color')
	row:tag('th')
		:wikitext('abbrev')
	row:tag('th')
		:wikitext('shortname')

	for party_name, party_vals in pairs(data.full) do
		isColorValid(party_vals.color)
		row = root:tag('tr')
		row:wikitext(table_row(party_name, party_vals))
	end
	return tostring(root)

end

return p