모듈:Episode table

-- This module implements {{Episode table}} and {{Episode table/part}}.

local HTMLcolor = mw.loadData( 'Module:Color contrast/colors' )

--------------------------------------------------------------------------------
-- EpisodeTable class
-- The main class.
--------------------------------------------------------------------------------

local contrast_ratio = require('Module:Color contrast')._ratio
local EpisodeTable = {}

function EpisodeTable.cell(background, width, text, reference, textColor)
	local cell = mw.html.create('th')
	
	-- Width
	local cell_width
	if width == 'auto' then
		cell_width = 'auto'
	elseif tonumber(width) ~= nil then
		cell_width = width .. '%'
	else
		cell_width = nil
	end
	
	-- Cell
	cell:attr('scope','col')
		:css('background',background or '#CCCCFF')
		:css('width',cell_width)
		:css('color',textColor)
		:wikitext(text)
	
	-- Reference
	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('color','black')
			: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')
	
	if (args.c == nil or args.c == '') then args.c = '#CCCCFF' end
	
	local black_cr = contrast_ratio{args.c or '#CCCCFF', 'black', ['error'] = 0}
	local white_cr = contrast_ratio{'white', args.c or '#CCCCFF', ['error'] = 0}
	
	local partTypes = {
		{'act','액트'},
		{'chapter','챕터'},
		{'part','파트'},
		{'volume','볼륨'},
		{'week','위크'},
		{'액트','액트'},
		{'챕터','챕터'},
		{'파트','파트'},
		{'볼륨','볼륨'},
		{'위크','위크'},
	}
	local partTypes2 = {
		{'부','부'},
		{'장','장'},
		{'권','권'},
		{'주','주'},
	}
	
	local displaytext = ''
	local isAnyPartSet = false
	
	for k,v in pairs(partTypes) do
		if args[v[1]] then
			isAnyPartSet = true
			displaytext = v[2] .. ' ' .. args[v[1]]
		end
	end
	for k,v in pairs(partTypes2) do
		if args[v[1]] then
			isAnyPartSet = true
			displaytext = args[v[1]] ..  v[2]
		end
	end
	if args.subtitle or args['부분제목'] then
		displaytext = displaytext .. ((isAnyPartSet and ': ' or '') .. (args.subtitle or args['부분제목']) )
	end

	local plainText = require('Module:Plain text')._main
	local displayTextAnchor = plainText(displaytext)
	
	row:tag('td')
		:attr('colspan', 13)
		:attr('id', args.id or displayTextAnchor)
		:css('text-align', 'center')
		:css('background-color', args.c or '#CCCCFF')
		:css('color', black_cr > white_cr and 'black' or 'white')
		:wikitext((args.nobold and displaytext or args['굵지않게'] and displaytext or "'''" .. displaytext .. "'''") .. (args.r and "&#8202;" .. EpisodeTable.reference(args.r, args.c) or ''))
	
	return tostring(row)
end

