Module:Sandbox/BrownHairedGirl/IrelandByCountyCatNav3

	-- each title consists of 3 parts
	--    * prefix
	--    * county name
	--    * suffix
	-- e.g. "Foo in County Mayo"
	--    * prefix = "Foo in "
	--    * county name = "County Mayo"
	--    * suffix = ""
	-- e.g. "County Sligo-related lists"
	--    * prefix = ""
	--    * county name = "County Sligo"
	--    * suffix = "-related lists"

local ROI_counties = {
	'Carlow',
	'Cavan',
	'Clare',
	'Cork',
	'Donegal',
	'Dublin',
	'Galway',
	'Kerry',
	'Kildare',
	'Kilkenny',
	'Laois',
	'Leitrim',
	'Limerick',
	'Longford',
	'Louth',
	'Mayo',
	'Meath',
	'Monaghan',
	'Offaly',
	'Roscommon',
	'Sligo',
	'Tipperary',
	'Waterford',
	'Westmeath',
	'Wexford',
	'Wicklow'
}

local Norniron_counties = {
	'Antrim',
	'Armagh',
	'Down',
	'Fermanagh',
	'Londonderry',
	'Tyrone'
}

local New_counties = {
	'Dún Laoghaire–Rathdown',
	'Fingal',
	'South Dublin'
}



function nil_or_value(s)
	if (s == nil) then
		return "nil"
	end
	return s
end

function makeTable()
	local i, myCounty
	local myTable = '<table class="infobox" style="margin-left:auto; margin-right:auto; font-size: 90%; clear:left; float:left; width:auto;">\n'
	myTable = myTable .. '<tr>\n'
--	myTable = myTable .. '<td colspan="2" style="text-align:center; background-color:#f3f3f3"></td>\n'
	myTable = myTable .. '</tr>\n'
	myTable = myTable .. '<td style="text-align:right; font-weight: bold;">' .. 'Republic of Ireland</td>\n'
	myTable = myTable .. '<td style="text-align:left;"><div class="hlist">\n'
	for i, myCounty in ipairs(ROI_counties) do
		myCatName = make_cat_name(myCounty, title_prefix, title_suffix, title_nocountyword)
		myTable = myTable .. "* " .. make_cat_link(myCatName, myCounty) .. "\n"
		local j, nuCounty
		if (myCounty == "Dublin") then
			for j, nuCounty in ipairs(New_counties) do
				myCatName = make_cat_name(nuCounty, title_prefix, title_suffix, true)
				myTable = myTable .. "** " .. make_cat_link(myCatName, nuCounty) .. "\n"
			end
			myCatName = make_cat_name("Dublin (city)", title_prefix, title_suffix, title_nocountyword)
			myTable = myTable .. "** " .. make_cat_link(myCatName, "city") .. "\n"
		end
	end

	myTable = myTable .. "</div></td></tr>"
	myTable = myTable .. '<tr style="margin:1.5em; background-color:#f3f3f3">\n'
	myTable = myTable .. '<td style="text-align:right;  font-weight: bold;">' .. 'Northern&nbsp;Ireland</td>\n'
	myTable = myTable .. '<td style="text-align:left;"> <div class="hlist">\n'
	for i, myCounty in ipairs(Norniron_counties) do
		myCatName = make_cat_name(myCounty, title_prefix, title_suffix, title_nocountyword)
		myTable = myTable .. "* " .. make_cat_link(myCatName, myCounty) .. "\n"
	end
	myTable = myTable .. "</div></td></tr></table>\n"
	return myTable
end

local debugmsg = nil
local getArgs = require('Module:Arguments').getArgs
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_name(countyname, prefix, suffix, nocounty)
	local this_cat_name = '';
	this_cat_name = this_cat_name .. prefix
	if not (nocounty) then
		this_cat_name = this_cat_name .. 'County ';
	end
	this_cat_name = this_cat_name .. countyname
	this_cat_name = this_cat_name .. suffix
	return this_cat_name
end


function find_new_county_name_in_pagename(pn)
	local i, testCounty
	for i, testCounty in ipairs(ROI_counties) do
		debuglog(4, "testing new_county: ["  .. testCounty .. "]")
		if (mw.ustring.match(pn, testCounty)) then
			if mw.ustring.match(pn, "^" .. testCounty .. "[^%w]") then
				return testCounty
			elseif mw.ustring.match(pn, "[^%w]" .. testCounty .. "$") then
				return testCounty
			elseif mw.ustring.match(pn, "[^%w]" .. testCounty .. "[^%w]") then
				return testCounty
			elseif mw.ustring.match(pn, "[^%w]" .. testCounty .. "[^%w]") then
				return testCounty
			end
		end
	end
	return nil
