Jump to content

Module:Check DYK hook/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 13:25, 25 October 2020 (add an underscore to the function name). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- 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

return p