Module:Alternatives

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Alternatives [view] [edit] [history] [purge] (How does this work?)

This module implements {{alternatives}}.

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"},
    -- control schemes
    {"kbm", "On mouse and keyboard"},
    {"controller", "On controller"},
}

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[1]]
        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[2])
                :wikitext(alternative)
            )
        end
    end

    for i, 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
        local span = mw.html.create("span")
            :addClass("alternatives__alternative")
            :wikitext(alternative)

        -- set title attribute according to any corresponding "condition" attribute
        local condition = args["condition" .. i] or (i == 1 and args["condition"])
        if condition then
            span:attr("title", condition)
        end

        result:node(span)
    end

    return result
end

return p