Модуль:Табліца эпізодаў

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі

Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Табліца эпізодаў/Дакумэнтацыя

-- Гэты модуль выкарыстоўваецца ў шаблёне {{Табліца эпізодаў}}.

local HTMLcolor = mw.loadData( 'Модуль:Кантраст колераў/колеры' )

--------------------------------------------------------------------------------
-- Кляс EpisodeTable
-- Асноўны кляс.
--------------------------------------------------------------------------------

local contrast_ratio = require('Модуль:Кантраст колераў')._ratio
local EpisodeTable = {}

function EpisodeTable.cell(background, width, text, reference)
	local cell = mw.html.create('th')
	
	-- Ячэйка
	cell:attr('scope','col')
		:css('background',background or '#CCCCFF')
		:css('width',width ~= '' and width .. '%' or nil)
		:wikitext(text)
	
	-- Крыніца
	if reference and reference ~= '' then
		cell:wikitext(" " .. EpisodeTable.reference(reference, background))
	end
	
	return cell
end

function EpisodeTable.reference(reference, background)
	local link1_cr = contrast_ratio{'#0645AD', background or '#CCCCFF', ['error'] = 0}
	local link2_cr = contrast_ratio{'#0B0080', background or '#CCCCFF', ['error'] = 0}
	
	local refspan = mw.html.create('span')
		:wikitext(reference)
	
	if link1_cr < 7 or link2_cr < 7 then
		refspan
			:css('background-color','white')
			:css('padding','1px')
			:css('display','inline-block')
			:css('line-height','50%')
	end
	
	return tostring(refspan)
end

function EpisodeTable.abbr(text,title)
	local abbr = mw.html.create('abbr')
		:attr('title',title)
		:wikitext(text)
	return tostring(abbr)
end

function EpisodeTable.part(frame,args)
	local row = mw.html.create('tr')
	
	local black_cr = contrast_ratio{args.c, 'black', ['error'] = 0}
	local white_cr = contrast_ratio{'white', args.c, ['error'] = 0}
	
	local displaytext = (not args.nopart and 'Частка ' or '') .. (args.p or '')
	
	local plainText = require('Модуль:Просты тэкст')._main
	local displayTextAnchor = plainText(displaytext)
	
	row:tag('td')
		:attr('colspan', 13)
		:attr('id', displayTextAnchor)
		:css('text-align', 'center')
		:css('background-color', args.c)
		:css('color', black_cr > white_cr and 'black' or 'white')
		:wikitext("'''" .. displaytext .. "'''" .. (args.r and "&#8202;" .. EpisodeTable.reference(args.r, args.c) or ''))
	
	return tostring(row)
end

