Modul:Detect singular: Unterschied zwischen den Versionen
(flip argument meaning) |
(add comments) |
||
| Zeile 3: | Zeile 3: | ||
local yesNo = require('Module:Yesno') | local yesNo = require('Module:Yesno') | ||
-- function to determine whether "sub" occurs in "s" | |||
local function plainFind(s, sub) | local function plainFind(s, sub) | ||
return mw.ustring.find(s, sub, 1, true) | return mw.ustring.find(s, sub, 1, true) | ||
end | end | ||
-- function to count the number of times "pattern" (a regex) occurs in "s" | |||
local function countMatches(s, pattern) | local function countMatches(s, pattern) | ||
local _, count = mw.ustring.gsub(s, pattern, '') | local _, count = mw.ustring.gsub(s, pattern, '') | ||
| Zeile 12: | Zeile 14: | ||
end | end | ||
-- 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) | |||
function p._main(origArgs) | function p._main(origArgs) | ||
local args = {} | local args = {} | ||
| Zeile 48: | Zeile 56: | ||
s = mw.ustring.gsub(s,'%b[]','WIKILINK') | s = mw.ustring.gsub(s,'%b[]','WIKILINK') | ||
end | end | ||
-- Five conditions: any one of them can make the string a plural | |||
local hasComma = checkComma and mw.ustring.find(s, '%D[,;]%D') -- semi-colon similar to comma | local hasComma = checkComma and mw.ustring.find(s, '%D[,;]%D') -- semi-colon similar to comma | ||
local hasAnd = mw.ustring.find(s,'[,%s]and%s') | local hasAnd = mw.ustring.find(s,'[,%s]and%s') | ||
| Zeile 53: | Zeile 62: | ||
local hasBullets = countMatches(s,'%*+') > 1 | local hasBullets = countMatches(s,'%*+') > 1 | ||
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row | local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row | ||
-- return bool: is it singular? | |||
return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids) | return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids) | ||
end | end | ||
| Zeile 69: | Zeile 79: | ||
function p.main(frame) | function p.main(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
-- For template, return 1 if singular, blank if not | |||
if p._main(args) then | if p._main(args) then | ||
return 1 | return 1 | ||
Version vom 9. Januar 2022, 06:54 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Detect singular/doc erstellt werden
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesNo = require('Module:Yesno')
-- 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 count
end
-- 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)
function p._main(origArgs)
local args = {}
-- canonicalize boolean arguments
for key, default in pairs({no_comma=false,parse_links=false}) do
if origArgs[key] == nil then
args[key] = default
else
args[key] = yesNo(origArgs[key],default)
end
end
local checkComma = not args.no_comma
local rewriteLinks = not args.parse_links
local s = origArgs[1] -- the input string
if not s then
return nil -- empty input returns nil
end
s = tostring(s)
if plainFind(s,'forcedetectsingular') then -- magic data string to return true
return true
end
if plainFind(s,'forcedetectplural') then -- magic data string to return false
return false
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 == 1 then
return true
end
if numListItems > 1 then
return false
end
-- replace all wikilinks with fixed string
if rewriteLinks then
s = mw.ustring.gsub(s,'%b[]','WIKILINK')
end
-- Five conditions: any one of them can make the string a plural
local hasComma = checkComma and mw.ustring.find(s, '%D[,;]%D') -- semi-colon similar to comma
local hasAnd = mw.ustring.find(s,'[,%s]and%s')
local hasBreak = mw.ustring.find(s,'<%s*br')
local hasBullets = countMatches(s,'%*+') > 1
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row
-- return bool: is it singular?
return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids)
end
function p._pluralize(args)
local singularForm = args[3] or ""
local pluralForm = args[4] or ""
if args[5] then
link = tostring(args[5])
singularForm = '[['..link..'|'..singularForm..']]'
pluralForm = '[['..link..'|'..pluralForm..']]'
end
return args[2] and pluralForm or p._main(args) and singularForm or pluralForm
end
function p.main(frame)
local args = getArgs(frame)
-- For template, return 1 if singular, blank if not
if p._main(args) then
return 1
end
return ""
end
function p.pluralize(frame)
local args = getArgs(frame)
return p._pluralize(args)
end
return p