ਸਮੱਗਰੀ 'ਤੇ ਜਾਓ

ਮੌਡਿਊਲ:Header/year

ਵਿਕੀਸਰੋਤ ਤੋਂ

Documentation for this module may be created at ਮੌਡਿਊਲ:Header/year/doc

local p = {}
--[=[
Parse the provded year and add relevant categories.
--]=]
function p.parse_year_and_categorise(year_args)
	-- The string that will eventually be returned.
	local ret = ""
	local is_bce = nil
	local input_year = year_args[1]

	-- Only active in mainspace and Translation:
	if not mw.title.getCurrentTitle():inNamespaces(0, 114) then
		return
	end

	-- Do nothing if year param is empty or missing.
	if input_year == nil or input_year == '' then
		return
	end

	-- Extract common era info to make it easier to process
	if string.match(input_year, "BCE$") then
		is_bce = true
		input_year:gsub("%s*BCE$", "")
		-- Also tag it as a non-numeric year
		ret = ret .. "[[Category:" .. "Works with non-numeric dates" .. "]]"
	else
		is_bce = false
	end

	-- If the year provided is a plain year (all digits)
	if string.match(input_year, "^%d+$") then
		if year_args["noprint"] ~= 1 then
			ret = ret .. " (" .. year_args[1] .. ") "
		end
		if year_args["nocat"] ~= 1 then
			ret = ret .. "[[Category:" .. year_args[1] .. " works]]"
		end

		-- For simple years we're done.
		return ret
	end

	-- Add tracking category for all non-numeric dates
	if year_args["nocat"] ~= 1 then
		ret = ret .. "[[Category:" .. "Works with non-numeric dates" .. "]]"
	end

	-- Explicitly tagged as being of unknown date
	if string.match(input_year, "^(\?|unknown)$") then
		if year_args["noprint"] ~= 1 then
			ret = ret .. " (unknown) "
		end
		if year_args["nocat"] ~= 1 then
			ret = ret .. "[[Category:" .. "Works of unknown date" .. "]]"
		end

		-- For explicitly given unknown years we're done.
		return ret
	end

	-- 
	-- Now figure out a complex date
	-- 

	-- Check if it looks like a decade
	if string.match(input_year, "0s$") then
		if year_args["noprint"] ~= 1 then
			ret = ret .. " (" .. input_year .. ") "
		end
		if year_args["nocat"] ~= 1 then
			ret = ret .. "[[Category:" .. year_args[1] .. " works]]"
			ret = ret .. "[[Category:" .. "Works of uncertain date" .. "]]"
		end

		-- For explicitly given decades we're done.
		return ret
	end

	-- Or a century
	if string.match(input_year, "(st|nd|rd|th) century$") then
		if year_args["noprint"] ~= 1 then
			ret = ret .. " (" .. year_args[1] .. ") "
		end
		if year_args["nocat"] ~= 1 then
			ret = ret .. "[[Category:" .. year_args[1] .. " works]]"
			ret = ret .. "[[Category:" .. "Works of uncertain date" .. "]]"
		end

		-- For explicitly given centuries we're done.
		return ret
	end

	-- Approximate years
	if string.match(input_year, "^c%.?") or string.match(input_year, "^circa") then
		input_year:gsub("^c%.?", "")
		input_year:gsub("^circa", "")
		input_year:gsub("^( |/)", "")
		if year_args["noprint"] ~= 1 then
			if is_bce then
				ret = ret .. " (c. " .. input_year .. " BCE) "
			else
				ret = ret .. " (c. " .. input_year .. ") "
			end
		end

		if tonumber(input_year) == nil then
			-- Handle bad year
		end
		decade = input_year
		decade:gsub("^(%d*)\d", "%10s")

		if year_args["nocat"] ~= 1 then
			if is_bce then
				ret = ret .. "[[Category:" .. decade .. " BCE works]]"
			else
				ret = ret .. "[[Category:" .. decade .. " works]]"
			end
			ret = ret .. "[[Category:" .. "Works of uncertain date" .. "]]"
		end

		-- For approximate years we're done.
		return ret
	end

	-- If we're here we didn't manage to parse it.
	if year_args["noprint"] ~= 1 then
		ret = ret .. " (" .. year_args[1] .. ") "
	end
	if year_args["nocat"] ~= 1 then
		ret = ret .. "[[Category:" .. "Works with unrecognised dates" .. "]]"
		ret = ret .. "[[Category:" .. "Undated works" .. "]]"
	end

	-- Return whatever we have and the tracking cats
	return ret
end

return p