Documentation for this module may be created at Module:Sandbox/Gabrielchihonglee/TempReign/doc

-- For GCI Lua Task 10
local p = {}

function p.reign( frame )
	
	
	-- **** Taking in all parameters' value **** --
    local startDate = frame.args[1] or "" -- Start date, string and/or number, optional
    local endDate = frame.args[2] or "" -- End date, string and/or number, optional
    local startDate2 = frame.args[3] or "" -- 2nd Start date, string and/or number, optional
    local endDate2 = frame.args[4] or "" -- 2nd End date, string and/or number, optional
    local single = frame.args.single or "" -- Single date, string and/or number, optional
    local show = frame.args.show or frame.args.link or frame.args.lk or "" -- Formats "r.", "none", "link", "word", "colon", "lword", "lcolon", "blank", optional
    local cap = frame.args.cap or "" -- Capitalizes the r./reigned/reign text, "yes" or any other values, optional
    local preDate = frame.args['pre-date'] or "" -- Single date before date range, string and/or number, optional
    local postDate = frame.args['post-date'] or "" -- Single date after date range, string and/or number, optional
    local midDate = frame.args['mid-date'] or "" -- Single date between 2 date range, string and/or number, optional
    local era = frame.args.era or "" -- Adds era at the end, string and/or number (posibly link), optional
    local wrap = frame.args.wrap or "" -- Adds era at the end, "yes" or any other values, optional
	local sortable = frame.args.sortable -- 
	local sortDate = frame.args.sort_date --
	local label = frame.args.label or "" -- Phrase replacing "r.", string and/or number, optional
	
	
	if startDate ~= "" and endDate == "" and startDate2 == "" and endDate2 == "" and preDate == "" and postDate == "" and midDate == "" then
		single = startDate
		startDate = ""
	end
	
	
	-- **** Cleaning dates (remove head and tail space) **** --
	local dateList = {startDate, endDate, startDate2, endDate2, preDate, postDate, midDate}
	for dateListCount = 1, 7 do
		if string.sub(dateList[dateListCount], 1, 1) == ' ' then dateList[dateListCount] = string.sub(dateList[dateListCount], 2, -1) end
		if string.sub(dateList[dateListCount], -1) == ' ' then dateList[dateListCount] = string.sub(dateList[dateListCount], 1, -2) end
	end
	startDate, endDate, startDate2, endDate2, preDate, postDate, midDate = unpack(dateList)
	
	
	-- **** Initial output **** --
	local result = '<abbr title="reign">r</abbr>.&nbsp;'
	if show == 'word' then result = 'reigned ' end
	if show == 'word' and cap ~= '' then result = 'Reigned ' end
	if show == 'colon' then result = 'reign: ' end
	if show == 'colon' and cap ~= '' then result = 'Reign: ' end
	if show == 'lword' then result = '[[Reign|reigned]] ' end
	if show == 'lword' and cap ~= '' then result = '[[Reign|Reigned]] ' end
	if show == 'lcolon' then result = '[[Reign|reign]]: ' end
	if show == 'lcolon' and cap ~= '' then result = '[[Reign|Reign]]: ' end
	if show == 'blank' then result = '' end
	
	
	if label ~= '' then
		result = label .. ' '
	end
	
	-- **** Add space between 'c.' and number if needed **** --
	for dateListCount = 1, 4 do
		if string.sub(dateList[dateListCount], 1, 2) == 'c.' and string.sub(dateList[dateListCount], 1, 3) ~= 'c. ' then
		dateList[dateListCount] = 'c. ' .. string.sub(dateList[dateListCount], 3, -1)
		end
	end
	startDate, endDate, startDate2, endDate2 = unpack(dateList)
	
	
	-- **** Adding space before and after dash **** --
	local isComplexDate1 = false
	local isComplexDate2 = false
	if string.find(startDate, ' ') ~= nil then isStartDateComplex = true end -- Check for space in middle of startDate
	if string.find(endDate, ' ') ~= nil then isEndDateComplex = true end -- Check for space in middle of endDate
	if string.find(startDate2, ' ') ~= nil then isStartDate2Complex = true end -- Check for space in middle of startDate2
	if string.find(endDate2, ' ') ~= nil then isEndDate2Complex = true end -- Check for space in middle of endDate2
	if string.sub(endDate, -4) == ' BCE' then
		if string.find(string.sub(endDate, 1, -5), ' ') == nil then isEndDateComplex = false end
	end
	if isStartDateComplex == true or isEndDateComplex == true then
		isComplexDate1 = true -- true = there is at least 1 complex date
	end
	if isStartDate2Complex == true or isEndDate2Complex == true then
		isComplexDate2 = true -- true = there is at least 1 complex date
	end
	local dash1 = '–'
	local dash2 = '–'
	if startDate2 == '' and endDate2 == '' then	dash2 = '' end
	if isComplexDate1 then dash1 = ' – ' end
	if isComplexDate2 then dash2 = ' – ' end
	
	
	if era ~= '' then era = ' ' .. era end
	
	if preDate ~= '' then preDate = preDate .. ', ' end
	if postDate ~= '' then postDate = ', ' .. postDate end
	if midDate ~= '' then midDate = ', ' .. midDate .. ', ' end
	
	-- **** Producing output **** --
	if startDate == "" and endDate == "" and preDate == "" and postDate == "" and single == "" then
		return result
	elseif single ~= "" then
		result = result .. single
		return result
	else
		if startDate == "" and endDate == "" then dash1 = '' end
		if startDate == "" and endDate == "" and preDate ~= "" and postDate ~= "" then postDate = string.sub(postDate, 3, -1) end
		if startDate == "" and endDate ~= "" then startDate = '?' end
		if endDate == "" and startDate ~= "" then endDate = " " end
		if endDate2 == "" and startDate2 ~= "" then endDate2 = " " end
		local commaBetweenDates = ''
		if startDate ~= "" and endDate ~= "" and startDate2 ~= "" and endDate2 ~= "" and midDate == "" then commaBetweenDates = ', ' end
		if wrap ~= "" then wrap = '<wbr>' end
		result = '<span style="white-space:nowrap;">' .. result .. preDate .. wrap .. startDate .. dash1 .. endDate .. wrap .. midDate .. commaBetweenDates .. wrap .. startDate2 .. dash2 .. endDate2 .. wrap .. postDate .. era .. '</span>'
		return result
	end
end

return p