Module:Lswitch

From Wikipedia

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

local bodge = require "Module:mw"

local z = {}

-- This is used by template {{Lswitch}}.
function z.lua_switches(frame)
    local parmframe = frame:getParent()
    local args= parmframe.args --parameters passed TO template by caller page
    local config= frame.args --arguments passed BY template from inside
    local pagename = config.pagename or ""
    local namespace = config.namespace or ""

    local title, disambiguator 
    if "true" ~= args.force then -- This is a string, "true", not a boolean.
        title, disambiguator = pagename:match("(.*)(%(.*%))")
        if nil == title then
            local index = 1
            while true do
                local arg = args[index]
                if arg == nil then break end
                title, disambiguator = pagename:match("(.*)(" .. arg .. ")")
                if title ~= nil then break end
                index = index + 1
            end
        end
    end
    if nil == title then title, disambiguator = pagename, "" end
    if "" ~= namespace then namespace = namespace .. ":" end
    return frame:preprocess("{{DISPLAYTITLE:" .. namespace .. "''" .. title .. "''" .. (disambiguator or "") .. "}}")
end

-- This is used by template {{Lswitch}}.
function z.switch(frame)
    local pframe = frame:getParent()
    local args = pframe.args -- arguments passed TO template by caller page
    local config = frame.args -- arguments passed BY the template inside
    local p1 = args[1] or ""
    local p2 = args[2] or ""
    local p3 = args[3] or ""
 
    local debugsw = args.debug or ""
    local p
    local say = "(no match)"

    local index = 2
    while true do
      p = args[index]
      if p == nil then break end
      if p == p1 then
        say = args[index+1]
        break
      end
      index = index + 2
    end

    if debugsw ~= "" then
      say = say.."<br>'''Lswitch (ver M1a):'''  p1=" ..p1
        .. "  p2=" ..p2 .. "  p3=" ..p3
        .. "  debugsw=" .. debugsw
    end

    return say
end

return z
-------------------------------------------------------------------------
--HISTORY:
--25Mar2013 Created.
--25Mar2013 Expanded to unlimited parameters ("args[index]").
--
--
-- End Module:Lswitch