Module:Yesno: Difference between revisions

m
1 revision imported
(better code for getting args)
m (1 revision imported)
 
(13 intermediate revisions by 4 users not shown)
Line 1:
-- Function allowing for consistent treatment of boolean-like wikitext input.
local p = {}
-- It works similarly to the template {{yesno}}.
 
return function p.yesno(frameval, default)
-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you
 
-- should replace "val:lower()" with "mw.ustring.lower(val)" in the
-- defaults
-- following line.
local retvals = {
val = type(val) == 'string' and val:lower() or val
yes = "yes",
if val == nil then
no = "",
return pnil
["¬"] = ""
elseif val == '¬'true then
}
if or val == 'yes' then
 
or val == 'y'
-- Allow arguments to override defaults. Arguments are taken from
or val == 'true'
-- the parent frame; other arguments are ignored.
or val == 't'
local args;
or val == 'on'
if frame == mw.getCurrentFrame() then
or tonumber(val) == 1
-- We're being called via #invoke. If the invoking template passed any args, use
then
-- them. Otherwise, use the args that were passed into the template.
return true
args = frame:getParent().args;
elseif val == false
for k, v in pairs(frame.args) do
or val == 'no'
args = frame.args;
or val == 'n'
break
or val == 'false'
end
or val == 'f'
else
or val == 'off'
-- We're being called from another module or from the debug console, so assume
or tonumber(val) == 0
-- the args are passed in directly.
then
args = frame;
return false
end
else
return default
for k,v in pairs(args) do
end
retvals[k] = v
end
 
val = args[1]
 
-- First deal with the case if val is nil, then deal with other cases.
if val == nil then
return retvals['¬']
end
 
val = val:lower() -- Make lowercase.
val = val:match'^%s*(.*%S)' or '' -- Trim whitespace.
 
if val == '' then
return retvals['blank'] or retvals['no']
elseif val == 'n' or val == 'no' or tonumber(val) == 0 then
return retvals['no']
elseif val == 'y' or val == 'yes' or tonumber(val) == 1 then
return retvals['yes']
elseif val == '¬' then
return retvals['¬']
else
return retvals['def'] or retvals['yes']
end
end
 
return p