Jump to content

Module:Sandbox/BrownHairedGirl/IrelandByCountyCatNav19

From Wikipedia, the free encyclopedia

This is the current revision of this page, as edited by BrownHairedGirl (talk | contribs) at 05:43, 5 April 2018 ("By county"). The present address (URL) is a permanent link to this version.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
--[[ v19

 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"
]]

-- config
local textSize = '90%'
local tableClass="infobox"
local tableStyle="margin-left:auto; margin-right:auto; clear:left; float:left; width:auto;"
local evenRowStyle = "background-color:#f3f3f3;"
local oddRowStyle = ""
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 includeNewCounties = true
local useCountyWord = true
local title_prefix = ""
local title_suffix = ""
local displayAbbrevCounty = false
local countySet = nil
local title_prefix
local title_suffix
local thisPageCounty

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

local TwentySixCounties = {
	'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 SixCounties = {
	'Antrim',
	'Armagh',
	'Down',
	'Fermanagh',
	'Londonderry',
	'Tyrone'
}

local newCounties = {
	'Dún Laoghaire–Rathdown',
	'Fingal',
	'South Dublin (county)'
}

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

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


local abbreviatedCountyName = {
--using ISO 3166-2:IE
	['Cork'] = 'CO',
	['Clare'] = 'CE',
	['Cavan'] = 'CN',
	['Carlow'] = 'CW',
	['Dublin'] = 'D',
	['Donegal'] = 'DL',
	['Galway'] = 'G',
	['Kildare'] = 'KE',
	['Kilkenny'] = 'KK',
	['Kerry'] = 'KY',
	['Limerick'] = 'LK',
	['Longford'] = 'LD',
	['Louth'] = 'LH',
	['Leitrim'] = 'LM',
	['Laois'] = 'LS',
	['Meath'] = 'MH',
	['Monaghan'] = 'MN',
	['Mayo'] = 'MO',
	['Offaly'] = 'OY',
	['Roscommon'] = 'RN',
	['Sligo'] = 'SO',
	['Tipperary'] = 'TA',
	['Waterford'] = 'WD',
	['Westmeath'] = 'WH',
	['Wexford'] = 'WX',
	['Wicklow'] = 'WW',
-- made-up codes for new counties
	['Fingal'] = 'FL',
	['South Dublin'] = 'DS',
	['South Dublin (county)'] = 'DS',
	['Dún Laoghaire–Rathdown'] = 'DLR',
-- made-up codes for Northern Ireland
	['Antrim'] = 'AM',
	['Armagh'] = 'AH',
	['Down'] = 'DN',
	['Fermanagh'] = 'FH',
	['Londonderry'] = 'LY',
	['Tyrone'] = 'TE'
}


function makeTableRow(rowLabel, useCountyWord, countyList)
	debugLog(2, "makeTableRow, label: ")
	if (rowLabel == nil) then
		rowLabel = "By county"
		debugLog(nil, rowLabel)
	else
		rowLabel = mw.text.trim(rowLabel)
		debugLog(nil, " [" .. rowLabel .. "]")
	end
	tableRowNum = tableRowNum + 1
	local thisRow
	if (tableRowNum % 2) == 0 then
		debugLog(3, "Even-numbered")
		thisRow = '<tr style="' .. evenRowStyle .. '">\n'
	else
		debugLog(3, "Odd-numbered")
		thisRow = '<tr style="' .. oddRowStyle .. '">\n'
	end
	if not ((rowLabel == nil) or (rowLabel =="")) then
		thisRow = thisRow .. '<td style="' .. labelStyle .. '">' .. rowLabel .. '</td>\n'
	end
	-- now begin making the row contents
	local countyWord = ""
	if useCountyWord then
		debugLog(3, "Using countyWord")
		countyWord = "County "
	else
		debugLog(3, "Not using countyWord")
	end
	thisRow = thisRow .. '<td style="' .. listStyle .. ';"><div class="hlist">\n'
	local i, aCounty
		debugLog(3, "Process countyList")
	for i, aCounty in ipairs(countyList) do
		debugLog(4, "No. [" .. tostring(i) .. ": [" .. aCounty .. "]")
		myCatName = makeCatName(countyWord .. aCounty, title_prefix, title_suffix)
		thisRow = thisRow .. "* " .. makeCatLink(myCatName, aCounty) .. "\n"
		local j, nuCounty
		if (includeNewCounties and (aCounty == "Dublin")) then
			-- make a sub-list for the newCounties
			local subCatName
			for j, nuCounty in ipairs(newCounties) do
				subCatName = makeCatName(nuCounty, title_prefix, title_suffix)
				thisRow = thisRow .. "** " .. makeCatLink(subCatName, nuCounty) .. "\n"
			end
			subCatName = makeCatName("Dublin (city)", title_prefix, title_suffix)
			thisRow = thisRow .. "** " .. makeCatLink(subCatName, "City") .. "\n"
		end
	end
	thisRow = thisRow .. '</div></td>\n</tr>'
	return thisRow
