Module:ModuleData: Difference between revisions
Jump to navigation
Jump to search
(<@188013945699696640> <-that's a test) |
(No difference)
|
Revision as of 18:14, 24 October 2018
Documentation for this module may be created at Module:ModuleData/doc
local p = {}
local trim = mw.text.trim
local split = mw.text.split
local tableParser = mw.text.listToText
local data = {}
local translation = {}
local english = {}
local order = {}
local langCode = ""
local titlearg
function check(string)
return string and #tostring(string) > 0
end
function toTable(table)
local bck = {}
for k,v in pairs(table) do
bck[k] = v
end
return bck
end
function transform(inp, toType)
if type(inp) == toType then
return inp
end
if toType == "string" then
return tostring(inp)
end
if toType == "number" then
return tonumber(inp)
end
end
function translate(string)
if string == "pagename" then
string = titlearg or string
end
if string == "pagename" and data.translated then
return data.translated[langCode] or translation[string] or english[string] or string
end
return translation[string] or english[string] or string
end
function replace(string, fnd, repl)
local splits = split(string, fnd,true)
local bck = splits[1]
for i = 2, #splits do
bck = bck .. repl .. splits[i]
end
return bck
end
function special(value, rules)
-- Only run if a special tag is in the "order"
local i = 1
while rules[i] do
-- Loop through all the rules until a good one is found
-- (while cause it's a fake table)
cur = rules[i]
-- Use of "copy" cause the source needs to stay its type
local copy
if check(cur.transform) then
copy = transform(value,cur.transform)
else
copy = value
end
-- Transformation of fake table to real table so the count parameter can be used
if type(copy) == "table" then
copy = toTable(copy)
end
local ok = true -- This rule is good until there is a problem
local req = cur.require
if check(req) then
ok = ok and ( ( not check(req.count) ) or ( #copy == req.count) )
ok = ok and ( ( not check(req.type) ) or ( type(copy) == req.type) )
ok = ok and ( ( not check(req.regex) ) or not(mw.ustring.gmatch( copy, req.regex )() == nil) )
-- http://dev.wikia.com/wiki/Lua_reference_manual/Standard_libraries#Patterns
-- http://dev.wikia.com/wiki/Lua_reference_manual/Scribunto_libraries#Ustring_patterns
end
if ok then
local table = {}
if type(cur.result) == "string" then
return replace(cur.result,"$",transform(copy,"string"))
elseif type(cur.result) == "table" then
for k,v in pairs(cur.result.format) do
if type(v) == "string" then
table[k] = replace(v,"$",copy)
if table[k] == v then
table[k] = translate(v)
end
elseif type(v) == "number" then
table[k] = copy[v]
end
end
return tableParser(table,
translate(cur.result.separatorMid or "separatorMid"),
translate(cur.result.separatorEnd or "separatorEnd")
)
end
end
i = i + 1
end
return "Data was suplied in the wrong format."
end
function addData(value, info)
q = value or info.default
if not(check(q)) then
return ""
end
t = "<" .. info.tag .. ">"
if check(info.label) then
t = t .. "<label>" .. translate(info.label) .. "</label>"
end
t = t .. "<default>"
if check(info.special) then
t = t .. special(q, info.special)
elseif type(q) == "string" then
t = t .. translate(q)
elseif type(q) == "table" then
local tabl = {}
for k,v in pairs(q) do
table[k] = v
end
t = t .. tableParser(table,translate("separatorMid"), translate("separatorEnd"))
end
t = t .. "</default>"
return t .. "</" .. info.tag .. ">\n"
end
function loopTilNil(func,start)
return func(start) and (loopTilNil(func,func(start)) or start)
end
function p.ModuleInfo(frame,bol)
local page = ""
local mainPage
if(bol) then
page = "Sweethearts"
langCode = "cs"
else
mw.log("this is not supported in debuger mate")
mainPage = loopTilNil(
function(start)
return start:getParent()
end
,frame)
titlearg = frame.args["Title"]
page = frame.args["Title"] or mainPage:preprocess("{{Template:English Parent}}")
langCode = frame.args["Language Code"] or mainPage:preprocess("{{Template:Language Code}}")
end
if not pcall(function() data = mw.loadData("Module:ModuleData/" .. page) end) then
return "Couldn't find module:" .. page
end
if not pcall(function() order = mw.loadData("Module:ModuleData/Order") end) then
return "An error has ocured, please contact staff\nErrorcode:ModuleData#orderNotFound"
end
if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/" .. langCode) end) then
if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/en") end) then
return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
else
english = translation
end
else
if not pcall(function() english = mw.loadData("Module:ModuleData/Translations/en") end) then
return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
end
end
local t = "<div style=\"width:280px; float:right;\"><tabber>\n"
local i = 1
while data[i] do
if i > 1 then
t = t .. "\n|-|"
end
t = t .. data[i].version .. "=<infobox>\n"
local j = 1
while order[j] do
t = t .. addData(data[i].moduleInfo[order[j].source],order[j])
j = j + 1
end
t = t .. "</infobox>\n"
i = i + 1
end
if bol or frame.args.raw then
return t .. "\n</tabber></div>"
end
return frame:preprocess(t .. "\n</tabber></div>")
end
return p