Modul:Detect singular: Unterschied zwischen den Versionen
(count the number of list elements) |
(an entry in a list should always be singular, also) |
||
| Zeile 21: | Zeile 21: | ||
end | end | ||
s = tostring(s) | s = tostring(s) | ||
if plainFind(s,'forcedetectsingular') then -- magic data string to return true | if plainFind(s,'forcedetectsingular') then -- magic data string to return true | ||
return true | return true | ||
| Zeile 28: | Zeile 27: | ||
return false | return false | ||
end | end | ||
-- replace all wikilinks with fixed string | |||
s = mw.ustring.gsub(s,'%b[]','WIKILINK') | |||
-- replace all list items with a fixed string | |||
s, numListItems = mw.ustring.gsub(s,'<%s*li%s*>.-<%s*/li%s*>','<li>LISTITEM</li>') | |||
local hasComma = checkComma and mw.ustring.find(s, '%a,%s') | local hasComma = checkComma and mw.ustring.find(s, '%a,%s') | ||
local hasMultipleListElements = | local hasMultipleListElements = numListItems > 1 | ||
local hasAnd = mw.ustring.find(s,'%Aand%A') | local hasAnd = mw.ustring.find(s,'%Aand%A') | ||
local hasBreak = mw.ustring.find(s,'<%s*br') | local hasBreak = mw.ustring.find(s,'<%s*br') | ||
Version vom 8. Januar 2022, 08:07 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')
local function plainFind(s, sub)
return mw.ustring.find(s, sub, 1, true)
end
local function countMatches(s, pattern)
local dbg, count = mw.ustring.gsub(s, pattern, 'ASTERISK')
mw.log(dbg)
return count
end
function p._main(args)
local checkComma = not yesNo(args.no_comma,true)
local checkBullets = yesNo(args.bullets,true)
local s = args[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
-- replace all wikilinks with fixed string
s = mw.ustring.gsub(s,'%b[]','WIKILINK')
-- replace all list items with a fixed string
s, numListItems = mw.ustring.gsub(s,'<%s*li%s*>.-<%s*/li%s*>','<li>LISTITEM</li>')
local hasComma = checkComma and mw.ustring.find(s, '%a,%s')
local hasMultipleListElements = numListItems > 1
local hasAnd = mw.ustring.find(s,'%Aand%A')
local hasBreak = mw.ustring.find(s,'<%s*br')
local hasBullets = checkBullets and countMatches(s,'%*+') > 1
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row
return not (hasComma or hasMultipleListElements or hasAnd or hasBreak or hasBullets or multipleQids)
end
function p.main(frame)
local args = getArgs(frame)
if p._main(args) then
return 1
end
return ""
end
return p