end


function makeTable()
	debugLog(1, "makeTable")
	tableRowNum = 0
	local myTable = '<table class="' .. tableClass .. '" style="' .. tableStyle .. ' ; font-size:' .. textSize .. ';">\n'
	if (countySet == "thirtytwo") then
		myTable = myTable .. makeTableRow(nil, useCountyWord, Traditional32Counties)
	elseif (countySet == "twentysix") then
		myTable = myTable .. makeTableRow(nil, useCountyWord, TwentySixCounties)
	elseif (countySet == "six") then
		myTable = myTable .. makeTableRow(nil, useCountyWord, SixCounties)
	elseif (countySet == "gaa") then
		myTable = myTable .. makeTableRow(nil, useCountyWord, GAACounties)
	else -- default to 26 plus 6
		if isCountyInSet(thisPageCounty, SixCounties, "SixCounties") then
			myTable = myTable .. makeTableRow("Northern Ireland", useCountyWord, SixCounties)
			myTable = myTable .. makeTableRow("Republic of Ireland", useCountyWord, TwentySixCounties)
		else
			myTable = myTable .. makeTableRow("Republic of Ireland", useCountyWord, TwentySixCounties)
			myTable = myTable .. makeTableRow("Northern Ireland", useCountyWord, SixCounties)
		end
	end
	myTable = myTable .. "</table>\n"
	return myTable
end


function makeAbbreviatedCountyName(s)
	debugLog(5, "displayAbbrevCounty? ")

	if not displayAbbrevCounty then
		debugLog(nil, " No")
		return s
	end
		debugLog(nil, " Yes")
	if (abbreviatedCountyName[s] ~= nil) then
		debugLog(5, "AbbreviatedCountyName for: [" .. s .. "] = [" .. abbreviatedCountyName[s] .. "]")
		return abbreviatedCountyName[s]
	end
	debugLog(5, "No abbreviatedCountyName for: [" .. s .. "]")
	return s
end

-- Make a piped link to a category, if it exists
-- If it doesn't exist, just display the greyed the link title without linking
function makeCatLink(catname, disp)
	local displaytext
	if (disp ~= "") and (disp ~= nil) then
		-- use 'disp' parameter, but strip any trailing disambiguator
		displaytext = mw.ustring.gsub(disp, "%s+%(.+$", "");
		displaytext = makeAbbreviatedCountyName(displaytext)
	else
		displaytext = catname
	end
	local fmtlink
	local catPage = mw.title.new( catname, "Category" )
	if (catPage.exists) then
		fmtlink = "[[:Category:" .. catname .. "|" .. displaytext .. "]]"
	else
		fmtlink = '<span style="color:' .. greyLinkColor .. '">' .. displaytext .. "</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


