Module:Alternatives: Difference between revisions

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
aaa
make iteration over possible conditions better
Line 3: Line 3:
local conditionDescriptions = {
local conditionDescriptions = {
     -- genders
     -- genders
     m = "If player character is male",
     {"m", "If player character is male"},
     f = "If player character is female",
     {"f", "If player character is female"},
     -- races
     -- races
     hyur = "If player character is a Hyur",
     {"hyur", "If player character is a Hyur"},
     elezen = "If player character is an Elezen",
     {"elezen", "If player character is an Elezen"},
     lalafell = "If player character is a Lalafell",
     {"lalafell", "If player character is a Lalafell"},
     miqote = "If player character is a Miqo'te",
     {"miqote", "If player character is a Miqo'te"},
     roegadyn = "If player character is a Roegadyn",
     {"roegadyn", "If player character is a Roegadyn"},
     aura = "If player character is an Au Ra",
     {"aura", "If player character is an Au Ra"},
     viera = "If player character is a Viera",
     {"viera", "If player character is a Viera"},
     hrothgar = "If player character is a Hrothgar",
     {"hrothgar", "If player character is a Hrothgar"},
}
}


Line 25: Line 25:
     local first = true
     local first = true


     for condition, alternative in pairs(args) do
     for _, possibleCondition in pairs(conditionDescriptions) do
         -- ignore number keys, those are handled later
         local alternative = args[possibleCondition[0]]
         if type(condition) == "string" then
         if alternative then
            local description = conditionDescriptions[condition]
             if first then
             if not description then
                 -- skip adding the separator before the first alternative
                 result:node(mw.html.create("span")
                 first = false
                    :addClass("error")
                    :wikitext(("Error: Unknown condition \"%s\""):format(condition))
                 )
 
             else
             else
                 if first then
                 -- add the separator
                    -- skip adding the separator before the first alternative
                 result:node(separator)
                    first = false
            end
                 else
                    -- add the separator
                    result:node(separator)
                end


                result:node(mw.html.create("span")
            result:node(mw.html.create("span")
                    :addClass("alternatives__alternative")
                :addClass("alternatives__alternative")
                    :attr("title", description)
                :attr("title", possibleCondition[1])
                    :wikitext(alternative)
                :wikitext(alternative)
                )
            )
            end
         end
         end
     end
     end

Revision as of 21:19, 21 February 2026

Documentation for Module:Alternatives does not exist yet [create] (How does this work?)
local getArgs = require("Module:Arguments").getArgs

local conditionDescriptions = {
    -- genders
    {"m", "If player character is male"},
    {"f", "If player character is female"},
    -- races
    {"hyur", "If player character is a Hyur"},
    {"elezen", "If player character is an Elezen"},
    {"lalafell", "If player character is a Lalafell"},
    {"miqote", "If player character is a Miqo'te"},
    {"roegadyn", "If player character is a Roegadyn"},
    {"aura", "If player character is an Au Ra"},
    {"viera", "If player character is a Viera"},
    {"hrothgar", "If player character is a Hrothgar"},
}

local separator = [[<span class="alternatives__separator">/</span>]]
local p = {}

function p.main(frame)
    local args = getArgs(frame, {parentOnly = true})

    local result = mw.html.create("span"):addClass("alternatives")
    local first = true

    for _, possibleCondition in pairs(conditionDescriptions) do
        local alternative = args[possibleCondition[0]]
        if alternative then
            if first then
                -- skip adding the separator before the first alternative
                first = false
            else
                -- add the separator
                result:node(separator)
            end

            result:node(mw.html.create("span")
                :addClass("alternatives__alternative")
                :attr("title", possibleCondition[1])
                :wikitext(alternative)
            )
        end
    end

    for _, alternative in ipairs(args) do
        if first then
            -- skip adding the separator before the first alternative
            first = false
        else
            -- add the separator
            result:node(separator)
        end

        -- add the alternative after the separator
        result:node(mw.html.create("span")
            :addClass("alternatives__alternative")
            :wikitext(alternative)
        )
    end

    return result
end

return p