Module:Yesno: Difference between revisions

m
26 revisions imported from wikipedia:Module:Yesno: this one is missing for the BS map to work
(more accurate default value for "no")
m (26 revisions imported from wikipedia:Module:Yesno: this one is missing for the BS map to work)
 
(23 intermediate revisions by 9 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 = "<!-- null -->",
return pnil
["¬"] = ""
elseif val == '¬'true then
}
if or val == 'yes' then
 
or val == 'y'
-- Allow arguments to override defaults.
or val == 'true'
-- 'any' tracks the presence of any arguments at all.
or val == 't'
local args = frame.args
or val == 'on'
local any = false
or tonumber(val) == 1
for k,v in pairs(args) do
then
any = true
return true
retvals[k] = v
elseif val == false
end
or val == 'no'
-- If there are no arguments, try and get them from the parent frame.
or val == 'n'
if any == false then
or val == 'false'
local pframe = frame:getParent()
or val == 'f'
args = pframe.args
or val == 'off'
for k,v in pairs(args) do
or tonumber(val) == 0
retvals[k] = v
then
end
return false
end
else
 
return default
val = args[1]
end
 
-- 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