Module:Sandbox/BrownHairedGirl/xyz

From English Wikipedia @ Freddythechick

local p = {}

function make_cat_link(catname, disp)
	local displaytext
	if (disp ~= "") then
		displaytext = disp
	else
		displaytext = catname
	end
	local link = "[[:Category:" .. catname .. "|" .. displaytext .. "]]"
	local fmtlink
	local linktitle = mw.title.new( catname, "Category" )
	if (linktitle.exists) then
		fmtlink = link
	else
		fmtlink = "<span style=\"color:#888\">" .. displaytext .. "</span>"
	end

	return fmtlink
end

function make_cat_link4(catname, disp)
	local link = "{{LinkCatIfExists2|" .. catname
	if (disp ~= "") then
		link = link .. "|" .. disp
	end
	link = link .. "}}"
	return link
end

function make_decade_cat_link(prefix, start, rest, decadenum)
	local decadestart = (prefix * 100) + (decadenum * 10)
	local decadename = decadestart .. "s"
	return make_cat_link(start .. decadename .. rest, decadename)
end

function ordinal_numbers(n)
  local ordinal, digit = {"st", "nd", "rd"}, string.sub(n, -1)
  if tonumber(digit) > 0 and tonumber(digit) <= 3 and mw.ustring.sub(n,-2) ~= 11 and mw.ustring.sub(n,-2) ~= 12 and mw.ustring.sub(n,-2) ~= 13 then
    return n .. ordinal[tonumber(digit)]
  else
    return n .. "th"
  end
end

function make_century_name(n)
	local centuryordinal = ordinal_numbers(n)
	return centuryordinal ..  " century"
end

function p.main(frame)
	local pagename
	local nilly
	if (frame.args[1] ~= "") then -- a parameter was supplied, so use it as page title
		nilly = "no nil.   value='" .. frame.args[1] .. "'"
		if (string.match(frame.args[1], "^Category *: *")) then
			pagename = mw.ustring.gsub(frame.args[1], "^Category *: *", "");
		else
			pagename = frame.args[1]
		end
	else -- use the page title
		nilly = "nil"
		pagename = mw.title.getCurrentTitle().text
	end
	if (false) then
		return nilly
	end
	local dddo = mw.ustring.match(pagename, "%d%d*0s")
	if (dddo == nil) then
		return "Error: no 'YYOs' in '" .. pagename .. "'"
	end
	local dd = mw.ustring.match(dddo, "^%d*")
	local yy = (dd - (dd % 100))/ 100
	local startoftitle = mw.ustring.gsub(pagename, dddo .. ".*$", "")
	local restoftitle = mw.ustring.gsub(pagename, "^.*" .. dddo, "")
	local diagnotics = "; pagename\n* " .. pagename .. "\n;dddo\n*" .. dddo .. "\n;dd\n*" .. dd
	diagnotics = diagnotics .. "\n;yy\n*" .. yy .. "\n;startoftitle\n*<code>'" .. startoftitle	.. "'</code>"
	diagnotics = diagnotics .. "\n;restoftitle\n*<code>'" .. restoftitle .. "'</code>"
	local retval
	retval = "{| class=\"toccolours\" style=\"border:1px solid black; padding:5px\"\n|-\n|style=\"text-align:center\"| '''" .. startoftitle
	if (startoftitle == "") then
		retval = retval .. "Decades "
	else
		retval = retval .. " decades "
	end
	retval = retval .. " of the " .. make_century_name(yy+1) .. restoftitle .. "'''\n|-\n| "
	retval = retval .. "<small>''" .. make_decade_cat_link(yy, startoftitle, restoftitle, -1) .. "&nbsp;&bull; " .. "''</small>"
	local i;
	for i = 0, 9 do
		retval = retval .. make_decade_cat_link(yy, startoftitle, restoftitle, i)
		if i < 9 then
			retval = retval .. "&nbsp;&bull; "
		end
	end
	retval = retval .. "<small>''" .. "&nbsp;&bull; " .. make_decade_cat_link(yy, startoftitle, restoftitle, 10) .. "''</small>"
	retval = retval .. "\n|}"
	
	return retval
end

return p