Content added Content deleted
(use parent args again) |
m (1 revision imported: import clickable buttons 2) |
||
(31 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
-- Function allowing for consistent treatment of boolean-like wikitext input. |
|||
local p = {} |
|||
-- It works similarly to the template {{yesno}}. |
|||
function |
return function (val, 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", |
|||
⚫ | |||
no = "", |
|||
⚫ | |||
["¬"] = "" |
|||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
-- Allow arguments to override defaults. Arguments are taken from |
|||
or val == 'true' |
|||
-- the parent frame; other arguments are ignored. |
|||
or val == 't' |
|||
for k,v in pairs(frame:getParent().args) do |
|||
⚫ | |||
retvals[k] = v |
|||
then |
|||
⚫ | |||
return true |
|||
⚫ | |||
val = frame:getParent().args[1] |
|||
or val == 'no' |
|||
or val == 'n' |
|||
-- First deal with the case if val is nil, then deal with other cases. |
|||
or val == 'false' |
|||
or val == 'f' |
|||
return retvals['¬'] |
|||
⚫ | |||
end |
|||
then |
|||
return false |
|||
val = val:lower() -- Make lowercase. |
|||
⚫ | |||
val = val:match'^%s*(.*%S)' or '' -- Trim whitespace. |
|||
return default |
|||
⚫ | |||
-- Cases are ordered by (probable) likelihood of use. |
|||
⚫ | |||
return retvals['blank'] or retvals['no'] |
|||
⚫ | |||
return retvals['yes'] |
|||
⚫ | |||
return retvals['no'] |
|||
⚫ | |||
return retvals['yes'] |
|||
⚫ | |||
return retvals['no'] |
|||
elseif val == '¬' then |
|||
return retvals['¬'] |
|||
⚫ | |||
return retvals['yes'] |
|||
⚫ | |||
return retvals['no'] |
|||
⚫ | |||
return retvals['def'] or retvals['yes'] |
|||
end |
|||
end |
end |
||
⚫ |
Latest revision as of 00:31, 28 March 2020
Documentation for this module may be created at Module:Yesno/doc
-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.
return function (val, 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
-- following line.
val = type(val) == 'string' and val:lower() or val
if val == nil then
return nil
elseif val == true
or val == 'yes'
or val == 'y'
or val == 'true'
or val == 't'
or tonumber(val) == 1
then
return true
elseif val == false
or val == 'no'
or val == 'n'
or val == 'false'
or val == 'f'
or tonumber(val) == 0
then
return false
else
return default
end
end