Jump to content

Module:Political party linked

From Wikipedia, the free encyclopedia
local p = {}

local party_info
local link = true

local categories = {
	party_not_in_list = '[[Category:Pages using Political party with unknown party]]',
	shortname_not_in_list = '[[Category:Pages using Political party with missing shortname]]',
	color_not_in_list = '[[Category:Pages using Political party with missing color]]',
}

local function create_error(error_message)
	return string.format('<strong class="error">%s</strong>', error_message)
end

local function getFirstLetter(party)
	local index = mw.ustring.sub(party, 1, 1)
	-- Set index for non-A-Z starts
	if string.match(index, '%A') then
		return '1'
	end
	return string.upper(index)
end

local function page_exist(page_name)
	--if require('Module:Disambiguation')._isDisambiguationPage(page_name) then return false end
	if mw.title.new(page_name).exists then return true end
	return false
end

local function stripToNil(text)
	-- If text is a string, return its trimmed content, or nil if empty.
	-- Otherwise return text (which may, for example, be nil).
	if type(text) == 'string' then
		text = text:match('(%S.-)%s*$')
		local delink = require('Module:Delink')._delink
		text = delink({text, wikilinks = "target"})
	end
	return text
end

function p._fetch(args)
	if not args[1] then
		return create_error("parameter 1 should be a party name.")
	end

	if not args[2] then
		return create_error("parameter 2 should be the output type.")
	end

	local party = stripToNil(args[1])
	local out_type = stripToNil(args[2])
	local index = getFirstLetter(party)

	-- Load data from submodule
	local data = mw.loadData('Module:Political party/' .. index)
	local data_all = data.full

	local party_alt = data.alternate[party]
	if party_alt then
		if party_alt:find("S%)") or not page_exist(party_alt)--[[temporaries, should be fixed--]] or require('Module:Political party linked/exempt')[party] then link = false else party = party_alt end --leechers
		if data_all[party_alt] then
			party_info = data_all[party_alt]
		else
			index = getFirstLetter(party_alt)
			data = mw.loadData('Module:Political party/' .. index)
			party_info = data.full[party_alt]
		end
	else
		party_info = data_all[party]
	end

	-- Check if database value exists
	-- * Not even in database - return given error or input
	-- * No shortname/abbrev - return first non-blank of abbrev->shortname->input
	if not party_info then return args.error or party end
	local display_name = party_info[out_type]
	if display_name == "" then
		if out_type == 'shortname' then
			if party_info.abbrev ~= "" then
				display_name = party_info.abbrev
			else
				display_name = party
			end
		elseif out_type == 'abbrev' then
			if party_info.shortname ~= "" then
				display_name = party_info.shortname
			else
				display_name = party
			end
		else
			display_name = party
		end
	elseif not display_name then
		if out_type == 'full' then
			display_name = mw.ustring.gsub(party, '%s+%b()$', '')
		elseif out_type == 'dab' then
			display_name = party
		elseif out_type == '_shortname' then
			display_name = args[3]
		else
			return nil
		end
	end

	if link and party_info.link then
		if party_info.link == "" then return display_name end
		return string.format("[[%s|%s]]", party_info.link, display_name)
	elseif page_exist(party) then
		return string.format("[[%s|%s]]", party, display_name)
	else
		return display_name
	end
end

function p._tagCfetch(args)
	local linked_party = p._fetch(args)
	local color = party_info and (party_info.color ~= "") and party_info.color or "#F8F9FA"
	return linked_party, color
end

function p.fetch(frame)
	-- Initialise and populate variables
	local getArgs = require("Module:Arguments").getArgs
	local args = getArgs(frame)

	return p._fetch(args)
end

return p