Module:Sandbox/Usien6 Explained

local p =

p.man = function(frame) return p.ptwiki(frame)end

-- https://en.wikibooks.org/wiki/Lua_Functional_Programming/Functionsfunction where(criteria, array) local result = for key, value in pairs(array) do if criteria(key, value) then mw.log("where: criteria(" .. key .. ", " .. value .. ") -> True") table.insert(result, value) -- table.insert(result, key, value) end end return resultend

function all(criteria, array) local result = true for _, value in pairs(array) do if not criteria(value) then result = false break end end return resultend is_string = function(object) return type(object)

"string"end

select_arg = function(frame, mandatory, keys, default) local selected_key = "" local selected_keys local selected_keys_count local selected_arg = default local selected_any

-- assert(all(is_string, keys), "Erro interno")

mw.log("select_arg: Procurando parâmetros `" .. table.concat(keys, "`, ou `") .. "`")

selected_keys = where(function(keykey, key) return frame.args[key] ~= nil end, keys) selected_keys_count = table.getn(selected_keys) selected_any = (selected_keys_count > 0) mw.log("select_arg: Encontrados " .. selected_keys_count .. ": `" .. table.concat(selected_keys, "`, e `") .. "`") if selected_any then assert(selected_keys_count <= 1, "Conflito de parâmetros: `" .. table.concat(selected_keys, "`, e `") .. "`!") selected_key = table.remove(selected_keys, 1) selected_arg = frame.args[selected_key] assert(type(selected_arg)

"string", "Parâmetro `" .. selected_key .. "` não é do tipo 'string'!") else assert(not mandatory, "Falta parâmetro: `" .. table.concat(keys, "`, ou `") .. "`!") end assert(is_string(selected_arg), "Parâmetro `" .. selected_key .. "` não é do tipo 'string'!") return selected_any, selected_key, selected_argend

p.ptwiki = function(frame) local section = "" local name = "" local system = "" local style = "" mw.log("ptwiki: novo quadro

=")

_, _, section = select_arg(frame, true,, nil) _, _, name = select_arg(frame, true,, nil) _, _, system = select_arg(frame, false,, "7") _, _, style = select_arg(frame, false,, "referência") return "* " .. frame:expandTemplate .. "  -  " .. system .. " (" .. style .. ")"end

return p