Modül:Seri genel bakış

Vikipedi, özgür ansiklopedi
Gezinti kısmına atla Arama kısmına atla
Modül belgelemesi[oluştur]
-- Bu modül {{Seri genel bakış}} şablonunu sağlamaktadır.

require('Modül:No globals')
local yesno = require('Modül:Yesno')

--------------------------------------------------------------------------------
-- Seri genel bakış biçimi
-- Ana biçim.
---------------------------------m-----------------------------------------------

local SeriesOverview = {}

function SeriesOverview.cellspan(SeasonEntries, SeasonEntries_ordered, key, cell)
	local spanlength = 1
	for i = cell+1, #SeasonEntries_ordered do
		local entry = SeasonEntries[SeasonEntries_ordered[i]]
		-- Split season, then regular season
		if entry.baslaA then
			if not entry[key..'A'] then spanlength = spanlength + 1
			else break end
			if not entry[key..'B'] then spanlength = spanlength + 1
			else break end
		else
			if not entry[key] then spanlength = spanlength + 1
			else break end
		end
	end
	return spanlength
end

function SeriesOverview.new(frame, args)
	args = args or {}
	local categories = ''
	local title = mw.title.getCurrentTitle()
	local allReleased = yesno(args.allreleased)

	-- Create series overview table
	local root = mw.html.create('table')
	local cellPadding = '0 8px'

	root
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:css('text-align', 'center')

	-- Caption
	if args.caption then
		root:tag('caption'):wikitext(args.caption)
	end
	
	-- Sorting function
	local function series_sort(op1, op2)
	    local n1,s1 = string.match(op1,"(%d+)(%a*)");
	    local n2,s2 = string.match(op2,"(%d+)(%a*)");
	    local n1N,n2N = tonumber(n1),tonumber(n2)
	
	    if n1N == n2N then
	        return s1 < s2
	    else
	        return n1N < n2N
	    end
	end
	
	-- Allow usages of {{N/A}} cells
	local function series_attributes(infoParam)
		local entries = {}
		local infoCell = mw.html.create('td')
		
		while true do
			local a,b = string.match(infoParam,'(%a*)="([^"]*)"');
			if a == nil or b == nil then break end
			infoCell:attr(a,b)
			infoParam = string.gsub(infoParam,'(%a*)="([^"]*)"','',1)
		end
	
		infoParam = string.gsub(infoParam,'%s*|%s*','',1)
		infoCell:wikitext(infoParam)
		
		return infoCell
	end
	
	-- Extract seasons info and place into a 3D array
	local SeasonEntries = {};
	for k,v in pairs(args) do
		local str, num, str2 = string.match(k, '([^%d]*)(%d*)(%a*)')
		if num ~= '' then 
			-- Special
			local special = false
			if string.sub(str2,1,1) == 'S' then
				special = true
				num = num .. str2
				str2 = ''
			end
			-- Add to entries, create if necessary
			if not SeasonEntries[num] then
				SeasonEntries[num] = {}
			end
			SeasonEntries[num][str .. str2] = v
			if special then
				SeasonEntries[num]['special'] = 'y'
			end
		end
	end

	-- Order table by season number
	local SeasonEntries_ordered = {}
	for k in pairs(SeasonEntries) do
		table.insert(SeasonEntries_ordered, k)
	end
	table.sort(SeasonEntries_ordered,series_sort)
	
	local firstRow = SeasonEntries[SeasonEntries_ordered[1]]
	
	-- Colspan calculation for information cells (0 = no info set)
	local numInfoCells = 0;
	for i = string.byte('A'), string.byte('Z') do
		local param = 'ekstra' .. string.char(i)
		if args[param] then numInfoCells = numInfoCells + 1 end
	end
	
	-- Top info cell
	-- @ = string.char(64), A = string.char(65)
	local topInfoCell = args.ekstrabaslik and (numInfoCells > 0 and string.char(numInfoCells + (string.byte('A') - 1)) or 'A') or '@'

	-- Headers
	do
		local headerRow = root:tag('tr')
		headerRow
			:css('text-align', 'center')

		-- Networks are included if the very first entry sets the first network
		local setNetwork = firstRow.kanal or firstRow.kanalA
		local releasedBlurb = args.released and 'tarihi' or 'tarihi'
		
		-- Base series/season content on the format of the first date; Series = D M Y, Season = M D, Y
		local matchDMY = false
		local thisStart = firstRow.basla or firstRow.baslaA
		if string.match(thisStart:gsub("&nbsp;"," "), '(%d+)%s(%a+)%s(%d+)') then
			matchDMY = true
		end
		
		-- Season header
		headerRow:tag('th')
			:attr('scope', 'col')
			:attr('rowspan', allReleased and numInfoCells == 0 and 1 or 2)
			:attr('colspan', 2)
			:css('padding', cellPadding)
			:wikitext(args.sezonT or 'Sezon')
		
		-- Aux headers
		for i = string.byte('A'), string.byte('Z') do
			local param = 'fazla' .. string.char(i)
			if args[param] then
				headerRow:tag('th')
					:attr('scope', 'col')
					:css('padding', cellPadding)
					:attr('rowspan', allReleased and numInfoCells == 0 and 1 or 2)
					:wikitext(args[param])
			end
		end

		-- Episodes header
		headerRow:tag('th')
			:attr('scope', 'col')
			:attr('rowspan', allReleased and numInfoCells == 0 and 1 or 2)
			:attr('colspan', 2)
			:css('padding', cellPadding)
			:wikitext('Bölüm<br>sayısı')

		-- Originally aired header
		headerRow:tag('th')
			:attr('scope', 'col')
			:attr('rowspan', allReleased and numInfoCells > 0 and 2 or 1)
			:attr('colspan', setNetwork and 3 or 2)
			:wikitext('Orijinal yayın ' .. releasedBlurb)
		
		-- Network subheader for released series
		if setNetwork and allReleased then
			headerRow:tag('th')
				:attr('scope', 'col')
				:attr('rowspan', allReleased and numInfoCells == 0 and 1 or 2)
				:css('padding', cellPadding)
				:wikitext('Kanal')
		end
		
		-- Subheader row
		local subheaderRow = mw.html.create('tr')

		-- Info header
		if args.ekstrabaslik then
			headerRow:tag('th')
				:attr('scope', 'col')
				:attr('colspan', numInfoCells > 0 and numInfoCells or nil)
				:attr('rowspan', (allReleased and 1) or (numInfoCells > 0 and 1) or 2)
				:css('padding', cellPadding)
				:wikitext(args.ekstrabaslik)
		end

		if not allReleased then
			-- First aired subheader
			subheaderRow:tag('th')
				:attr('scope', 'col')
				:wikitext('Başlangıç ' .. releasedBlurb)

			-- Last aired subheader
			subheaderRow:tag('th')
				:attr('scope', 'col')
				:wikitext('Bitiş ' .. releasedBlurb)
			
			-- Network subheader for aired series
			if setNetwork then
				subheaderRow:tag('th')
					:attr('scope', 'col')
					:css('padding', cellPadding)
					:wikitext('Kanal')
			end
		end

		-- Information subheaders, only if the infoheader doesn't span down (i.e. 2+ info cells set)
		if topInfoCell ~= 'A' then
			for i = string.byte('A'), string.byte(topInfoCell) do
				local param = 'ekstra' .. string.char(i)
				subheaderRow:tag('th')
					:attr('scope', 'col')
					:css('padding', cellPadding)
					:wikitext(args[param])
			end
		end
	
		-- Check for scenarios with an empty subheaderRow
		if not allReleased or numInfoCells > 0 then
			root:node(subheaderRow);
		end
	end

	-- Season rows
	do
		-- One row entries, only categorized in the mainspace
		if title.namespace == 0 and #SeasonEntries == 1 then
			categories = categories .. '[[Kategori:"Şablon:Seri genel bakış" şablonunu yalnızca bir satırlık kullanan maddeler]]'
		end
	
		for X = 1, #SeasonEntries_ordered do
			local season, entry = SeasonEntries_ordered[X], SeasonEntries[SeasonEntries_ordered[X]]
			local splitSeason = entry.baslaA
			
			-- New season row
			local seasonRow = root:tag('tr');
			
			-- Dates
			local startDate = entry['basla']
			local endDate = entry['bitis']
			
			-- First season row
			
			-- Coloured cell
			seasonRow:tag('td')
				:attr('scope','row')
				:attr('rowspan', ((splitSeason and entry.renk) or (entry.renkA and entry.renkA == entry.renkB)) and 2 or nil)
				:css('background',entry.renkA or entry.renk)
				:css('width','10px')
			
			-- Season number link
			seasonRow:tag('td')
				:attr('rowspan', splitSeason and 2 or nil)
				:attr('colspan', entry.special and not entry.bolum and 3 or 1)
				:wikitext(entry.link and '[[' .. entry.link .. '|' .. (entry.linkT or season) .. ']]' or (entry.linkT or season))
		
			-- Aux cells
			for i = string.byte('A'), string.byte('Z') do
				local param = 'fazla' .. string.char(i)
				if entry[param] or entry[param..'A'] then
					seasonRow:tag('td')
						:attr('scope', 'col')
						:attr('rowspan', SeriesOverview.cellspan(SeasonEntries, SeasonEntries_ordered, param, X))
						:css('padding', cellPadding)
						:wikitext(entry[param] or entry[param .. 'A'])
				end
			end
			
			-- Episodes
			if entry.bolum then
				seasonRow:tag('td')
					:attr('colspan', splitSeason and 1 or 2)
					:attr('rowspan', splitSeason and 2 or nil)
					:wikitext(entry.bolum)
			elseif not entry.special then
				local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
				infoCell
					:attr('colspan', splitSeason and 1 or 2)
					:attr('rowspan', splitSeason and 2 or nil)
				seasonRow:node(infoCell);
			end
			
			-- Episodes for first half of split season
			if splitSeason then
				if entry.bolumA then
					seasonRow:tag('td')
						:wikitext(entry.bolumA or frame:expandTemplate{title='TableTBA'})
				else
					local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
					seasonRow:node(infoCell);
				end
			end
			
			-- Start date
			if entry.baslaA or startDate then
				seasonRow:tag('td')
					:attr('colspan', ((not entry.special and endDate == 'basla') or (entry.special and not endDate)) and 2 or 1)
					:css('padding',cellPadding)
					:wikitext(entry.baslaA or startDate)
			else
				local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}})
				infoCell:css('padding',cellPadding)
				seasonRow:node(infoCell);
				if title.namespace == 0 then
					categories = categories .. '[[Kategori:"Şablon:Seri genel bakış" şablonunu boş başlangıç zamanı ile kullanan maddeler]]'
				end
			end
			
			-- End date
			if not allReleased and endDate ~= 'basla' and ((entry.special and endDate) or not entry.special) then
				if entry.bitisA or endDate then
					seasonRow:tag('td')
						:css('padding',cellPadding)
						:wikitext(entry.bitisA or endDate)
				else
					local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
					infoCell:css('padding',cellPadding)
					seasonRow:node(infoCell);
				end
			end
			
			-- Network
			if entry.kanal or entry.kanalA then
				seasonRow:tag('td')
					:attr('rowspan', SeriesOverview.cellspan(SeasonEntries, SeasonEntries_ordered, 'kanal', X))
					:wikitext(entry.kanalA or entry.kanal)
			end
			
			-- Information
			for i = string.byte('A'), string.byte(topInfoCell) do
				local param = 'ekstra' .. string.char(i)
				local infoParam = entry[param .. 'A'] or entry[param]
				if infoParam then
					-- Cells with {{N/A|...}} already expanded
					if string.sub(infoParam,1,5) == 'style' then
						local infoCell = series_attributes(infoParam)
						infoCell:attr('rowspan', (splitSeason and entry[param]) and 2 or nil)
						seasonRow:node(infoCell);
					else
						-- Unstyled content info cell
						seasonRow:tag('td')
							:wikitext(infoParam)
							:attr('rowspan', (splitSeason and entry[param]) and 2 or nil)
					end
				else
					local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
					infoCell:attr('rowspan', (splitSeason and entry[param]) and 2 or nil)
					seasonRow:node(infoCell);
				end
			end
			
			-- Second season row for split seasons
			if splitSeason then
				-- Second row
				local seasonRowB = root:tag('tr');
				
				-- Coloured cell
				if not entry.renk and entry.renkB and entry.renkA ~= entry.renkB then
					seasonRowB:tag('td')
						:attr('scope','row')
						:css('background',entry.renkB)
						:css('width','10px')
				end
				
				-- Aux cells
				for i = string.byte('A'), string.byte('Z') do
					local param = 'fazla' .. string.char(i)
					if entry[param..'B'] then
						seasonRowB:tag('td')
							:attr('scope', 'col')
							:attr('rowspan', SeriesOverview.cellspan(SeasonEntries, SeasonEntries_ordered, param, X))
							:css('padding', cellPadding)
							:wikitext(entry[param .. 'B'])
					end
				end
				
				-- Episodes for first half of split season
				seasonRowB:tag('td')
					:wikitext(entry.bolumB or frame:expandTemplate{title='TableTBA'})
				
				-- Start date
				seasonRowB:tag('td')
					:attr('colspan',entry.bitisB == 'basla' and 2 or 1)
					:css('padding',cellPadding)
					:wikitext(entry.baslaB)
				
				-- End date
				if not allReleased and entry.bitisB ~= 'basla' then
					if entry.bitisB then
						seasonRowB:tag('td')
							:css('padding',cellPadding)
							:wikitext(entry.bitisB)
					else
						local infoCell = series_attributes( frame:expandTemplate{title='N/A',args={'TBA'}} )
						infoCell:css('padding',cellPadding)
						seasonRowB:node(infoCell);
					end
				end
				
				-- Network
				if entry.kanalB then
					seasonRowB:tag('td')
						:attr('rowspan', SeriesOverview.cellspan(SeasonEntries, SeasonEntries_ordered, 'kanal', X))
						:wikitext(entry.kanalB)
				end
				
				-- Information
				for i = string.byte('A'), string.byte('Z') do
					local param = 'ekstra' .. string.char(i)
					if entry[param .. 'B'] then
						seasonRowB:tag('td')
							:wikitext(entry[param .. 'B'])
					end
				end
			end -- End 'if' splitSeason
		end -- End 'for' SeasonEntries_ordered
	end -- End 'do' season rows

	return tostring(root) .. categories
end

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------

local p = {}

function p.main(frame)
	local args = require('Modül:Arguments').getArgs(frame, {
		wrappers = 'Şablon:Seri genel bakış'
	})
	return SeriesOverview.new(frame, args)
end

return p