Final Fantasy Wiki
m (+)
(.code doesnt seem to exist any more, but <code> is probably better here anyway)
 
(23 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
-- <nowiki>
tl = {}
+
local tl = {}
  +
local getArgs = require("Dev:Arguments").getArgs
  +
local data = mw.loadData("Module:Tl/data")
   
function tl.new(frame)
+
function tl._new(title, parameters)
local string = require "Module:String"
+
title = title or ""
  +
parameters = parameters or {}
local array = require "Module:Array"
 
   
local subst = false
+
local invoke = false
local substdisplay = ""
+
local pref = ""
local invoke = false
+
local ns = "Template"
local invokedisplay = ""
+
local nsdisplay = ""
local ns = "Template"
+
local page = ""
local nsdisplay = ""
+
local pagedisplay =""
local page = ""
+
local pagefollow = ""
local pagedisplay =""
 
local pagefollow = ""
 
   
 
local prefixes = {
local prefix = { "|", "int|MediaWiki", "Final Fantasy Wiki|Project", "User|User", "Etymology|Etymology", "MediaWiki|MediaWiki", "Template|Template", "Project|Project" }
 
 
{"", ""},
local self = { "#if", "#ifeq", "#switch", "plural", "#ifexpr", "gender", "#iferror", "#ifexist", "ifsubst", "ifstring", "ifnum", "ifregistered", "ifuser", "ifgroup" }
 
  +
{"int", "MediaWiki"},
local selfpage = "Help:Selection Functions"
 
  +
{"Final Fantasy Wiki", "Project"},
local strf = { "lc", "uc", "lcfirst", "ucfirst", "urlencode", "#urldecode", "anchorencode", "#len", "#pos", "#rpos", "#sub", "#explode", "padleft", "padright", "#pad", "char", "#replace" }
 
  +
{"User", "User"},
local strfpage = "Help:String Functions"
 
local matf = { "#expr", "%", "round", "cap" }
+
{"Etymology", "Etymology"},
  +
{"MediaWiki", "MediaWiki"},
local matfpage = "Help:Mathematical Functions"
 
  +
{"Template", "Template"},
local magw = { "TOCright", "TOCleft", "TOClimit", "DISPLAYTITLE", "DEFAULTSORT", "PAGENAME", "NAMESPACE", "FULLPAGENAME", "SUBPAGENAME", "BASEPAGENAME", "FULLBASEPAGENAME", "ROOTPAGENAME", "FULLROOTPAGENAME", "TALKSPACE", "TALKPAGENAME", "CURRENTTIMESTAMP", "CURRENTDATETIME", "CURRENTYEAR", "CURRENTMONTH", "CURRENTMONTHNAME", "CURRENTMONTHABBREV", "CURRENTWEEK", "CURRENTDAY", "CURRENTDAY2", "CURRENTDOW", "CURRENTDAYNAME", "CURRENTTIME", "CURRENTHOUR", "CURRENTMINUTE", "CURRENTSECOND", "NUMBEROFCONTENT", "NUMBEROFARTICLES", "NUMBEROFPAGES", "NUMBEROFFILES", "NUMBEROFEDITS", "NUMBEROFUSERS", "NUMBEROFACTIVEUSERS", "NUMBEROFMODS", "NUMBEROFADMINS", "USERNAME", "SITENAME", "SERVER", "SERVERNAME" }
 
  +
{"Project", "Project"}
local magwpage = "Help:Magic Words"
 
 
}
local nopg = { "formatnum", "ns", "pagesincategory", "#invoke", "#time" }
 
   
 
if title:lower():sub(1, 6) == "subst:" then
temp = {}
 
  +
pref = pref .. title:sub(1, 6)
for k,v in pairs(frame.args) do temp[k] = v end
 
 
title = title:sub(7)
 
end
   
  +
if title:lower():sub(1, 8) == "#invoke:" then
a = array.shift(temp)
 
 
invoke = true
b = array.join(temp, "|")
 
  +
pref = pref .. title:sub(1, 8)
 
ns = "Module"
 
title = title:sub(9)
 
end
   
if string.sub(string.lower(a), 1, 6) == "subst:" then
+
if title:find(":") then
 
local spl = mw.text.split(title, ":")
subst = true
 
substdisplay = string.sub(a, 1, 6)
 
a = string.sub(a, 7)
 
end
 
   
 
nsdisplay = table.remove(spl, 1)
if string.sub(string.lower(a), 1, 8) == "#invoke:" then
 
invoke = true
 
invokedisplay = string.sub(a, 1, 8)
 
ns = "Module"
 
nsdisplay = ""
 
a = string.sub(a, 9)
 
end
 
   
  +
for _, prefix in ipairs(prefixes) do
if string.find(a, ":")~= nil then
 
  +
if nsdisplay:lower() == prefix[1]:lower() then
local spl = string.split(a, ":")
 
nsdisplay = array.shift(spl)
+
pagedisplay = table.concat(spl, ":")
for i=1, #prefix do
+
ns = prefix[2]
  +
if string.lower(nsdisplay) == string.split(string.lower(prefix[i]), "|")[1] then
 
ns = string.split(prefix[i], "|")[2]
+
if ns == "" then
if ns=="" then nsdisplay = ":" end
+
nsdisplay = ":"
pagedisplay = array.join(spl, ":")
+
end
  +
break
 
end
+
break
if i==#prefix then
+
end
pagedisplay = nsdisplay
+
end
  +
nsdisplay = ""
 
ns = "Template"
+
if pagedisplay == "" then
pagefollow = ":" .. array.join(spl, ":")
+
pagedisplay = nsdisplay
end
+
nsdisplay = ""
  +
ns = "Template"
  +
pagefollow = ":" .. table.concat(spl, ":")
 
end
 
end
 
end
end
 
   
if pagedisplay == "" then pagedisplay = a end
+
if pagedisplay == "" then
 
pagedisplay = title
 
end
   
page = pagedisplay
+
page = pagedisplay
   
if nsdisplay == "" and not invoke then
+
if nsdisplay == "" and not invoke then
if array.indexof(self, pagedisplay) ~= -1 then page = string.split(selfpage, ":")[2] .. "#" .. pagedisplay; ns = string.split(selfpage, ":")[1]
+
local ucfirst = pagedisplay:sub(1, 1):upper() .. pagedisplay:sub(2)
  +
elseif array.indexof(strf, pagedisplay) ~= -1 then page = string.split(strfpage, ":")[2] .. "#" .. pagedisplay; ns = string.split(strfpage, ":")[1]
 
  +
for target, list in pairs(data) do
elseif array.indexof(matf, pagedisplay) ~= -1 then page = string.split(matfpage, ":")[2] .. "#" .. pagedisplay; ns = string.split(matfpage, ":")[1]
 
  +
if list[ucfirst] then
elseif array.indexof(magw, pagedisplay) ~= -1 then page = string.split(magwpage, ":")[2] .. "#" .. pagedisplay; ns = string.split(magwpage, ":")[1]
 
  +
if target == "" then
elseif array.indexof(nopg, pagedisplay) ~= -1 then page = ""
 
  +
page = ""
else
 
page = pagedisplay
+
else
  +
local split = mw.text.split(target, ":")
  +
  +
page = split[2] .. "#" .. pagedisplay
  +
ns = split[1]
  +
end
  +
  +
break
  +
end
  +
end
 
end
 
end
end
 
   
local pref = (subst and substdisplay or "") .. (invoke and invokedisplay or "")
+
local link = page ~= "" and (ns .. (ns == "" and "" or ":") .. page) or ""
local link = page~="" and (ns .. (ns=="" and "" or ":") .. page) or ""
+
local text = nsdisplay .. ((nsdisplay == "" or nsdisplay == ":") and "" or ":") .. pagedisplay
local text = nsdisplay .. ((nsdisplay=="" or nsdisplay==":") and"" or ":") .. pagedisplay
 
local suff = pagefollow
 
   
local out = "{{" .. pref .. (link~="" and ("[[" .. link .. "|" .. text .. "]]") or text) .. suff .. (b~="" and "|" .. b or "") .. "}}"
+
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)
return '<span class="code">' .. out .. '</span>'
 
 
end
 
end
   
 
return tl
 
return tl
  +
-- </nowiki>

Latest revision as of 01:01, 9 July 2018

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>