-- Does the pagename include a bare county name? (i.e. without the prefix "County ")
-- This would be one line in regex, but Lua pattern matching is cruder, so
-- we need several passes to ensure that any match is of a complete word
function findBareCountyNameInPagename(pn, countylist, description)
	local i, testCounty
	debugLog(2, "trying bare_county name [" .. pn .."] in county set: " .. description)
	for i, testCounty in ipairs(countylist) do
		debugLog(3, "testing new_county: ["  .. testCounty .. "]")
		local testCountyEncoded = mw.ustring.gsub(testCounty, "([%W])", "%%%1")
		-- For efficiency, the first test is a simple match as a a screening test
		-- If the bare county name is nowhere in the pagename, then no need for
		-- more precise checks
		if (mw.ustring.match(pn, testCountyEncoded)) then
			debugLog(4, "simple match success")
			debugLog(4, "match at start, followed by separator? ")
			if mw.ustring.match(pn, "^" .. testCountyEncoded .. "[^%w]") then
				debugLog(nil, "Yes")
				return testCounty
			else
				debugLog(nil, "No")
			end
			debugLog(4, "match at end, preceded by separator? ")
			if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "$") then
				debugLog(nil, "Yes")
				return testCounty
			else
				debugLog(nil, "No")
			end
			debugLog(4, "match anywhere, preceded and followed by separator? ")
			if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "[^%w]") then
				debugLog(nil, "Yes")
				return testCounty
			else
				debugLog(nil, "No")
			end
		else
			debugLog(4, "simple match fail")
		end
	end
	return nil
end

-- check whether a given county name is in a particular set
function isCountyInSet(s, aSet, description)
	local thisCounty = mw.ustring.gsub(s, "^County +", "")
	debugLog(4, "Checking [" .. thisCounty .. "] in set: " .. description)
	local aValidCounty
	for i, aValidCounty in ipairs(aSet) do
		debugLog(4, "Compare with [" .. aValidCounty .. "]: ")
		if mw.ustring.match(thisCounty, "^" .. aValidCounty .. "$") then
			debugLog(nil, " match")
			return true
		end
		debugLog(nil, " not matched")
	end
	return false
end

function isCountyValidCheck(s)
	local thisCounty = mw.ustring.gsub(s, "^County +", "")
	local aValidCounty
	if (countySet == "twentysix") then
		return isCountyInSet(thisCounty, TwentySixCounties, "twentysix")
	elseif (countySet == "thirtytwo")  then
		return isCountyInSet(thisCounty, Traditional32Counties, "thirtytwo")
	elseif (countySet == "six") then
		return isCountyInSet(thisCounty, SixCounties, "six")
	elseif (countySet == "gaa") then
		return isCountyInSet(thisCounty, GAACounties, "gaa")
	else -- default: trear as (countySet == "twentysixplussix")
		if (isCountyInSet(thisCounty, TwentySixCounties, "twentysix")) then
			return true
		end
		return isCountyInSet(thisCounty, SixCounties, "six")
	end
	--shouldn't get here, but just in case
	return false
end

function parsePagename(pn)
	debugLog(1, "parsePagename: [" .. pn .. "]")
	debugLog(2, "simple parse")
	match_prefix, match_county, match_suffix = mw.ustring.match(pn, "^(.*)(County%s+%a+)(.*)$")
	if not (match_county == nil or match_county == '') then
		debugLog(3, "County Foo = [" .. match_county .. "]")
		-- we have a "County Foo" ... but is it a valid county in this set?
		debugLog(3, "is [" .. match_county .. "] a valid county in this set? ")
		if isCountyValidCheck(match_county) then
			debugLog(3, "We have a valid county")
		else
			debugLog(3, "Not a valid county")
			return false
		end
	else -- simple parse failed
		debugLog(3, "No match 'County Foo'")
		--do we need to check newCounties
		if includeNewCounties then
			debugLog(3, "check newCounties")
			local new_county = findBareCountyNameInPagename(pn, newCounties, "New_counties")
			if new_county == nil then
				debugLog(3, "no match in new counties")
			return false
			end
			debugLog(3, "found new county: [" .. new_county .. "]")
		end
		debugLog(3, "parse pagename for [" .. new_county .. "]")
		local testCountyEncoded = mw.ustring.gsub(new_county, "([%W])", "%%%1")
		match_prefix, match_county, match_suffix = mw.ustring.match(pn, "^(.*)(" .. testCountyEncoded .. ")(.*)$")
	end
	title_prefix = match_prefix
	title_suffix = match_suffix
	thisPageCounty = mw.ustring.gsub(match_county, "^County%s+", "")
	debugLog(2, "parse successful")
	debugLog(3, "title_prefix = [" .. title_prefix .. "]")
	debugLog(3, "thisPageCounty = [" .. thisPageCounty .. "]")
	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 p.main(frame)
