Module:Print ref/Matcher

local p = {}

-- how templates can call this module -- only uses one unnamed arg function p.matchCodeTemplate(frame) local printWork = frame.args[1] printCode, issueNumber = p.matchCode(printWork) return printCode, issueNumber end

-- for other modules to use the query pairs to match any input string to the formatted episode code -- can handle if an issue number is included function p.matchCode(printWork) local printCode local issueNumber if isempty(printWork) then return printCode, issueNumber end printInfos = mw.loadData('Module:Print ref/Array') printWork = printWork:lower --1st way: try to match the key exactly if searchKeys(printWork, printInfos) then printCode = printWork else queryPairs = makeQueryPairs printWork = printWork:gsub("%s+", ""):gsub("_", "")

--2nd way: try to match title or alternate codes exactly if queryPairs[printWork] then printCode = queryPairs[printWork]

--3rd way: see if it's a comic + issue number elseif searchWithinKeys(printWork, queryPairs) then codeToMatch, issueNumber = searchWithinKeys(printWork, queryPairs) printCode = queryPairs[codeToMatch] -- handle VMO I issues if printCode == 'vmo' and tonumber(issueNumber) then printCode = 'vmo i'           end end end return printCode, issueNumber end

-- for converting Print ref/Array into pairs of input string:output episode code -- ignores spaces in matching; make sure no array keys are the same except for spaces function makeQueryPairs local printInfos = mw.loadData('Module:Print ref/Array') queryPairs = {} for k, v in pairs(printInfos) do       queryPairs[k:lower:gsub("%s+", "")] = k -- remove spaces for key, val in pairs(v) do           if key == 'issues' then elseif type(val) == 'string' then queryPairs[val:lower:gsub("%s+", "")] = k           else for i, option in pairs(val) do                   queryPairs[option:lower:gsub("%s+", "")] = k                    end end end end return queryPairs end

-- functions for searching an array for a value (case-insensitive) function keyset(dict) local keys = {} for k,v in pairs(dict) do	   keys[k:lower]=true end return keys end

function searchKeys(item, dict) local keys = keyset(dict) if keys[item:lower] then return true else return false end end

-- if the code includes an issueNumber, return printCode + issueNumber function searchWithinKeys(item, dict) item = item:lower:gsub(' ', '') keys = keyset(dict) local match = false local printCode local issueNumber for k, v in pairs(keys) do       k2 = k:gsub(' ', '') if match == false then if item:find(k2) or k2:find(item) then remainder = item:gsub(k2, ):gsub(k, ) -- only counts as an issue if the remainder is a number if tonumber(remainder) then match = true codeToMatch = k                   issueNumber = remainder break -- most permissive result else codeToMatch = k2               end end end end return codeToMatch, issueNumber end

--function to determine if a string is empty function isempty(s) return s == nil or s == '' end

return p