Module:Check DYK hook: Difference between revisions
not sure why but this seems to fix it |
fix whitespace trimming; only trim whitespace in p.isValidHook, not in the Lua p._isValidHook interface; add a "hook" keyword parameter; and check the type of the input in p._isValidHook |
||
Line 1: | Line 1: | ||
-- This module performs validation checks for [[WP:DYK]] hooks |
-- This module performs validation checks for [[WP:DYK]] hooks |
||
local libraryUtil = require('libraryUtil') |
|||
local p = {} |
local p = {} |
||
Line 7: | Line 8: | ||
-- Check that hooks start with three periods, followed by an acceptable |
-- Check that hooks start with three periods, followed by an acceptable |
||
-- follow-on word. |
-- follow-on word. |
||
"^ |
"^%.%.%. *that", |
||
"^ |
"^%.%.%. *about", |
||
}, |
}, |
||
{ |
{ |
||
-- Check that hooks end with a question mark, or another acceptable |
-- Check that hooks end with a question mark, or another acceptable |
||
-- phrase. |
-- phrase. |
||
[[.%?%]*'*"? |
[[.%?%]*'*"?$]], |
||
[[.?</span>%]*'*"? |
[[.?</span>%]*'*"?$]], |
||
"[Yy]ou probably did%.+ |
"[Yy]ou probably did%.+$", |
||
} |
} |
||
} |
} |
||
Line 24: | Line 25: | ||
-- a hook is valid or not. Hooks are treated as valid if they match at least |
-- a hook is valid or not. Hooks are treated as valid if they match at least |
||
-- one pattern from each group. |
-- one pattern from each group. |
||
libraryUtil.checkType("_isValidHook", 1, hook, "string") |
|||
for _, patternGroup in ipairs(validationPatternGroups) do |
for _, patternGroup in ipairs(validationPatternGroups) do |
||
local found = false |
local found = false |
||
Line 40: | Line 42: | ||
function p.isValidHook(frame) |
function p.isValidHook(frame) |
||
local |
local args = frame.args |
||
local hook = args.hook or args[1] |
|||
if not hook then |
if not hook then |
||
error("No hook specified") |
error("No hook specified") |
||
end |
end |
||
hook = |
hook = hook:match('^%s*(.-)%s*$') -- Trim whitespace |
||
if p._isValidHook(hook) then |
if p._isValidHook(hook) then |
||
return "yes" |
return "yes" |
Latest revision as of 12:31, 29 November 2020
This module checks the format of Wikipedia:Did you know hooks used in {{DYK talk}} and {{Article history}}.
Usage
This module can be used from templates or from other Lua modules.
From templates
From templates, call the isValidHook
function, and use the |hook=
parameter to specify the hook.
{{#invoke:Check DYK hook|isValidHook|hook=hook}}
Valid hooks will return a value of "yes"; invalid hooks will return the empty string.
You can also use the first positional parameter to specify the hook. If you do this, it is recommended to use |1=
explicitly; if not, and the hook contains an equals sign, the text before the equals sign will be treated as a parameter name and the hook will not be recognised.
From modules
From modules, call the _isValidHook
function with the hook as the first positional parameter.
mCheckDYKHook = require('Module:Check DYK hook')
result = mCheckDYKHook._isValidHook(hook)
Valid hooks will return true
; invalid hooks will return false
.
-- This module performs validation checks for [[WP:DYK]] hooks
local libraryUtil = require('libraryUtil')
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.
[[.%?%]*'*"?$]],
[[.?</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.
libraryUtil.checkType("_isValidHook", 1, hook, "string")
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 args = frame.args
local hook = args.hook or args[1]
if not hook then
error("No hook specified")
end
hook = hook:match('^%s*(.-)%s*$') -- Trim whitespace
if p._isValidHook(hook) then
return "yes"
else
return ""
end
end
return p