-- getArgs
-- In all cases, convert to blank (i.e. '')
--   * a nil value
--   * a value consisting only of whitespace

	debugLog(1, "Check parameters")
	
	debugLog(2, "debug")
	if (frame.args['debug'] == nil) or (frame.args['debug'] == "") then
		debugLog(3, "not set")
	else
		debugLog(3, "debug=[" .. frame.args['debug'] .. "]")
		debugLog(4, "Evaluate as: ")
		if (yesno(frame.args['debug'], false)) then
			debugLog(nil, "Yes")
			debugging = true
		else
			debugLog(nil, "No, or invalid")
			debugging = false
		end
	end

	debugLog(2, "usecountyword")
	if ((frame.args['usecountyword'] == nil) or (frame.args['usecountyword'] == "")) then
		debugLog(3, "not set")
	else
		debugLog(3, "debug=[" .. frame.args['usecountyword'] .. "]")
		debugLog(4, "Evaluate as: ")
		if (yesno(frame.args['usecountyword'], true)) then
			debugLog(nil, "Yes, or invalid")
			useCountyWord = true
		else
			debugLog(nil, "No")
			useCountyWord = false
		end
	end

	debugLog(2, "abbrev")
	if ((frame.args['abbrev'] == nil) or (frame.args['abbrev'] == "")) then
		debugLog(3, "not set")
	else
		debugLog(3, "abbrev=[" .. frame.args['abbrev'] .. "]")
		debugLog(4, "Evaluate as: ")
		if (yesno(frame.args['abbrev'], false)) then
			debugLog(nil, "Yes")
			displayAbbrevCounty = true
		else
			debugLog(nil, "No, or invalid")
			displayAbbrevCounty = false
		end
	end

	debugLog(2, "newcounties")
	if ((frame.args['newcounties'] == nil) or (frame.args['newcounties'] == "")) then
		debugLog(3, "not set")
	else
		debugLog(3, "debug=[" .. frame.args['newcounties'] .. "]")
		debugLog(4, "Evaluate as: ")
		if (yesno(frame.args['newcounties'], false)) then
			debugLog(nil, "Yes, or invalid")
			includeNewCounties = true
		else
			debugLog(nil, "No")
			includeNewCounties = false
		end
	end

	countySetParam = "twentysixplussix" -- default
	debugLog(2, "countySet")
	if ((frame.args['countyset'] == nil) or (frame.args['countyset'] == "")) then
		debugLog(3, "not set")
	else
		debugLog(3, "countyset=[" .. frame.args['countyset'] .. "]")
		local countySetParam = mw.text.trim(mw.ustring.lower(frame.args['countyset']))
		debugLog(4, "Evaluate as: ")
		if (countySetParam == "twentysix") or
			(countySetParam == "six") or
			(countySetParam == "thirtytwo") or
			(countySetParam == "gaa") or
			(countySetParam == "twentysixplussix")
		then
			countySet = countySetParam
			debugLog(nil, "[" .. countySetParam .. "]")
		else
			debugLog(nil, "not a valid set")
		end
	end

	-- 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