Jump to content

Module:Check DYK hook: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Undid revision 990847290 by Frietjes (talk) this broke the check
not sure why but this seems to fix it
Line 44: Line 44:
error("No hook specified")
error("No hook specified")
end
end
hook = hook:match("^%s*(.*?)%s*$") -- Trim whitespace
hook = mw.ustring.gsub(hook, "^%s*(.*?)%s*$", "%1") -- Trim whitespace
if p._isValidHook(hook) then
if p._isValidHook(hook) then
return "yes"
return "yes"

Revision as of 15:39, 27 November 2020

-- This module performs validation checks for [[WP:DYK]] hooks

local p = {}

local validationPatternGroups = {
	{
		-- Check that hooks start with three periods, followed by an acceptable
		-- follow-on word.
		"^ *%.%.%. *that",
		"^ *%.%.%. *about",	
	},
	{
		-- Check that hooks end with a question mark, or another acceptable
		-- phrase.
		[[.%?%]*'*"? *$]],
		[[.&#63;</span>%]*'*"? *$]],
		"[Yy]ou probably did%.+ *$",
	}
}

function p._isValidHook(hook)
	-- Whether the given hook is valid.
	-- We use the patterns in the validationPatternGroups table to find whether
	-- a hook is valid or not. Hooks are treated as valid if they match at least
	-- one pattern from each group.
	for _, patternGroup in ipairs(validationPatternGroups) do
		local found = false
		for _, pattern in ipairs(patternGroup) do
			if mw.ustring.find(hook, pattern) then
				found = true
				break
			end
		end
		if not found then
			return false
		end
	end
	return true
end

function p.isValidHook(frame)
	local hook = frame.args[1]
	if not hook then
		error("No hook specified")
	end
	hook = mw.ustring.gsub(hook, "^%s*(.*?)%s*$", "%1") -- Trim whitespace
	if p._isValidHook(hook) then
		return "yes"
	else
		return ""
	end
end

return p