Module:Alternatives

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
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 condition, alternative in pairs(args) do
        -- ignore number keys, those are handled later
        if type(condition) == "string" then
            local description = conditionDescriptions[condition]
            if not description then
                result:node(mw.html.create("span")
                    :addClass("error")
                    :wikitext(("Error: Unknown condition \"%s\""):format(condition))
                )

            else
                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", description)
                    :wikitext(alternative)
                )
            end
        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