Module:GAAbyCountyCatNav

--[[ GAA v0, forked from v20 each title consists of 3 parts    * prefix    * county name    * suffix e.g. "Foo in Mayo"    * prefix = "Foo in "    * county name = Mayo"    * suffix = ""]]-- configlocal textSize = '90%'local tableClass="toc"local tableFallbackMaxWidth="auto"local tableMaxWidth="calc(100% - 25em)" -- Template:GeoGroup has width: 23em<-- local tableStyle="margin-left:0; margin-right:auto; clear:left !important; margin-top:0 !important; float:left; width:auto;"local tableStyle="margin-left:0; margin-right:auto; clear:left !important; margin-top:0 !important; width:auto;"local evenRowStyle = "vertical-align:top; background-color:#f3f3f3;"local oddRowStyle = "vertical-align:top;"local labelStyle = "text-align:right; font-weight: bold; padding: 0.25em 0.5em 0.25em 0.5em;"local listStyle = "text-align:left; font-weight: normal; padding: 0.25em 0.5em 0.25em 0.5em;"local greyLinkColor = "#888"local callingTemplates = {'Template:GAAbyCountyCatNav'}-- globals for this modulelocal debugging = falselocal debugmsg = ""local tableRowNum = 0local title_prefix = ""local title_suffix = ""local thisPageCountylocal greyLinkCount = 0local blueLinkCount = 0local parentname = ""local templateNamelocal getArgs = require('Module:Arguments').getArgslocal yesno = require('Module:Yesno')local horizontal = require('Module:List').horizontallocal p = {}local IrishGAACounties = {'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 OverseasGAACounties = {'London','New York'}local function publishDebugLog()if not debugging thenreturn ""endreturn "==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 nilfunction debugLog(level, msg)if (debugmsg == nil) thendebugmsg = ""endif (level ~= nil) then-- not appending, so make a new linedebugmsg = debugmsg .. "\n"-- then add the levellocal ifor i = 1, level doif (i % 2) == 1 thendebugmsg = debugmsg .. "#"elsedebugmsg = debugmsg .. "*"endend enddebugmsg = debugmsg .. " " .. msgreturn trueendlocal function trackingCategory()-- discount this page, which will always be coded as a blue link, but rendered as bold un-navigableblueLinkCount = blueLinkCount - 1if greyLinkCount == 0 thenreturn "[[Category:" .. templateName .. " with no grey links]]"endif blueLinkCount == 0 thenreturn "[[Category:" .. templateName .. " with all grey links]]"endif greyLinkCount > 25 thenreturn "[[Category:" .. templateName .. " with over 25 grey links]]"elseif greyLinkCount > 15 thenreturn "[[Category:" .. templateName .. " with over 15 grey links]]"elseif greyLinkCount > 5 thenreturn "[[Category:" .. templateName .. " with over 5 grey links]]"endreturn ""end-- Make a piped link to a category, if it exists-- If it doesn't exist, just display the greyed the link title without linkingfunction makeCatLink(catname, disp)local displaytextif (disp ~= "") and (disp ~= nil) then-- use 'disp' parameter, but strip any trailing disambiguatordisplaytext = mw.ustring.gsub(disp, "%s+%(.+$", "")elsedisplaytext = catnameendlocal fmtlinklocal catPage = mw.title.new( catname, "Category" )if (catPage.exists) thenfmtlink = "[[:Category:" .. catname .. "|" .. displaytext .. "]]"blueLinkCount = blueLinkCount + 1elsefmtlink = '<span style="color:' .. greyLinkColor .. '">' .. displaytext .. "</span>"greyLinkCount = greyLinkCount + 1endreturn fmtlinkendlocal function makeCatName(countyName, prefix, suffix)local this_cat_name = '';this_cat_name = this_cat_name .. prefixthis_cat_name = this_cat_name .. countyNamethis_cat_name = this_cat_name .. suffixreturn this_cat_nameendlocal function makeTableRow(rowLabel, useCountyWord, countyList)debugLog(2, "makeTableRow, label: ")if (rowLabel == nil) thenrowLabel = "By&nbsp;county"debugLog(nil, rowLabel)elserowLabel = mw.text.trim(rowLabel)debugLog(nil, " [" .. rowLabel .. "]")endtableRowNum = tableRowNum + 1local thisRowif (tableRowNum % 2) == 0 thendebugLog(3, "Even-numbered")thisRow = '<tr style="' .. evenRowStyle .. '">\n'elsedebugLog(3, "Odd-numbered")thisRow = '<tr style="' .. oddRowStyle .. '">\n'endif not ((rowLabel == nil) or (rowLabel =="")) thenthisRow = thisRow .. '<td style="' .. labelStyle .. '">' .. rowLabel .. ': </td>\n'end-- now begin making the row contentsdebugLog(3, "Process countyList")local list_args = {}for i, aCounty in ipairs(countyList) dodebugLog(4, "No. [" .. tostring(i) .. ": [" .. aCounty .. "]")myCatName = makeCatName(aCounty, title_prefix, title_suffix)table.insert(list_args, makeCatLink(myCatName, aCounty))endreturn thisRow .. '<td style="' .. listStyle .. ';">' .. horizontal(list_args) .. '</td>\n</tr>'endlocal function makeTable()debugLog(1, "makeTable")tableRowNum = 0local myTable = '<table class="' .. tableClass .. '"'myTable = myTable .. ' style="' .. tableStyle .. '; font-size:' .. textSize .. '; max-width:' .. tableFallbackMaxWidth .. '; max-width:' .. tableMaxWidth ..'">\n'myTable = myTable .. makeTableRow("Irish&nbsp;GAA&nbsp;Counties", useCountyWord, IrishGAACounties)myTable = myTable .. makeTableRow("Overseas&nbsp;GAA&nbsp;Counties", useCountyWord, OverseasGAACounties)myTable = myTable .. "</table>\n"return myTableendlocal function patternSearchEncode(s)return mw.ustring.gsub(s, "([%W])", "%%%1")end-- Does the pagename include a county name?local function findCountyNameInPagename(pn, countylist, description)debugLog(2, "checking [" .. pn .."] for a county name in county set: " .. description)for i, aCounty in ipairs(countylist) dodebugLog(3, "testing: ["  .. aCounty .. "]")local testCountyEncoded = patternSearchEncode(aCounty)-- 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 worddebugLog(4, "simple match? ")if (not mw.ustring.match(pn, testCountyEncoded)) thendebugLog(nil, "Fail")elsedebugLog(nil, "Success")debugLog(4, "match at start, followed by separator? ")if mw.ustring.match(pn, "^" .. testCountyEncoded .. "[^%w]") thendebugLog(nil, "Yes")return aCountyenddebugLog(nil, "No")debugLog(4, "match at end, preceded by separator? ")if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "$") thendebugLog(nil, "Yes")return aCountyenddebugLog(nil, "No")debugLog(4, "match anywhere, preceded and followed by separator? ")if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "[^%w]") thendebugLog(nil, "Yes")return aCountyenddebugLog(nil, "No")endendreturn nilend-- parse the pagename to find 3 parts: prefix, county name, suffixlocal function parsePagename(pn)debugLog(1, "parsePagename: [" .. pn .. "]")if mw.ustring.match(pn, "^County%s+") ~= nil thendebugLog(2, 'Invalid. [' .. pn .. '] includes word "County"')return falseendlocal validCountyNamevalidCountyName = findCountyNameInPagename(pn, IrishGAACounties, "Irish GAA Counties")if (validCountyName == nil) thenvalidCountyName = findCountyNameInPagename(pn, OverseasGAACounties, "Overseas GAA Counties")endif validCountyName == nil thenreturn falseend-- if we get here, the page name "pn" includes a validCountyName-- so now we need to split the stringdebugLog(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_prefixtitle_suffix = match_suffixthisPageCounty = match_countydebugLog(2, "parse successful")debugLog(3, "title_prefix = [" .. title_prefix .. "]")debugLog(3, "thisPageCounty = [" .. thisPageCounty .. "]")debugLog(3, "title_suffix = [" .. title_suffix .. "]")return trueendlocal function getYesNoParam(args, thisParamName, defaultVal)local returnValuedebugLog(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) thendebugLog(nil, "Yes")elsedebugLog(nil, "No")endreturn returnValueendlocal function isValidParent(p)for i, aParent in ipairs(callingTemplates) doif p == aParent thenreturn trueendendreturn falseendfunction p.main(frame)local parent = frame:getParent()if parent then parentname = parent:getTitle():gsub('/sandbox$', '')endif (parentname == nil) or not isValidParent(parentname) thenlocal errormsg = '<p class="error"> Error: ' .. parentname .. ' is not a valid wrapper for [[' .. frame:getTitle() .. ']]\n'errormsg = errormsg .. '<br><br>Valid wrappers: 'local i, aParentfor i, aParent in ipairs(callingTemplates) doerrormsg = errormsg .. '[[' .. aParent  .. ']]'enderrormsg = errormsg .. '</p>'return errormsgendtemplateName = mw.ustring.gsub(parentname, "^Template:", "")debugLog(1, "Check parameters")debugging = getYesNoParam(frame.args, "debug", false)-- get the page titlethispage = 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)) thendebugLog(nil, "No, this is not a category")debugLog(1, "Not a category, so no output")return publishDebugLog()enddebugLog(nil, "Yes, this is a category")if not parsePagename(thispagename) then-- some error parsing the title, so don't proceed to outputreturn publishDebugLog()enddebugLog(1, "all parse done")debugLog(2, "title_prefix = [" .. title_prefix .. "]")debugLog(2, "title_suffix = [" .. title_suffix .. "]")return publishDebugLog() .. makeTable() .. trackingCategory()endreturn p