Difference between revisions of "Module:Used by"

From Team Fortress Wiki
Jump to: navigation, search
(Created page with "local p = {} function common_string(args) return mw.getCurrentFrame():expandTemplate{title='Common string', args=args} end function p.classes(frame) local args = frame...")
 
(Various improvements/simplifications)
 
Line 5: Line 5:
 
end
 
end
  
function p.classes(frame)
+
function get_args(frame)
     local args = frame.args
+
     local args = {}
    local count = 0
+
     for _, arg in ipairs(frame.args) do
     for _ in pairs(args) do  
+
         table.insert(args, arg)
         count = count + 1
 
 
     end
 
     end
     if count == 0 then
+
     if #args > 0 then return args end
        args = frame:getParent().args
+
 
         for _ in pairs(args) do
+
    for _, arg in ipairs(frame:getParent().args) do
            count = count + 1
+
         table.insert(args, arg)
        end
 
 
     end
 
     end
 
+
 
     local first = args[1] or ''
+
    return args
    if first == 'all' then
+
end
 +
 
 +
function p.classes(frame)
 +
     local args = get_args(frame)
 +
 
 +
    if args[1] == 'all' then
 
         return common_string{'infobox-used-by-all'}
 
         return common_string{'infobox-used-by-all'}
     elseif first == 'all-except' then
+
     elseif args[1] == 'all-except' then
         local ex_args = {}
+
         table.remove(args, 1) -- Remove the 'all-except' argument
         for i = 2, count do
+
         assert(#args >= 1, "You must provide at least 1 class when using 'all-except'.")
            ex_args[i-1] = args[i]
+
         assert(#args <= 2, "You must provide at most 2 classes when using 'all-except'.")
         end
+
         return common_string{'infobox-used-by-all-except-' .. #args, unpack(args)}
        local count = math.max(count - 1, 1)
 
         local text = 'infobox-used-by-all-except-' .. count
 
        return common_string{text, unpack(ex_args)}
 
 
     else
 
     else
         local all_args = {}
+
         assert(#args >= 1, "You must provide at least 1 class when using this function.")
         for i = 0, count do
+
         assert(#args <= 5, "You must provide at most 5 classes when using this function.")
            all_args[i] = args[i]
+
         return common_string{'infobox-used-by-' .. #args, unpack(args)}
         end
 
        local text = 'infobox-used-by-' .. count
 
        return common_string{text, unpack(all_args)}
 
 
     end
 
     end
 
end
 
end
  
 
return p
 
return p

Latest revision as of 22:18, 18 March 2023

Parameter Output
all
{{Used by|all}}
All classes
all-except (max: 2)
{{Used by|all-except|Soldier|etc.}}
All classes (except Soldier)
All classes (except Soldier and Pyro)
classname (max: 5)
{{Used by|Soldier|etc.}}
Soldier
Soldier, Pyro
Soldier, Pyro, Engineer
Soldier, Pyro, Engineer, Demoman
Soldier, Pyro, Engineer, Demoman, Heavy

local p = {}

function common_string(args)
  return mw.getCurrentFrame():expandTemplate{title='Common string', args=args}
end

function get_args(frame)
    local args = {}
    for _, arg in ipairs(frame.args) do
        table.insert(args, arg)
    end
    if #args > 0 then return args end

    for _, arg in ipairs(frame:getParent().args) do
        table.insert(args, arg)
    end

    return args
end

function p.classes(frame)
    local args = get_args(frame)

    if args[1] == 'all' then
        return common_string{'infobox-used-by-all'}
    elseif args[1] == 'all-except' then
        table.remove(args, 1) -- Remove the 'all-except' argument
        assert(#args >= 1, "You must provide at least 1 class when using 'all-except'.")
        assert(#args <= 2, "You must provide at most 2 classes when using 'all-except'.")
        return common_string{'infobox-used-by-all-except-' .. #args, unpack(args)}
    else
        assert(#args >= 1, "You must provide at least 1 class when using this function.")
        assert(#args <= 5, "You must provide at most 5 classes when using this function.")
        return common_string{'infobox-used-by-' .. #args, unpack(args)}
    end
end

return p