Module:ModuleShowVariantsOrSingle: Difference between revisions

From The First Descendant Wiki
No edit summary
(Manually setting variantID instead of by index)
 
(3 intermediate revisions by the same user not shown)
Line 16: Line 16:
local function addChapter(variantID, moduleText)
local function addChapter(variantID, moduleText)
     return "\n= " .. tostring(variantID) .. " =\n" ..moduleText
     return "\n= " .. tostring(variantID) .. " =\n" ..moduleText
end
local function getVariantFromArgs(frame, variantID)
    if variantID == 1 then
        return frame.args.variant1
    elseif variantID == 2 then
        return frame.args.variant2
    elseif  variantID == 3 then
        return frame.args.variant3
    elseif  variantID == 4 then
        return frame.args.variant4
    elseif  variantID == 5 then
        return frame.args.variant5
    elseif  variantID == 6 then
        return frame.args.variant6
    elseif  variantID == 7 then
        return frame.args.variant7
    elseif  variantID == 8 then
        return frame.args.variant8
    elseif  variantID == 9 then
        return frame.args.variant9
    elseif  variantID == 10 then
        return frame.args.variant10
    else
        return "ERROR: Module:ModuleShowVariantsOrSingle told to pass an unknown VariantID. Please tell us on Discord!"
    end
end
end


Line 26: Line 52:
     local smw_count = tonumber(frame.args.count)
     local smw_count = tonumber(frame.args.count)


     -- Count how many Template:ModuleUniqueDetails have been included as params
     -- Something like frame.args[i] won't work, manually pulling the values by name
    -- If this doesn't match how many SMW entries there are, someone screwed up data entry
     local variant = frame.args.variant1
    local arg_count = 0
    local debugstring = ""
    for k, v in pairs(frame.args) do
        if #v > 1 then
            debugstring = debugstring .. k .. "@"
            arg_count = arg_count + 1
        end
    end
    if true then
        return debugstring
    end
 
    -- Two args are the module name and smw count
     if (arg_count-2) ~= smw_count then
        return "arg_count: " .. tostring(arg_count) .. "  smw_count: " .. tostring(smw_count) .. "randomval: " .. tostring(frame.args[6])
        --return "ERROR: The Module page template requires each module to be defined twice. Make sure you put it in both spots!"
    end


     local result = ""
     local result = ""
     if arg_count > 3 then
     if smw_count > 1 then
         result = moreThanOne(frame.args.module_name)
         result = moreThanOne(frame.args.module_name)
        -- In MediaWiki, args are a table. Named args get a string key, unnamed args get a sequential number key.
         for i=1, smw_count do
        -- Thus, the first unnamed arg will be 1, and we can loop through them all by subtracting the number of named args.
             result = result .. addChapter(i, getVariantFromArgs(frame, i))
         for i=1, (arg_count-2) do
             result = result .. addChapter(i, frame.args[i])
         end
         end


         result = result .. "\n<headertabs />"
         result = result .. "\n<headertabs />"
     else
     else
         result = frame.args[1] --If it's unique and this gives an error: good. You deserve it.
         result = getVariantFromArgs(frame, 1)
     end
     end



Latest revision as of 23:29, 7 June 2024

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

------------------------------------
-- Modules can have variants, or they can be one of a kind.
-- It doesn't make sense to show all the variant fluff if a module is one of a kind.
-- This script shows or hides all that variant info based on how many unique modules are defined,
-- no having to manually pick a template yourself!

local p = {}


local function moreThanOne(module_name)
    local variants_chapter = "== Variants ==\n{{Template:ModuleTableCompareLevels|module_name=" .. module_name .. "}}\n\n"
    local variant_details_chapter = "== Variant Details ==\n"
    return variants_chapter .. variant_details_chapter
end

local function addChapter(variantID, moduleText)
    return "\n= " .. tostring(variantID) .. " =\n" ..moduleText
end

local function getVariantFromArgs(frame, variantID)
    if variantID == 1 then
        return frame.args.variant1
    elseif variantID == 2 then
        return frame.args.variant2
    elseif  variantID == 3 then
        return frame.args.variant3
    elseif  variantID == 4 then
        return frame.args.variant4
    elseif  variantID == 5 then
        return frame.args.variant5
    elseif  variantID == 6 then
        return frame.args.variant6
    elseif  variantID == 7 then
        return frame.args.variant7
    elseif  variantID == 8 then
        return frame.args.variant8
    elseif  variantID == 9 then
        return frame.args.variant9
    elseif  variantID == 10 then
        return frame.args.variant10
    else
        return "ERROR: Module:ModuleShowVariantsOrSingle told to pass an unknown VariantID. Please tell us on Discord!"
    end
end


function p.processData(frame)
    if frame.args.count == nil then
        return "ERROR: Module:ModuleShowVariantsOrSingle not passed a count. Please tell us on Discord!"
    end

    local smw_count = tonumber(frame.args.count)

    -- Something like frame.args[i] won't work, manually pulling the values by name
    local variant = frame.args.variant1

    local result = ""
    if smw_count > 1 then
        result = moreThanOne(frame.args.module_name)
        for i=1, smw_count do
            result = result .. addChapter(i, getVariantFromArgs(frame, i))
        end

        result = result .. "\n<headertabs />"
    else
        result = getVariantFromArgs(frame, 1)
    end

    return frame:preprocess(result)
end

return p