Module:Sandbox/BrownHairedGirl/IrelandByCountyCatNav3 explained

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

local Norniron_counties =

local New_counties =

function nil_or_value(s) if (s

nil) then return "nil" end return send

function makeTable local i, myCounty local myTable = '

' .. 'Northern Ireland

\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 .. "

\n" return myTableend

local debugmsg = nillocal getArgs = require('Module:Arguments').getArgslocal p = function make_cat_link(catname, disp) local displaytext if (disp ~= "") then displaytext = disp else displaytext = catname end local link = "" .. displaytext .. "" local fmtlink local linktitle = mw.title.new(catname, "Category") if (linktitle.exists) then fmtlink = link else fmtlink = "

" .. displaytext .. "" end

return fmtlinkend

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_nameend

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 nilend

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 trueend

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 trueend

function argValueFunc(value) if (value

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

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