local p = local getArgs = require('Module:Arguments').getArgslocal yesNo = require('Module:Yesno')local getPlain = require('Module:Text').Text.getPlain
-- function to determine whether "sub" occurs in "s"local function plainFind(s, sub) return mw.ustring.find(s, sub, 1, true)end
-- function to count the number of times "pattern" (a regex) occurs in "s"local function countMatches(s, pattern) local _, count = mw.ustring.gsub(s, pattern, ) return countend
local singular = 1local likelyPlural = 2local plural = 3
-- Determine whether a string is singular or plural (i.e., it represents one-- item or many)-- Arguments:-- origArgs[1]: string to process-- origArgs.no_comma: if false, use commas to detect plural (default false)-- origArgs.parse_links: if false, treat wikilinks as opaque singular objects (default false)-- Returns:-- singular, likelyPlural, or plural (see constants above), or nil for completely unknownfunction p._main(origArgs) origArgs = type(origArgs)
nil then args[key] = default else args[key] = yesNo(origArgs[key],default) end end local checkComma = not args.no_comma local checkAnd = not args.no_and local rewriteLinks = not args.parse_links local anyComma = args.any_comma local s = origArgs[1] -- the input string if not s then return nil -- empty input returns nil end s = tostring(s) s = mw.text.decode(s,true) --- replace HTML entities (to avoid spurious semicolons) if plainFind(s,'data-plural="0"') then -- magic data string to return true return singular end if plainFind(s,'data-plural="1"') then -- magic data string to return false return plural end -- count number of list items local numListItems = countMatches(s,'<%s*li') -- if exactly one, then singular, if more than one, then plural if numListItems
function p._pluralize(args) args = type(args)
nil then return "" -- return blank on complete failure end if detect
likelyPlural then return likelyForm else return pluralForm endend
function p.main(frame) local args = getArgs(frame) -- For template, return 1 if singular, blank if plural or empty local result = p._main(args) if result
singular and 1 or ""end
function p.pluralize(frame) local args = getArgs(frame) return p._pluralize(args)end
return p