m (+) |
DarthKitty (talk | contribs) (.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. |
+ | function tl._new(title, parameters) |
− | + | title = title or "" |
|
+ | parameters = parameters or {} |
||
− | local array = require "Module:Array" |
||
− | local |
+ | local invoke = false |
− | local |
+ | local pref = "" |
− | local |
+ | local ns = "Template" |
− | local |
+ | local nsdisplay = "" |
− | local |
+ | local page = "" |
− | local |
+ | local pagedisplay ="" |
− | local |
+ | local pagefollow = "" |
⚫ | |||
⚫ | |||
⚫ | |||
− | 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" |
||
− | + | {"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" } |
||
⚫ | |||
− | temp = {} |
||
+ | pref = pref .. title:sub(1, 6) |
||
− | for k,v in pairs(frame.args) do temp[k] = v end |
||
⚫ | |||
⚫ | |||
+ | if title:lower():sub(1, 8) == "#invoke:" then |
||
− | a = array.shift(temp) |
||
⚫ | |||
− | b = array.join(temp, "|") |
||
+ | pref = pref .. title:sub(1, 8) |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | if |
+ | if title:find(":") then |
⚫ | |||
− | subst = true |
||
− | substdisplay = string.sub(a, 1, 6) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | invokedisplay = string.sub(a, 1, 8) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | for _, prefix in ipairs(prefixes) do |
||
− | if string.find(a, ":")~= nil then |
||
+ | if nsdisplay:lower() == prefix[1]:lower() then |
||
⚫ | |||
− | + | pagedisplay = table.concat(spl, ":") |
|
− | + | ns = prefix[2] |
|
+ | |||
− | if string.lower(nsdisplay) == string.split(string.lower(prefix[i]), "|")[1] then |
||
− | ns = |
+ | 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 |
+ | if pagedisplay == "" then |
⚫ | |||
⚫ | |||
− | page = pagedisplay |
+ | page = pagedisplay |
− | if nsdisplay == "" and not invoke then |
+ | if nsdisplay == "" and not invoke then |
− | + | 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 |
|
+ | local split = mw.text.split(target, ":") |
||
+ | |||
+ | page = split[2] .. "#" .. pagedisplay |
||
+ | ns = split[1] |
||
+ | end |
||
+ | |||
+ | break |
||
+ | end |
||
+ | end |
||
end |
end |
||
⚫ | |||
− | local |
+ | local link = page ~= "" and (ns .. (ns == "" and "" or ":") .. page) or "" |
− | local |
+ | local text = nsdisplay .. ((nsdisplay == "" or nsdisplay == ":") and "" or ":") .. pagedisplay |
− | local text = nsdisplay .. ((nsdisplay=="" or nsdisplay==":") and"" or ":") .. pagedisplay |
||
− | local suff = pagefollow |
||
− | local out = |
+ | local out = pref .. (link ~= "" and ("[[" .. link .. "|" .. text .. "]]") or text) .. pagefollow |
+ | |||
+ | for _, param in ipairs(parameters) do |
||
+ | out = out .. "|" .. param |
||
+ | end |
||
+ | |||
⚫ | |||
+ | end |
||
+ | |||
+ | function tl.new(frame) |
||
+ | local args = getArgs(frame) |
||
+ | local title = args[1] |
||
⚫ | |||
+ | local parameters = {} |
||
+ | |||
+ | -- handle {{tl|=}} |
||
+ | if (frame:getParent().args[""] or frame.args[""]) == "" then |
||
⚫ | |||
+ | 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 |
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>