function EpisodeTable.new(args)
	args = args or {}
	local categories = ''
	local background = (args.background and args.background ~= '' and args.background ~= '#') and args.background or nil
	
	-- Пры патрэбе дадаць да колеру фону сымбаль #
	if background ~= nil and HTMLcolor[background] == nil then
		background = '#'..(mw.ustring.match(background, '^[%s#]*([a-fA-F0-9]*)[%s]*$') or '')
	end
	
	-- Дапомная шырыня
	local defaultwidths = {};
	defaultwidths['скразны'] = 5;
	defaultwidths['сэзон'] = 5;
	defaultwidths['шэраг'] = 5;
	defaultwidths['прадкод'] = 7;
	defaultwidths['гледачы'] = 10;
	
	-- Стварыць табліцу сэрыяў
	local root = mw.html.create('table')
	
	root
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:addClass('wikiepisodetable')
		:css('width', args.total_width and string.gsub(args.total_width,'%%','') .. '%' or '100%')
	
	-- Назва
	if args['назва'] then
		root:tag('caption'):wikitext(args['назва'])
	end
	
	-- Кантраст колераў; дадаваць у катэгорыю, толькі калі ў асноўнай прасторы
	local title = mw.title.getCurrentTitle()
	local black_cr = contrast_ratio{background, 'black', ['error'] = 0}
	local white_cr = contrast_ratio{'white', background, ['error'] = 0}
	
	if title.namespace == 0 and (args.background and args.background ~= '' and args.background ~= '#') and black_cr < 7 and white_cr < 7 then
		categories = categories .. '[[Катэгорыя:Вікіпэдыя:Патэнцыйна нечытэльныя артыкулы]]' 
	end
	
	-- Галоўны шэраг
	local mainRow = root:tag('tr')
	mainRow
		:css('color', background and (black_cr > white_cr and 'black' or 'white') or 'black')
		:css('text-align', 'center')
	
	-- Ячэйкі
	do
		local used_season = false
		local country = args['краіна'] ~= '' and args['краіна'] ~= nil
		local viewers = (country and args['краіна'] or '') .. ' ' .. (country and 'г' or 'Г') .. 'ледачоў' ..
			((not args['гледачы_адзінка'] or args['гледачы_адзінка'] ~= '') and '<br />(' .. (args['гледачы_адзінка'] or 'млн') .. ')' or '')
		
		local cellNames = {
			{'скразны','СкразныНумар',EpisodeTable.abbr('№','Нумар') ..
				((args['сэзон'] or args['шэраг'] or args['СэзонныНумар'] or args['ПарадкавыНумар'] or args.forceoverall) and '<br />агульны' or '')},
			{'сэзон','СэзонныНумар',EpisodeTable.abbr('№','Нумар') .. ' у<br />сэзоне'},
			{'шэраг','ПарадкавыНумар',EpisodeTable.abbr('№','Нумар') .. ' у<br />пасьлядоўнасьці'},
			{'назва','Назва','Назва'},
			{'дад1','Дад1',''},
			{'рэжысэр','Рэжысэр','Рэжысэр'},
			{'сцэнарыст','Сцэнарыст','Сцэнарыст'},
			{'дад2','Дад2',''},
			{'дад3','Дад3',''},
			{'датавыхаду','ДатаВыхаду','Першапачатковая дата' .. (args['выйшаў'] and ' выхаду' or ' паказу')},
			{'іншаядата','ІншаяДата',''},
			{'госьці','Госьці','Гасьцей'},
			{'музгосьці','МузычныяГосьці','Музычных гасьцей'},
			{'прадкод','ПрадКод','код<br />' .. EpisodeTable.abbr('прад.','прадукцыі')},
			{'гледачы','Гледачы',viewers},
			{'дад4','Дад4',''}
		}
	
		for k,v in pairs(cellNames) do
			local thisCell = args[v[1]] or args[v[2]]
			if thisCell and (v[1] ~= 'шэраг' or (v[1] == 'шэраг' and used_season == false)) then
				if v[1] == 'сэзон' then used_season = true end
				if (k <= 3 and thisCell == '') then thisCell = '5' end
				if (thisCell == '' and defaultwidths[v[1]]) then thisCell = defaultwidths[v[1]] end
				
				local thisCellT = args[v[1] .. 'T'] or args[v[2] .. 'T']
				local thisCellR = args[v[1] .. 'R'] or args[v[2] .. 'R']
				mainRow:node(EpisodeTable.cell(background, thisCell, thisCellT or v[3], thisCellR))
			end
		end
	
		-- Сэрыі
		if args.anchor then 
			args['сэрыі'] = string.gsub(args['сэрыі'], "(id=\")(ep%w+\")", "%1" .. args.anchor .. "%2")
		end
		
		root:node(args['сэрыі'])
	end
	
	return (args.dontclose and mw.ustring.gsub(tostring(root), "</table>", "") or tostring(root)) .. categories
end

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

local p = {}

function p.main(frame)
	local args = require('Модуль:Аргумэнты').getArgs(frame, {
		removeBlanks = false,
		wrappers = 'Шаблён:Табліца эпізодаў'
	})
	return EpisodeTable.new(args)
end

function p.part(frame)
	local args = require('Модуль:Аргумэнты').getArgs(frame, {
		removeBlanks = false,
		wrappers = 'Шаблён:Табліца эпізодаў/частка'
	})
	return EpisodeTable.part(frame,args)
end

function p.ref(frame)
	local args = require('Модуль:Аргумэнты').getArgs(frame, {
		removeBlanks = false,
	})
	return EpisodeTable.reference(args.r,args.b)
end

return p