end


function parse_pagename(pn)
	debuglog(1, "parse_pagename: [" .. pn .. "]")
	debuglog(2, "simple parse")
	match_prefix, match_county, match_suffix = string.match(pn, "^(.*)(County%s+%a+)(.*)$")
	if (match_county == nil or match_county == '') then
		debuglog(3, "No match_'County Foo'")
		debuglog(2, "try new counties")
		local new_county = find_new_county_name_in_pagename(pn)
		if new_county == nil then
			debuglog(3, "no match in new counties")
			return false
		end
		debuglog(3, "found new county: [" .. new_county .. "]")
		match_prefix, match_county, match_suffix = string.match(pn, "^(.*)(" .. new_county .. ")(.*)$")

	end
	title_prefix = match_prefix
	title_suffix = match_suffix
	debuglog(2, "parse successful")
	debuglog(3, "match_prefix = [" .. match_prefix .. "]")
	debuglog(3, "match_county = [" .. match_county .. "]")
	debuglog(3, "match_suffix = [" .. match_suffix .. "]")
	return true
end

function debuglog(level, msg)

	if (debugmsg == nil) then -- we are not debugging
		return false
	end

	if (string.match(debugmsg, "^%s+$")) then
		debugmsg = "==Debugging ==\n\n"
	end

	debugmsg = debugmsg .. "\n"
	if (level == 1) then
		debugmsg = debugmsg .. "# "
	elseif  (level == 2) then
		debugmsg = debugmsg .. "#* "
	elseif  (level == 3) then
		debugmsg = debugmsg .. "#*# "
	elseif  (level == 4) then
		debugmsg = debugmsg .. "#*#* "
	end
	debugmsg = debugmsg .. " " .. msg
	return true
end


function argValueFunc(value)
	if (value == nil) then
		value = '' -- nil value = blank
	end
	value = mw.ustring.gsub(value, "^%s+$", "") -- only whitespace, so replace with ''
	return value
end

		
function p.main(frame)
-- getArgs
-- In all cases, convert to blank (i.e. '')
--   * a nil value
--   * a value consisting only of whitespace
-- for the third parameter ("nospace"), trim whitespace and convert to lowercase

local myArgs = {}
	myArgs[1] = argValueFunc(frame.args[1])
	myArgs[2] = argValueFunc(frame.args[2])
	myArgs[3] = argValueFunc(frame.args[3])
	myArgs[3] = mw.text.trim(myArgs[3]:lower())


	debuglog(1, "myArgs")
	debuglog(2, "myArgs[1] = [" .. myArgs[1] .. "]")
	debuglog(2, "myArgs[2] = [" .. myArgs[2] .. "]")
	debuglog(2, "myArgs[3] = [" .. myArgs[3] .. "]")

	-- now set the key variables
	title_prefix = myArgs[1]
	title_suffix = myArgs[2]
	title_nocountyword = false
	if (myArgs[3] == 'nocountyword') then
		title_nocountyword = true
	end
	debuglog(1, "set main variables")
	debuglog(2, "title_prefix = [" .. title_prefix .. "]")
	debuglog(2, "title_suffix = [" .. title_suffix .. "]")
	
	-- 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 (thispage:inNamespace(14)) then
		debuglog(3, "yes, this is a category")
	else
		debuglog(3, "no, this is not a category")
	end

	-- do we need to parse the page title?
	-- if neither title_prefix nor title_suffix was supplied as a parameter, then yes
	-- test by concatenating them, and see if the combination is blank
	if ((title_prefix .. title_suffix) == '') then
		debuglog(1, "Yes, we need to parse the page title")
		parse_pagename(thispagename)
	else
		debuglog(1, "No, we don't need to parse the page title")
	end
	debuglog(1, "all parse done")
	debuglog(2, "title_prefix = [" .. title_prefix .. "]")
	debuglog(2, "title_suffix = [" .. title_suffix .. "]")

	if (debugmsg == nil) then
		debugmsg = ""
	else
		debugmsg = debugmsg .. "\n== Output ==\n"
	end
	return debugmsg .. makeTable()


end

return p