Module:SongContestArtist

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

local p = {}

local dataModule = require('Module:SongContestData')

local function getData(a,b,c)
	return dataModule.main({a,b,c})
end

local function formatArtist(entry, artistType, num)
	local name    = entry[artistType .. num]
	local first   = entry[artistType .. "_first"   .. num]
	local last    = entry[artistType .. "_last"    .. num]
	local article = entry[artistType .. "_article"] or entry[artistType .. "_article" .. num]

	if name then
		if article then
			return string.format("[[%s|%s]]", article, name)
		else
			return name
		end
	end
	if first and last then
		local sortname = require('Module:Sort').name
		if article then
			return sortname({first, last, article})
		else
			return sortname({first, last, article, nolink=1})
		end
	end

	return nil
end

local function getArtists(entry, artistType)
	local list = {}
	local n = 1
	while true do
		local artist = formatArtist(entry, artistType, n)
		if not artist then break end
		table.insert(list, artist)
		n = n + 1
	end
	return list
end

function p.main(f)
	local contest = f.args[1]
	local year    = f.args[2]
	local country = f.args[3]

	local entry = require('Module:SongContestData').getEntryData({contest, year, country})
	if not entry then return "" end

	local mainArtists = getArtists(entry, "artist")
	local featArtists = getArtists(entry, "artist_feat")

	local output = mw.text.listToText(mainArtists, ", ", " and ")
	if #featArtists > 0 then
		feat = f:expandTemplate{title="Template:Feat."}
		output = output .. " "..feat.." " .. mw.text.listToText(featArtists, ", ", " and ")
	end

	return output
end

return p