function EpisodeTable.new(frame,args)
	args = args or {}
	local categories = ''
	local background = ((args.background and args.background ~= '' and args.background ~= '#') or (args['배경'] and args['배경'] ~= '' and args['배경'] ~= '#')
						) and (args.background or args['배경']) or nil

	-- Add # to background if necessary
	if background ~= nil and HTMLcolor[background] == nil then
		background = '#'..(mw.ustring.match(background, '^[%s#]*([a-fA-F0-9]*)[%s]*$') or '')
	end
	
	-- Default widths noted by local consensus
	local defaultwidths = {};
	defaultwidths.overall = 5;
	defaultwidths.overall2 = 5;
	defaultwidths.season = 5;
	defaultwidths.series = 5;
	defaultwidths.prodcode = 7;
	defaultwidths.viewers = 10;
	
	-- Create episode table
	local root = mw.html.create('table')
	
	-- Table width
	local table_width = string.gsub(args.total_width or args['전체너비'] or '','%%','')
	if args.total_width == 'auto' or args['전체너비'] == 'auto' or args.total_width == '' or args['전체너비'] == '' then
		table_width = 'auto'
	elseif tonumber(table_width) ~= nil then
		table_width = table_width .. '%'
	else
		table_width = '100%'
	end
	
	root
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:addClass('wikiepisodetable')
		:css('width', table_width)
	
	-- Caption
	if args.show_caption or args['설명표시'] then
		-- Visible caption option, with a tracking category
		root:tag('caption'):wikitext(args.caption or args['설명'])
		categories = categories .. '[[분류:보이는 표 설명을 이용한 에피소드 표]]'
	elseif args.caption or args['설명']then
		-- If a visible caption isn't defined, then default to the screenreader-only caption
		root:tag('caption'):wikitext(frame:expandTemplate{title='스크린 리더 전용',args={args.caption or args['설명']}})
	end
	
	-- Colour contrast; add to category only if it's in the mainspace
	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
	
	if title.namespace == 0 and (args['배경'] and args['배경'] ~= '' and args['배경'] ~= '#') and black_cr < 7 and white_cr < 7 then
		categories = categories .. '[[분류:잘못된 색상 조합을 이용한 에피소드 표]]' 
	end
	
	-- Main row
	local textColor = background and (black_cr > white_cr and 'black' or 'white') or 'black'
	local mainRow = root:tag('tr')
	mainRow
		:css('color', textColor)
		:css('text-align', 'center')
	
	-- Cells
	do
		local used_season = false
		local country = (args.country ~= '' and args.country ~= nil) or (args['국가'] ~= '' and args['국가'] ~= nil)
		local viewers_type = args.viewers_type ~= '' and args.viewers_type ~= nil or (args['시청자종류'] ~= '' and args['시청자종류'] ~= nil)
		local viewers = (country and (args.country or args['국가']) or '') .. ' 시청자 수' ..
			((viewers_type and (args.viewers_type or args['시청자종류']) or '100만') ~= '' and '<br />(' .. (args.viewers_type or args['시청자종류'] or '100만') .. ')' or '')
	
		local cellNames = {
			{'overall', 'EpisodeNumber','전체','번호',((args.season or args['시즌'] or args.series or args['시리즈'] or args.EpisodeNumber2 or args['번호2'] or 
				args.EpisodeNumber2Series or args['번호2시리즈'] or args.forceoverall or args['전체강제'] or args['번호2전체']) and (args.overall_type or args['전체종류'] or '전체') .. '<br />' or '' ) .. '번호'},
			{'overall2','전체2','*','*',''},
			{'season','EpisodeNumber2','시즌','번호2',(args.season_type or args['시즌종류'] or '시즌')..'<br />번호'},
			{'series','EpisodeNumber2Series','시리즈','번호2',(args.series_type or args['시리즈종류'] or '시리즈')..'<br />번호'},
			{'title','Title','제목','*','제목'},
			{'aux1','Aux1','예비1','*',''},
			{'director','DirectedBy','감독','*','감독'},
			{'writer','WrittenBy','작가','*','작가'},
			{'aux2','Aux2','예비2','*',''},
			{'aux3','Aux3','예비3','*',''},
			{'airdate', 'OriginalAirDate','방송날짜','*', '본 ' .. ((args.released or args['공개']) and '공개' or '방송') .. ' 날짜'},
			{'altdate','AltDate','방송날짜2','*',''},
			{'guests','Guests','게스트','*','게스트'},
			{'musicalguests','MusicalGuests','공연게스트','*','공연 게스트'},
			{'prodcode','ProdCode','제작번호','*','제작 번호'},
			{'viewers','Viewers','시청자','*',viewers},
			{'aux4','Aux4','예비4','*',''}
		}
	
		for k,v in pairs(cellNames) do
			local thisCell = args[v[1]] or args[v[2]] or args[v[3]] or args[v[4]]
			if thisCell and (v[1] ~= 'series' or (v[1] == 'series' and used_season == false)) then
				if v[1] == 'season' 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'] or args[v[3] .. 'T'] or args[v[4] .. 'T']
				local thisCellR = args[v[1] .. 'R'] or args[v[2] .. 'R'] or args[v[3] .. 'R'] or args[v[4] .. 'R']
				mainRow:node(EpisodeTable.cell(background, thisCell, thisCellT or v[5], thisCellR, textColor))
			end
		end
	
		-- Episodes
		if args.episodes then
			if args.anchor then 
				args.episodes = string.gsub(args.episodes, "(id=\")(ep%w+\")", "%1" .. args.anchor .. "%2")
			end
			root:node(args.episodes)
		end
		if args['에피소드'] then
			if args['앵커'] then 
				args['에피소드'] = string.gsub(args['에피소드'], "(id=\")(ep%w+\")", "%1" .. args['앵커'] .. "%2")
			end
			root:node(args['에피소드'])
		end
	end

    local templateStyles = mw.getCurrentFrame():extensionTag{
		name = 'templatestyles', args = { src = 'Module:Episode table/styles.css' }
	}
	return (((args.dontclose or args['닫지않기'] or '') ~= '') and mw.ustring.gsub(tostring(root), "</table>", "") or tostring(root)) .. categories .. templateStyles
end

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

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		removeBlanks = false,
		wrappers = '틀:에피소드 표'
	})
	return EpisodeTable.new(frame,args)
end

function p.part(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		removeBlanks = false,
		wrappers = '틀:에피소드 표/부분'
	})
	return EpisodeTable.part(frame,args)
end

function p.ref(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		removeBlanks = false,
	})
	return EpisodeTable.reference(args.r,args.b)
end

return p
Prefix: a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

Portal di Ensiklopedia Dunia

Kembali kehalaman sebelumnya