Modul:Detect singular: Unterschied zwischen den Versionen

Aus skandinavien-wiki.net
(args.bullets defaults to true)
(make it an option to rewrite the links)
Zeile 12: Zeile 12:
end
end


function p._main(args)
function p._main(origArgs)
local checkComma = not yesNo(args.no_comma,true)
local args = {}
if args.bullets == nil then
-- canonicalize boolean arguments
args.bullets = true
for key, default in pairs({no_comma=false,bullets=true,ignore_links=true}) do
if origArgs[key] then
args[key] = default
else
args[key] = yesNo(origArgs[key],default)
end
end
end
local checkBullets = yesNo(args.bullets,true)
local checkComma = not args.no_comma
local checkBullets = args.bullets
local rewriteLinks = args.ignore_links
local s = args[1]  -- the input string
local s = args[1]  -- the input string
if not s then
if not s then
Zeile 39: Zeile 46:
end
end
-- replace all wikilinks with fixed string
-- replace all wikilinks with fixed string
s = mw.ustring.gsub(s,'%b[]','WIKILINK')  
if rewriteLinks then
s = mw.ustring.gsub(s,'%b[]','WIKILINK')  
end
local hasComma = checkComma and mw.ustring.find(s, '[%a%s],[%a%s]')  
local hasComma = checkComma and mw.ustring.find(s, '[%a%s],[%a%s]')  
local hasAnd = mw.ustring.find(s,'[,%s]and%s')
local hasAnd = mw.ustring.find(s,'[,%s]and%s')

Version vom 8. Januar 2022, 22:43 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 _, count = mw.ustring.gsub(s, pattern, '')
	return count
end

function p._main(origArgs)
	local args = {}
	-- canonicalize boolean arguments
	for key, default in pairs({no_comma=false,bullets=true,ignore_links=true}) do
		if origArgs[key] then
			args[key] = default
		else
			args[key] = yesNo(origArgs[key],default)
		end
	end
	local checkComma = not args.no_comma
	local checkBullets = args.bullets
	local rewriteLinks = args.ignore_links
	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
	-- 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
	local hasComma = checkComma and mw.ustring.find(s, '[%a%s],[%a%s]') 
	local hasAnd = mw.ustring.find(s,'[,%s]and%s')
	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 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