Memory Alpha
Advertisement

このモジュールについての説明文ページを モジュール:EpisodeData/doc に作成できます

local p = {}
local t = {}

local getArgs = require('Module:Arguments').getArgs
local data = mw.loadData('Module:EpisodeData/data')

function p.A(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'A')
end

function p.C(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'C')
end

function p.CP(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'CP')
end

function p.CR(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'CR')
end

function p.CT(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'CT')
end

function p.D(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'D')
end

function p.M(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'M')
end

function p.N(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'N')
end

function p.P(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'P')
end

function p.T(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'T')
end

function p.Y(frame)
	local args = getArgs(frame)
	return t.getEpisodeProp(args[1], 'Y')
end


t.entityMap = {['lt']='<',['gt']='>',['amp']='&',['quot']='"',['apos']="'"}
function t.unescape(str)
	return str:gsub('(&(#?x?)([%d%a]+);)', function(orig,n,s)
		return n=='' and t.entityMap[s]
			or n=='#' and tonumber(s) and string.char(s)
			or n=='#x' and tonumber(s,16) and string.char(tonumber(s,16))
			or orig
	end)
end

t.months = {
	'January', 'February', 'March', 'April', 'May', 'June',
	'July', 'August', 'September', 'October', 'November', 'December',
}
function t.getEpisodeProp(key, prop)
	if key == nil or prop == nil then return nil end
	local epType, epLang, isRemastered
	key = t.unescape(key)
	key = mw.getContentLanguage():ucfirst(key)
	key = key:gsub('(エピソード)$', ' (エピソード)')
	local name, suffix = key:match('^(.-)%s*%((.-)%)$')
	if suffix == 'エピソード' then
		key = name
		epType = 'episode'
		epLang = 'ja'
	elseif suffix == 'episode' or suffix == 'aftershow' or suffix == 'film' then
		key = name
		epType = suffix
		epLang = 'en'
	elseif key:sub(-2) == '-R' then
		key = key:sub(1, #key-2)
		isRemastered = true
	end
	if data[prop] and data[prop][key] then
		return data[prop][key]
	end
	if data.JA[key] then
		key = data.JA[key]
	end
	local episode = data.ALL[key]
	if episode and isRemastered then
		episode = episode.remastered
	end
	if episode then
		if prop == 'T' and episode.t then
			return episode.t.en
		end
		if prop == 'Y' or prop == 'M' or prop == 'D' then
			local ymd = t.parseDate(episode.ymd)
			if prop == 'Y' then
				return ymd.year
			end
			if prop == 'M' then
				return t.months[ymd.month]
			end
			if prop == 'D' then
				return ymd.day
			end
		end
		return episode[prop:lower()]
	end
end

function t.parseDate(ymd)
	if ymd == nil then return nil end
	local year, month, day = ymd:match('^(%d%d%d%d)%-(%d%d)%-(%d%d)$')
	return {
		year = tonumber(year),
		month = tonumber(month),
		day = tonumber(day),
	}
end

return p
Advertisement