Jump to content

Module:Person date

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Zackmann08 (talk | contribs) at 05:03, 18 September 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function is_valid_month (str)
	local months = {'Jan','January','Feb','February','Mar','March','Apr','April','May','Jun','June','Jul','July','Aug','August','Sep','September','Oct','October','Nov','November','Dec','December'}
    for index, value in ipairs(months) do
        if value == str then
            return true
        end
    end
    return false
end
function is_valid_date(str)
	local month = mw.ustring.match (str, '^%d+%s*(%a+)%s*%d%d%d%d') or mw.ustring.match(str, '^(%a+)%s+%d+,%s*%d%d%d%d') or mw.ustring.match(str, '^(%a+)%s+%d%d%d%d') 
	
	return is_valid_month(month) 
end

function is_year_only(str)
	return mw.ustring.match(str, '^%s*%d%d%d%d%s*')
end

function already_has_age(str)
	return mw.ustring.match(str, '%(aged%s+%d+%)')
end

function parse_birth(frame, args)
	local birth_date = args[1] or ''
	local death_date = args[2] or ''
	local original = birth_date
	
	-- Sanatize leading whitespace (this caused an issue before it was implemented)
	birth_date = mw.ustring.gsub(birth_date,'^%s*','')
	death_date = mw.ustring.gsub(death_date,'^%s*','')
	
	if already_has_age(birth_date) then
		return original
	end
	
	if is_valid_date(birth_date) then
		local location = mw.ustring.find(birth_date, '%d%d%d%d')
		local date = mw.ustring.sub(birth_date, 1,location+3)
		local extra = mw.ustring.sub(birth_date, location+4)
		if death_date == '' then
			return frame:expandTemplate{ title = 'Birth date and age text', args = {date}} .. extra
		else
			return frame:expandTemplate{ title = 'Birth date text', args = {date}} .. extra
		end
	end
	if is_year_only(birth_date) then
		if death_date == '' then
			return frame:expandTemplate{ title = 'bya', args = {mw.ustring.sub(birth_date, 1, 5)}} .. mw.ustring.sub(birth_date, 5)
		else
			return frame:expandTemplate{ title = 'birth year', args = {mw.ustring.sub(birth_date, 1, 5)}} .. mw.ustring.sub(birth_date, 5)
		end
	end
	
	return original
end

function parse_death(frame, args)
	local birth_date = args[1] or ''
	local death_date = args[2] or ''
	local original = death_date
	
	-- Sanatize leading whitespace (this caused an issue before it was implemented)
	birth_date = mw.ustring.gsub(birth_date,'^%s*','')
	death_date = mw.ustring.gsub(death_date,'^%s*','')
	
	if already_has_age(death_date) then
		return original
	end
	
	if is_valid_date(death_date) then
		local location = mw.ustring.find(death_date, '%d%d%d%d')
		local date = mw.ustring.sub(death_date, 1,location+3)
		local extra = mw.ustring.sub(death_date, location+4)
			
		if birth_date == '' then
			return frame:expandTemplate{ title = 'death date text', args = {date}} .. extra
		else
			if is_year_only(birth_date) then
				location =  mw.ustring.find(birth_date, '%d%d%d%d')
				bd = mw.ustring.sub(birth_date, 1,location+3)
			
				return frame:expandTemplate{ title = 'Death date and age text', args = {date, bd}} .. extra
			else
				if is_valid_date(birth_date) then
					location =  mw.ustring.find(birth_date, '%d%d%d%d')
					bd = mw.ustring.sub(birth_date, 1,location+3)
					
					return frame:expandTemplate{ title = 'Death date and age text', args = {date, bd}} .. extra	
				end	
			end
		end
	end
	if is_year_only(death_date) then
		if birth_date == '' then
			return frame:expandTemplate{ title = 'death year', args = {mw.ustring.sub(death_date, 1, 5)}} .. mw.ustring.sub(death_date, 5)
		else
			if is_year_only(birth_date) then
				return frame:expandTemplate{ title = 'Death year and age', args = {mw.ustring.sub(death_date, 1, 5), mw.ustring.sub(birth_date, 1, 5)}} .. mw.ustring.sub(death_date, 5)	
			else
				if is_valid_date(birth_date) then
					local location = mw.ustring.find(death_date, '%d%d%d%d')
					local date = mw.ustring.sub(death_date, 1,location+3)
					local extra = mw.ustring.sub(death_date, location+4)
					
					location =  mw.ustring.find(birth_date, '%d%d%d%d')
					bd = mw.ustring.sub(birth_date, 1,location+3)
					
					return frame:expandTemplate{ title = 'Death date and age text', args = {date, bd}} .. extra	
				end	
			end
		end
	end
	
	return original
end

function p.birth(frame)
	return parse_birth(frame, frame.args[1] and frame.args or frame:getParent().args)
end

function p.death(frame)
	return parse_death(frame, frame.args[1] and frame.args or frame:getParent().args)
end

return p