Final Fantasy Wiki
Register
Advertisement

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

-- <nowiki>
local tl = {}
local getArgs = require("Dev:Arguments").getArgs
local data = mw.loadData("Module:Tl/data")

function tl._new(title, parameters)
    title = title or ""
    parameters = parameters or {}

    local invoke = false
    local pref = ""
    local ns = "Template"
    local nsdisplay = ""
    local page = ""
    local pagedisplay =""
    local pagefollow = ""

    local prefixes = {
        {"", ""},
        {"int", "MediaWiki"},
        {"Final Fantasy Wiki", "Project"},
        {"User", "User"},
        {"Etymology", "Etymology"},
        {"MediaWiki", "MediaWiki"},
        {"Template", "Template"},
        {"Project", "Project"}
    }

    if title:lower():sub(1, 6) == "subst:" then
        pref = pref .. title:sub(1, 6)
        title = title:sub(7)
    end

    if title:lower():sub(1, 8) == "#invoke:" then
        invoke = true
        pref = pref .. title:sub(1, 8)
        ns = "Module"
        title = title:sub(9)
    end

    if title:find(":") then
        local spl = mw.text.split(title, ":")

        nsdisplay = table.remove(spl, 1)

        for _, prefix in ipairs(prefixes) do
            if nsdisplay:lower() == prefix[1]:lower() then
                pagedisplay = table.concat(spl, ":")
                ns = prefix[2]

                if ns == "" then
                    nsdisplay = ":"
                end

                break
            end
        end

        if pagedisplay == "" then
            pagedisplay = nsdisplay
            nsdisplay = ""
            ns = "Template"
            pagefollow = ":" .. table.concat(spl, ":")
        end
    end

    if pagedisplay == "" then
        pagedisplay = title
    end

    page = pagedisplay

    if nsdisplay == "" and not invoke then
        local ucfirst = pagedisplay:sub(1, 1):upper() .. pagedisplay:sub(2)

        for target, list in pairs(data) do
            if list[ucfirst] then
                if target == "" then
                    page = ""
                else
                    local split = mw.text.split(target, ":")

                    page = split[2] .. "#" .. pagedisplay
                    ns = split[1]
                end

                break
            end
        end
    end

    local link = page ~= "" and (ns .. (ns == "" and "" or ":") .. page) or ""
    local text = nsdisplay .. ((nsdisplay == "" or nsdisplay == ":") and "" or ":") .. pagedisplay

    local out = pref .. (link ~= "" and ("[[" .. link .. "|" .. text .. "]]") or text) .. pagefollow

    for _, param in ipairs(parameters) do
        out = out .. "|" .. param
    end

    return "<code>{{" .. out .. "}}</code>"
end

function tl.new(frame)
    local args = getArgs(frame)
    local title = args[1]
    local skipFirst = true
    local parameters = {}

    -- handle {{tl|=}}
    if (frame:getParent().args[""] or frame.args[""]) == "" then
        title = "="
        skipFirst = false
    end

    for i, param in ipairs(args) do
        if i > (skipFirst and 1 or 0) then
            table.insert(parameters, param)
        end
    end

    return tl._new(title, parameters)
end

return tl
-- </nowiki>
Advertisement