Module:Timeline

From Halopedia, the Halo wiki

Documentation for this module may be created at ModuleDoc:Timeline

local p = {}
-- Cache for page existence checks
local existenceCache = {}

-- This checks if a wiki page for a given year exists.
local function isValidYearPage(key)
	local title = mw.title.new(key)
	if not title or not title.exists then
		return false
	end
	return true
end

local function pageExists(year)
	local key = tostring(year)
	if existenceCache[key] ~= nil then
		return existenceCache[key]
	end
	local exists = isValidYearPage(key)
	existenceCache[key] = exists
	return exists
end

function p.main(frame)
	local args = frame:getParent().args
	local title = mw.title.getCurrentTitle().text

	-- Detect year and era (Example: 2552, 480 BCE, so on)
	local yearStr = args.year or title
	local yearNum, era

	-- This checks if the year is BCE or CE
	if yearStr:match("%d+%s*[BCE]+$") then
		yearNum = tonumber(yearStr:match("%d+"))
		era = "BCE"
	else
		yearNum = tonumber(yearStr:match("%d+")) or 0
		era = "CE"
	end

	-- This stops 0 from being used.
	if yearNum == 0 then
		return '<!-- Invalid year -->'
	end

	-- Parse ignore list
	local ignoreSet = {}
	if args.ignore and args.ignore ~= '' then
		for y in mw.text.gsplit(args.ignore, ",", true) do
			y = mw.text.trim(y)
			local num = tonumber(y:match("%d+"))
			if num then ignoreSet[num] = true end
		end
	end

	-- Use this to style the template.
	local html = mw.html.create('div')
		:addClass('infobox')
		:css({
			float = 'right',
			width = '300px',
			margin = '0 0 1em 1em',
			padding = '8px',
			border = '1px solid #aaa',
			background = '#f9f9f9',
			['border-radius'] = '4px'
		})

	-- Timeline Logo Header
	local logoClass = ''
	if args.image and args.image ~= '' then
		logoClass = 'notpageimage'
	end
	html:tag('div')
		:css('text-align', 'center')
		:css('margin-bottom', '10px')
		:wikitext('[[File:HP-Timeline.png|300px|link=Timeline|alt=Timeline|class=' .. logoClass .. ']]')

	-- Year Header
	local displayYear = (era == "BCE") and (yearNum .. " BCE") or tostring(yearNum)
	html:tag('div')
		:addClass('infobox-header')
		:css('text-align', 'center')
		:css('font-weight', 'bold')
		:css('font-size', '200%')
		:wikitext(displayYear)
	-- Year image + caption
	if args.image and args.image ~= '' then
		html:tag('div')
			:css('text-align', 'center')
			:css('margin', '10px 0 6px 0')
			:wikitext('[[' .. args.image .. '|300px]]')

		if args.caption and args.caption ~= '' then
			html:tag('div')
				:css('text-align', 'center')
				:css('font-size', 'larger')
				:css('margin-top', '4px')
				:wikitext(args.caption)
		end
	end

	-- Other calendars
	if args.other and args.other ~= '' then