Lua error: expandTemplate: template "mono" does not exist. Lua error: expandTemplate: template "mono" does not exist. Lua error: expandTemplate: template "mono" does not exist. Lua error: expandTemplate: template "mono" does not exist. Lua error: expandTemplate: template "mono" does not exist. Lua error: expandTemplate: template "mono" does not exist. Lua error: expandTemplate: template "mono" does not exist.


local p = {}

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

-- https://en.wikibooks.org/wiki/Lua_Functional_Programming/Functions
function 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 result
end

function all(criteria, array)
	local result = true
	for _, value in pairs(array) do
		if not criteria(value)
			then
				result = false
				break
		end
	end
	return result
end
	
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_arg
end

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

	_, _, section = select_arg(frame, true, {1, "seção"}, nil)
	_, _, name = select_arg(frame, true, {2, "nome"}, nil)
	_, _, system = select_arg(frame, false, {3, "sistema"}, "7")
	_, _, style = select_arg(frame, false, {4, "estilo"}, "referência")
	return "* <b>" .. frame:expandTemplate{ title = "mono", args = {name .. "(" .. section .. ")"}} .. "</b>&nbsp;&ndash;&nbsp;" .. system .. " ''(" .. style .. ")''"
end

return p