Difference between revisions of "Module:Yesno"

From BESA® Wiki
Jump to: navigation, search
m (1 revision)
(Ported Template:Yesno to Lua)
(23 intermediate revisions by 3 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 p.yesno( frame )
return function (val, default)
+
-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you
+
    local retvals = {
-- should replace "val:lower()" with "mw.ustring.lower(val)" in the
+
        yes  = "yes",
-- following line.
+
        no   = "",
val = type(val) == 'string' and val:lower() or val
+
        ["¬"] = ""
if val == nil then
+
    }
return nil
+
elseif val == true
+
    local any = false
or val == 'yes'
+
    for k,v in pairs( frame.args ) do
or val == 'y'
+
        any = true
or val == 'true'
+
        retvals[k] = v
or val == 't'
+
    end
or tonumber(val) == 1
+
then
+
    val = frame.args[1]
return true
+
elseif val == false
+
    if (val == nil and not any) or val == '¬' then
or val == 'no'
+
        return retvals['¬']
or val == 'n'
+
    end
or val == 'false'
+
or val == 'f'
+
    val = (val or ''):lower()
or tonumber(val) == 0
+
then
+
    if val == '' then
return false
+
        return retvals['blank'] ~= nil and retvals['blank'] or retvals['no']
else
+
    elseif val == 'n' or val == 'no' or val == '0' then
return default
+
        return retvals['no']
end
+
    elseif val == 'y' or val == 'yes' or val == '1' or retvals['def'] == nil then
 +
        return retvals['yes']
 +
    else
 +
        return retvals['def']
 +
    end
 
end
 
end
 +
 +
return p

Revision as of 11:40, 28 February 2013

Documentation for this module may be created at Module:Yesno/doc

Script error: Lua error: Internal error: The interpreter exited with status 126.

local p = {}
 
function p.yesno( frame )
 
    local retvals = {
        yes   = "yes",
        no    = "",
        ["¬"] = ""
    }
 
    local any = false
    for k,v in pairs( frame.args ) do
        any = true
        retvals[k] = v
    end
 
    val = frame.args[1]
 
    if (val == nil and not any) or val == '¬' then
        return retvals['¬']
    end
 
    val = (val or ''):lower()
 
    if val == '' then
        return retvals['blank'] ~= nil and retvals['blank'] or retvals['no']
    elseif val == 'n' or val == 'no'  or val == '0' then
        return retvals['no']
    elseif val == 'y' or val == 'yes' or val == '1' or retvals['def'] == nil then
        return retvals['yes']
    else
        return retvals['def']
    end
end
 
return p