Jump to content

Module:TFA title: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Make is_todays_tfa usable with {{#if}}
Add is_tfa_on()
Line 14: Line 14:
end
end


-- Is the specified title today's TFA, returns "yes" or ""
-- Is the specified title the TFA today?
-- so it can be used with {{#if:}}
-- Returns "yes" or "", for use with {{#if:}}
function p.is_todays_tfa(title)
function p.is_todays_tfa(title)
return p.is_tfa_on(title, today())
local today = p.today_title()
end
if today == nil then

-- Is the specified title the TFA on the specified date?
-- Returns "yes" or "", for use with {{#if:}}
function p.is_tfa_on(title, date)
local days_title = p.title(date)
if days_title == nil then
-- no clue
-- no clue
return ""
return ""
end
end
if type(today) == "string" then
if type(days_title) == "string" then
if title == today then
if title == days_title then
return "yes"
return "yes"
else
else
Line 31: Line 37:
-- table case, multiple titles
-- table case, multiple titles
for _, check in pairs( today ) do
for _, check in pairs( days_title ) do
if check == title then
if check == title then
return "yes"
return "yes"

Revision as of 00:42, 11 January 2023

local p = {}

local data = mw.loadJsonData("Template:TFA title/data.json")

-- Get the TFA title for the specified "YYYY-MM-DD" date. May be
-- a single title, an array of multiple titles, or nil, if no title is known
function p.title(date)
	return data.titles[date] or nil
end

-- Today's TFA, see title() for possible return values
function p.today_title()
	return p.title(today())
end

-- Is the specified title the TFA today?
-- Returns "yes" or "", for use with {{#if:}}
function p.is_todays_tfa(title)
	return p.is_tfa_on(title, today())
end

-- Is the specified title the TFA on the specified date?
-- Returns "yes" or "", for use with {{#if:}}
function p.is_tfa_on(title, date)
	local days_title = p.title(date)
	if days_title == nil then
		-- no clue
		return ""
	end
	if type(days_title) == "string" then
		if title == days_title then
			return "yes"
		else
			return ""
		end
	end
	
	-- table case, multiple titles
	for _, check in pairs( days_title ) do
		if check == title then
			return "yes"
		end
	end
	return ""
end

-- Internal, today's date as YYYY-MM-DD
function today()
	return os.date("%Y-%m-%d")
end


return p