Module:Sandbox/BrownHairedGirl/GenericDecadeCatNav v01

From English Wikipedia @ Freddythechick
--[[ v01
]]

-- config
local textSize = '90%'
local tableClass="infobox"
local tableFallbackMaxWidth="auto"
local tableMaxWidth="calc(100% - 300px)"
local tableStyle="margin-left:auto; margin-right:auto; clear:left; float:left;"
local RowStyle = ""
local labelStyle = "text-align:right; font-weight: bold;"
local listStyle = "text-align:left; font-weight: normal;"
local greyLinkColor = "#888"

-- globals for this module
local debugging = false
local debugmsg = ""
local tableRowNum = 0
local title_prefix = ""
local title_suffix = ""
local thisPageDecade = nil


local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local p = {}



function makeTableRow()
	debugLog(2, "makeTableRow")
	thisRow = '<tr style="' .. RowStyle .. '">'

	thisRow = thisRow .. '<td style="' .. listStyle .. ';"><div class="hlist">\n'
	for i = -5, 5 do
		thisRow = thisRow .. "* " .. makeCatLink(thisPageDecade + (i * 10)) .. "\n"
    end
	thisRow = thisRow .. '</div></td>\n</tr>\n'
	return thisRow
end


function makeTable()
	debugLog(1, "makeTable")
	tableRowNum = 0
	local myTable = '<table class="' .. tableClass .. '"'
	myTable = myTable .. ' style="' .. tableStyle .. '; font-size:' .. textSize .. '; max-width:' .. tableFallbackMaxWidth .. '; max-width:' .. tableMaxWidth ..'">\n'
	myTable = myTable .. makeTableRow("")
	myTable = myTable .. "</table>\n"
	return myTable
end


-- Make a piped link to a decade category, if it exists
-- If it doesn't exist, just display the greyed the link title without linking
function makeCatLink(x)
	local decadeFulltext = tostring(math.abs(x)) .. 's'
	if x < 0 then
		decadeFulltext = decadeFulltext .. " BC"
	end
	local catname = title_prefix .. decadeFulltext .. title_suffix

	local fmtlink
	local catPage = mw.title.new( catname, "Category" )
	if (catPage.exists) then
		fmtlink = "[[:Category:" .. catname .. "|" .. decadeFulltext .. "]]"
	else
		fmtlink = '<span style="color:' .. greyLinkColor .. '">' .. decadeFulltext .. "</span>"
	end

	return fmtlink
end


function makeCatName(countyName, prefix, suffix)
	local this_cat_name = '';
	this_cat_name = this_cat_name .. prefix
	this_cat_name = this_cat_name .. countyName
	this_cat_name = this_cat_name .. suffix
	return this_cat_name
end


function patternSearchEncode(s)
	return mw.ustring.gsub(s, "([%W])", "%%%1")
end


-- parse the pagename to find 3 parts: prefix, decade, suffix
function parsePagename(pn)
	debugLog(1, "parsePagename: [" .. pn .. "]")
	match_prefix, match_decade, match_suffix = mw.ustring.match(pn, "^(.-)(%d+0s BC)(.*)$")
	if match_decade == nil then
		match_prefix, match_decade, match_suffix = mw.ustring.match(pn, "^(.-)(%d+0s)(.*)$")
	end
	if match_decade == nil then
		debugLog(2, 'Invalid. [' .. pn .. '] no decade"')
		return false
	end
	debugLog(2, 'Split [' .. pn .. ']: /' .. match_prefix .. '/' .. match_decade ..'/' .. match_suffix ..'/')
	if (match_prefix ~= nil) and (match_prefix ~= "") then
		if (mw.ustring.sub(match_prefix, -1 ) ~= ' ') then
			debugLog(2, 'Invalid. [' .. pn .. '] has no space before decade')
			return false
		end
	end

	if (match_suffix ~= nil) and (match_suffix ~= "") then
		if (mw.ustring.sub(match_suffix, 1, 1) ~= ' ') then
			debugLog(2, 'Invalid. [' .. pn .. '] has no space after decade')
			return false
		end
	end
	
	-- if we got here, all is valid
	-- next, check for BC
	local isBC = false
	if (mw.ustring.sub(match_decade, -3) == ' BC') then
		isBC = true
		-- now strip trailing "BC"
		match_decade = mw.ustring.gsub(match_decade, " BC$", "")
	end
	-- now strip trailing "s"
	match_decade =  mw.ustring.gsub(match_decade, "0s$", "0")
	thisPageDecade = tonumber(match_decade)
	if isBC then
		thisPageDecade = -thisPageDecade
	end
	
	
	title_prefix = match_prefix
	title_suffix = match_suffix
	debugLog(2, "parse successful")
	debugLog(3, "title_prefix = [" .. title_prefix .. "]")
	debugLog(3, "thisPageDecade = [" .. tostring(thisPageDecade) .. "]")
	debugLog(3, "title_suffix = [" .. title_suffix .. "]")
	return true
end


function publishDebugLog()
	if not debugging then
		return ""
	end
	return "==Debugging ==\n\n" .. debugmsg .. "\n== Output ==\n"
end


-- debugLog builds a log which can be output if debuging is enabled
-- each log entry is given a level, so that the output is not simply a flat list
-- a debug msg may be appended to the previous msg by setting the level to nil
function debugLog(level, msg)

	if (debugmsg == nil) then
		debugmsg = ""
	end

	if (level ~= nil) then
		-- not appending, so make a new line
		debugmsg = debugmsg .. "\n"
		-- then add the level
		local i
		for i = 1, level do
			if (i % 2) == 1 then
				debugmsg = debugmsg .. "#"
			else
				debugmsg = debugmsg .. "*"
			end
		end 
	end
	debugmsg = debugmsg .. " " .. msg
	return true
end


function getYesNoParam(args, thisParamName, defaultVal)
	local returnValue
	debugLog(2, "Evaluate yes/no parameter: [" .. thisParamName .. "] = [" .. (((args[thisParamName] == nil) and "") or args[thisParamName]) .. "]")
	debugLog(3, "default = " .. ((defaultVal and "Yes") or "No"))
	debugLog(3, "Evaluate as: ")
	returnValue = yesno(args[thisParamName], defaultVal)
	if (returnValue) then
		debugLog(nil, "Yes")
	else
		debugLog(nil, "No")
	end
	return returnValue
end

function p.main(frame)
	debugLog(1, "Check parameters")
	debugging = getYesNoParam(frame.args, "debug", false)

	-- get the page title
	thispage = mw.title.getCurrentTitle()
	thispagename = thispage.text;
	
	debugLog(1, "mw.title.getCurrentTitle()")
	debugLog(2, "thispage.text = [" .. thispage.text .."]")
	debugLog(2, "thispage.namespace = [" .. thispage.namespace .."]")
	debugLog(2, "thispage.nsText = [" .. thispage.nsText .."]")
	debugLog(2, "is it a cat? using (thispage:inNamespace(14)): ")
	if not (thispage:inNamespace(14)) then
		debugLog(nil, "No, this is not a category")
		debugLog(1, "Not a category, so no output")
		return publishDebugLog()
	end
	debugLog(nil, "Yes, this is a category")

	if not parsePagename(thispagename) then
		-- some error parsing the title, so don't proceed to output
		return publishDebugLog()
	end
	
	debugLog(1, "all parse done")
	debugLog(2, "title_prefix = [" .. title_prefix .. "]")
	debugLog(2, "title_suffix = [" .. title_suffix .. "]")

	return publishDebugLog() .. makeTable()

end

return p