Moduł:Brudnopis/Paweł Ziemian/Kraj/loader

Z Wikipedii, wolnej encyklopedii

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/Paweł Ziemian/Kraj/loader/opis

return {

load = function(country, variant)
	local moduleprefix = "Module:Brudnopis/Paweł Ziemian/Kraj/"
	local moduleTitle = false
	local countryData = false
	if country then
		local moduletitle = mw.title.new(country)
		if not moduletitle or (moduletitle.namespace ~= 828) then
			moduletitle = mw.title.new(moduleprefix..country)
		end
		if not moduletitle then
			mw.log("„"..country.."” nie jest prawidłową nazwą definicji danych kraju")
		elseif moduletitle.exists then
			local status, countrydata = pcall(mw.loadData, moduletitle.fullText)
			if status then
				moduleTitle = moduletitle
				countryData = countrydata
			end
		end
	end

	if not countryData then
		mw.log("Ładuję substytut danych kraju")
		countryData = mw.loadData(moduleprefix.."brak")
	end

	local metatable, methodtable = {}, {}
	metatable.__index = methodtable
	
	methodtable.country = function()
		return country
	end
	
	methodtable.title = function()
		return moduleTitle
	end
	
	methodtable.data = function()
		return countryData
	end
	
	methodtable.get = function(self, name1, name2)
		local variants = self.variant
		if not variants then
			variants = { 1 }
		elseif type(variants) ~= "table" then
			variants = { variants, 1 }
		end
		
		for i, v in ipairs(variants) do
			local variantData = countryData[v]
			if variantData then
				local result1 = variantData[name1]
				if result1 ~= nil then
					return result1, variantData[name2]
				end
			end
		end
	end

	methodtable.getv = function(self, name, variant)
		local variantData = countryData[variant]
		return variantData and variantData[name] or nil
	end
	
	methodtable.drawFlag = function(self, builder, size, alt, link, prefix, suffix)
		local flag, border = self:get("flaga", "border")
		if flag and (#flag > 0) then
			builder:tag("span")
				:addClass("flagicon")
				:wikitext(prefix or "", "[[Plik:", flag, "|", size, border ~= false and "|border" or "", "|alt=", alt, "|link=", link, "]]", suffix or "")
		end
		
		local specialCat = self:get("specjalna kategoria")
		if specialCat then
			builder:wikitext(specialCat)
		end
	end
	
	methodtable.drawName = function(self, builder, name, link)
		builder:wikitext("[[", link or self:get("link"))
		if not name then
			name = self:get("nazwa")
		end
		
		if not name and link then
			name = self:get("link")
		end
		
		if name then
			builder:wikitext("|", name)
		end
		
		builder:wikitext("]]")
	end
	
	methodtable.drawTeamName = function(self, builder, link, name)
		local variant = self.variant
		local genitiv = self:getv("dopełniacz", variant) or self:getv("dopełniacz", "reprezentacja") or self:getv("dopełniacz", 1) or "{{{dopełniacz}}}"
		local nameLink = self:getv("nazwa", variant) or self:getv("link", variant) or self:getv("nazwa", 1) or self:getv("link", 1) or link
		builder:wikitext("[[Reprezentacja ", genitiv, " ", link or "", "|", nameLink, name and " " or "", name or "", "]]")
	end
		
	local result = {}
	setmetatable(result, metatable)
	result.variant = variant or country
	return result
end,

}