Модуль:Parameters
Материал из проекта «Повелитель Тайн Вики»
Дополнительные действия
local helpers = require("Module:Helpers")
local p = {}
local type_fn = "fn"
local type_inokver = "invoker"
local type_template = "template"
local mode_table = "table"
local mode_bullet = "bullet"
local DELIM = "@||;|;;"
local SUPER_DELIM = "@|||;|;|;;;"
function oneStringToTable(str)
if str ~= nil and #str > 0 then
local arg_set = helpers.split(str, DELIM)
local name = cleanString(arg_set[1])
local label = cleanString(arg_set[2])
local a_type = cleanString(arg_set[3]) or "Any"
local desc = cleanString(arg_set[4])
local required = cleanString(arg_set[5])
local default = cleanString(arg_set[6])
if name ~= nil then
return {
["id"] = name,
["label"] = label,
["type"] = a_type,
["desc"] = desc,
["required"] = helpers.strToBool(required),
["default"] = default
}
end
end
end
function paramStringToTable(frame)
local params = {}
for i = 1, 20 do
local p = frame.args[i]
local value = oneStringToTable(p)
if value ~= nil then
table.insert(params, value)
end
end
return params
end
function returnStringToTable(str)
if str == nil or #str == 0 then
return nil
end
local params = {}
for _, p in ipairs(helpers.split(str, SUPER_DELIM)) do
local value = oneStringToTable(p)
if value ~= nil then
table.insert(params,value)
end
end
return params
end
function cleanString(str)
if str == nil then return nil end
str = helpers.trimWhitespace(str)
if #str == 0 then return nil end
return str
end
function p.buildArgs(frame)
local str = table.concat({
frame.args[1] or "",
frame.args.label or "",
frame.args[2] or "",
frame.args[3] or "",
frame.args.required or "",
frame.args.default or ""
}, DELIM)
return str
end
function p.buildSuperArgs(frame)
local params = {}
for i = 1, 20 do
local p = frame.args[i]
if p ~= nil then
p = helpers.trimWhitespace(p)
if #p > 0 then
table.insert(params, p)
end
end
end
return table.concat(params, SUPER_DELIM)
end
function p.parameterInvokable(frame)
local a_type = frame.args["type"]
local a_mode = cleanString(frame.args["mode"]) or mode_table
local a_desc = cleanString(frame.args["desc"])
local faq_title = cleanString(frame.args["faq_title"])
local return_value = cleanString(frame.args["returns"])
local params = paramStringToTable(frame)
local returns = returnStringToTable(return_value)
local do_type
if a_type == type_fn then
do_type = "{{CodeFontN|Function|colour=#5FB4B4}} Parameters"
elseif a_type == type_inokver then
do_type = "{{CodeFontN|#invoke|colour=Script}} Parameters"
elseif a_type == type_template then
do_type = "{{CodeFontN|Template|colour=Param}} Parameters"
else
do_type = "Parameters"
end
local res_str = "<div>No Output.</div>"
if a_mode == mode_table then
local root = mw.html.create("table"):addClass("wikitable"):attr("style", "table-layout:auto;")
local header1 = root:tag("tr")
header1:tag("th"):attr("colspan", 5):wikitext(do_type)
if a_desc ~= nil and faq_title == nil then
local desk_row = root:tag("tr")
desk_row:tag("td"):attr("colspan", 5):wikitext(a_desc)
end
if #params > 0 then
local header2 = root:tag("tr")
header2:tag("th"):attr("colspan", 2):wikitext("Parameters")
header2:tag("th"):attr("rowspan", 2):wikitext("Type")
header2:tag("th"):attr("rowspan", 2):wikitext("Description")
header2:tag("th"):attr("rowspan", 2):wikitext("Status")
local header3 = root:tag("tr")
header3:tag("th"):wikitext("Id")
header3:tag("th"):wikitext("Label")
for _, p in ipairs(params) do
local p_row = root:tag("tr")
p_row:tag("th"):wikitext(p["label"] or p["id"])
p_row:tag("td"):wikitext(string.format("{{CodeFontN|%s}}", p["id"]))
p_row:tag("td"):wikitext(p["type"] or "Any")
p_row:tag("td"):attr("style", "width:100%"):wikitext(p["desc"] or "")
if p["default"] ~= nil then
p_row:tag("td"):wikitext(string.format("Default:\n<b>%s</b>", p["default"]))
else
p_row:tag("td"):wikitext(helpers.ternary(p["required"], "<b>required</b>", "optional"))
end
end
end
if returns ~= nil and #returns > 0 then
local header4 = root:tag("tr")
header4:tag("th"):attr("colspan", 5):wikitext("Return Value")
local header5 = root:tag("tr")
header5:tag("th"):attr("colspan", 2):wikitext("Label")
header5:tag("th"):wikitext("Type")
header5:tag("th"):attr("colspan", 2):wikitext("Description")
for _, r in ipairs(returns) do
local r_row = root:tag("tr")
r_row:tag("th"):attr("colspan", 2):wikitext(r["label"] or r["id"])
r_row:tag("td"):wikitext(r["type"] or "Any")
r_row:tag("td"):attr("colspan", 2):wikitext(r["desc"] or "")
end
end
res_str = tostring(root)
elseif a_mode == mode_bullet then
error("Unsupported mode: " .. mode_bullet)
else
error("Unkown mode: " .. a_mode)
end
if faq_title ~= nil then
local faq_id = faq_title:gsub("%s+", "_") .. "._faq"
res_str = string.format('{{FAQ|id = %s|%s|content = <div style="font-size: 14px">%s%s</div>}}', faq_id, faq_title or "Unnamed Function", a_desc or "", res_str)
end
return frame:preprocess(res_str)
end
return p