SiIvaGunner Wiki
Advertisement
SiIvaGunner Wiki

Documentation for this module may be created at Module:DateParam/doc

-- Implements the date parameter and categorization of Template:Rip
-- Written by overcast07

local p = {}

local lang = mw.language.getContentLanguage()

-- Format an MDY date for display
local function format_param(str)
	assert (str, 'str must exist')
 
	local result = ""
	if (str == "") then
		return str
	end

	local date_format = "F j, Y"

	local result

	-- Test if input can be formatted
	if pcall(function() result = lang:formatDate(date_format, str) end) then
		return result
	else
		-- Remove strip markers (including references)
		local format_str = mw.text.unstrip(str)
		-- Test if format_str can be formatted and if it is at the start of the original string
		if pcall(function() result = lang:formatDate(date_format, format_str) end) and ((str:find(format_str, 1, true)) == 1) then
			local pattern_str = mw.ustring.gsub(format_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
			local replace_str = mw.ustring.gsub(result, "%%", "%%%%" )
			return str:gsub("^"..pattern_str, replace_str)
		end
	end
	return str
end

-- Format a category link and check if the category exists
local function format_category(str)
	assert (str, 'str must exist')
 
	local result = ""
	if (str == "") then
		return str
	end

	local titleObj

	-- Attempt to create title object
	if pcall(function() titleObj = mw.title.new(str, "Category") end) then
		-- Check if category exists and is not a redirect
		if titleObj.exists and not titleObj.isRedirect then
			return "[[Category:" .. str .. "]]"
		end
	end

	-- If the category does not exist or is a redirect, add a tracking category
	return "[[Category:" .. str .. "]][[Category:Pages in non-existent categories|date]]"
end

-- Format a month and year for automatic categorization
local function categorize(str)
	assert (str, 'str must exist')
 
	local result = ""
	if (str == "") then
		return str
	end

	local date_format = 'F Y "rips"'

	local result

	-- Test if input can be formatted
	if pcall(function() result = lang:formatDate(date_format, str) end) then
		return format_category(result)
	else
		-- Remove strip markers (including references)
		local format_str = mw.text.unstrip(str)
		-- Test if format_str can be formatted and if it is at the start of the original string
		if pcall(function() result = lang:formatDate(date_format, format_str) end) and ((str:find(format_str, 1, true)) == 1) then
			return format_category(result)
		end
	end
	return ""
end

-- puts categories on the bottom of the page
function p.categorize(frame)
	local str = frame.args[1]
	local result
	if pcall(function() result = categorize(str) end) then 
		return frame:preprocess(result)
	else
		return frame:preprocess(str .. "[[Category:Pages with script errors]]")
	end
end

-- formats date, if possible
function p.main(frame)
	local str = frame.args[1]
	local result
	if pcall(function() result = format_param(str) end) then 
		return frame:preprocess(result)
	else
		return frame:preprocess(str .. "[[Category:Pages with script errors]]")
	end
end

return p
Advertisement