Modulo:Carriera sportivo

Da Uicchipèdie, 'a 'ngeclopedije lìbbere.

La documentazione per questo modulo può essere creata in Modulo:Carriera sportivo/man

local getArgs = require('Modulo:Arguments').getArgs
local p = {}

--i pattern supportati devono avere sempre 3 gruppi: (prefisso)(nome squadra)(suffisso)
local supportedPatterns = {
	'(→ *)(.*)(.-)',                         --per i trasferimenti
	'(<[^<>]*>)(.*)(< */[^<>]*>)',           --per i tag (riconosce anche {{TA}} e simili)
	'(.-)(.*)(%c\'"`UNIQ---ref.*QINU`"\'%c)' --per le note
}

local function makeTag(name, content, attributes)
	return string.format('<%s %s>%s</%s>', name, attributes or '', content or '', name)
end

local function makeTd(content, attributes)
	return makeTag('td', content, attributes)
end

local function getYearFromRange(range)
	--se l'intervallo temporale ha un formato riconosciuto, allora ricava l'ultimo anno e decrementalo di uno
	--altrimenti, se l'intervallo temporale è costituito solo da un anno, allora usa quest'ultimo
	--l'anno iniziale ammette anche i punti interrogativi
	return ( range:find('^[%d?][%d?][%d?][%d?] *[\-–] *%d%d%d%d$') and (tonumber(range:sub(-4)) - 1) )
		or ( range:find('^%d%d%d%d$') and range )
		or ''
end

local function unformat(content)
	local newContent = content
	local matchedPatterns = {}
	repeat
		local matched = false
		for i = 1, #supportedPatterns do
			local prefix, middle, suffix = newContent:match('^'..supportedPatterns[i]..'$')
			if middle and newContent ~= middle then
				matched = true
				newContent = middle
				--salvo la coppia prefisso-suffisso in prima posizione in modo che venga pescata dalla cima dello stack
				table.insert(matchedPatterns, 1, {prefix, suffix})
			end
		end
	until (not matched) --continuo il loop finché non ottengo più match
	return newContent, matchedPatterns
end

local function reformat(content, wrappers)
	local newContent = content
	for i = 1, #wrappers do
		local wrapper = wrappers[i]
		newContent = wrapper[1] .. newContent .. wrapper[2]
	end
	return newContent
end

function p.getList(frame)
	local args = getArgs(frame)
	local sport = args['sport'] or ''
	local pos = mw.title.getCurrentTitle().namespace == 0 and args['pos'] or ''
	
	--parsing preliminare per controllare che la terza colonna contenga almeno un valore
	local hasScore = false
	local rowNum = 0
	while true do
		local startIndex = rowNum * 3
		local team  = args[startIndex + 2]
		local score = args[startIndex + 3]
		if not team then break end
		if score then hasScore = true break end
		rowNum = rowNum + 1
	end
	
	local res = ''
	rowNum = 0
	while true do
		local startIndex = rowNum * 3
		local range = args[startIndex + 1] or ''
		local team  = args[startIndex + 2]
		local score = args[startIndex + 3] or ''
		
		local _, spaceCount = string.gsub(score, " ", "")
		if spaceCount <= 1 then score = makeTag('span', score, 'class="nowrap"') end
		
		if not team then break end
		
		if sport ~= '' then
			local year = getYearFromRange(range)
			local cleanedTeam, matchedPatterns = unformat(team)
			
			--chiamata protetta per verificare l'esistenza del template
			local success, result = pcall(frame.expandTemplate, frame, {
				title = string.format('Template:%s %s', sport, cleanedTeam),
				args = { pos, year }
			})
			if success then team = reformat(result, matchedPatterns) end
		end
		
		local range_cell = makeTd(range)
		local team_cell  = makeTd(team, not hasScore and 'colspan="2"' or '')
		local score_cell = hasScore and makeTd(score, '') or ''
		local content = range_cell .. team_cell .. score_cell
		local rowStyle = 'style="background:white"'
		res = res .. makeTag('tr', content, rowStyle)
		
		rowNum = rowNum + 1
	end
	
	return res
end

return p