Module:Sandbox/BrownHairedGirl/IrelandByCountyCatNav20

--[[ v20

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; padding-right: 1em" 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 isNorniron = false

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', -- made-up code for GAA county: Derry ['Derry'] = 'DY' }

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 = '\n' else debugLog(3, "Odd-numbered") thisRow = '\n' end if not ((rowLabel == nil) or (rowLabel =="")) then thisRow = thisRow .. '' .. rowLabel .. ' \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 .. ' \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 .. ' \n ' return thisRow end

function makeTable debugLog(1, "makeTable") tableRowNum = 0 local myTable = ' \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 = "" .. displaytext .. ""	else		fmtlink = ' ' .. displaytext .. " "	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

-- Does the pagename include a county name? -- with or without the prefix "County", depending on value of useCountyWord function findCountyNameInPagename(pn, countylist, description, prefixCountyWord) local i, aCounty, testCounty debugLog(2, "checking [" .. pn .."] for a county name in county set: " .. description) for i, aCounty in ipairs(countylist) do		if prefixCountyWord then testCounty = "County " .. aCounty else testCounty = aCounty end debugLog(3, "testing: [" .. testCounty .. "]") local testCountyEncoded = patternSearchEncode(testCounty) -- 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 -- This check 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 debugLog(4, "simple match? ") if (not mw.ustring.match(pn, testCountyEncoded)) then debugLog(nil, "Fail") else debugLog(nil, "Success")

debugLog(4, "match at start, followed by separator? ") if mw.ustring.match(pn, "^" .. testCountyEncoded .. "[^%w]") then debugLog(nil, "Yes") return testCounty end debugLog(nil, "No") debugLog(4, "match at end, preceded by separator? ") if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "$") then debugLog(nil, "Yes") return testCounty end debugLog(nil, "No")

debugLog(4, "match anywhere, preceded and followed by separator? ") if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "[^%w]") then debugLog(nil, "Yes") return testCounty end debugLog(nil, "No") 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

-- parse the pagename to find 3 parts: prefix, county name, suffix function parsePagename(pn) debugLog(1, "parsePagename: [" .. pn .. "]") isNorniron = false local validCountyName if (countySet == "twentysix") then validCountyName = findCountyNameInPagename(pn, TwentySixCounties, "twentysix", useCountyWord) if (validCountyName == nil and includeNewCounties) then validCountyName = findCountyNameInPagename(pn, newCounties, "new counties", false) end elseif (countySet == "thirtytwo") then validCountyName = findCountyNameInPagename(pn, Traditional32Counties, "thirtytwo", useCountyWord) if (validCountyName == nil and includeNewCounties) then validCountyName = findCountyNameInPagename(pn, newCounties, "new counties", false) end elseif (countySet == "six") then validCountyName = findCountyNameInPagename(pn, SixCounties, "six", useCountyWord, false) if validCountyName ~= nil then isNorniron = true end elseif (countySet == "gaa") then validCountyName = findCountyNameInPagename(pn, GAACounties, "gaa", useCountyWord, false) else -- default: treat as (countySet == "twentysixplussix") validCountyName = findCountyNameInPagename(pn, TwentySixCounties, "twentysix", useCountyWord) if validCountyName == nil then validCountyName = findCountyNameInPagename(pn, SixCounties, "six", useCountyWord) if validCountyName ~= nil then isNorniron = true end end if (validCountyName == nil and includeNewCounties) then validCountyName = findCountyNameInPagename(pn, newCounties, "new counties", false) end

end if validCountyName == nil then return false end -- if we get here, the page name "pn" includes a validCountyName -- so now we need to split the string debugLog(2, "split pagename around [" .. validCountyName .. "]") local validCountyEncoded = mw.ustring.gsub(validCountyName, "([%W])", "%%%1") match_prefix, match_county, match_suffix = mw.ustring.match(pn, "^(.*)(" .. validCountyEncoded .. ")(.*)$")	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 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) useCountyWord = getYesNoParam(frame.args, "usecountyword", true) displayAbbrevCounty = getYesNoParam(frame.args, "abbrev", false) includeNewCounties = getYesNoParam(frame.args, "newcounties", true)

countySetParam = "twentysixplussix" -- default debugLog(2, "countySet") if ((frame.args['countyset'] == nil) or (frame.args['countyset'] == "")) then debugLog(3, "not set") countySet = nil 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 .. "]") if (countySetParam == "gaa") then useCountyWord = false includeNewCounties = false debugLog(5, "Yes/no parameter [newcounties] reset to [no]") debugLog(5, "Yes/no parameter [usecountyword] reset to [no]") end else countySet = nil 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