Documentation for this module may be created at Module:PortalList/doc

local p = {}

function p.render(frame)
	local args = frame.args
	local moduleName = args.module
	local key = args.key
	local showcount = args.showcount == "yes"
	local limit = tonumber(args.limit)
	local validate = args.validate == "yes"

	if not moduleName or not key then
		return "<!-- PortalList error: module and key required -->"
	end

	local ok, data = pcall(require, "Module:" .. moduleName)
	if not ok or type(data) ~= "table" then
		return "<!-- PortalList error: module not found -->"
	end

	local list = data[key]
	if type(list) ~= "table" then
		return "<!-- PortalList error: key not found -->"
	end

	local out = {}
	local count = 0

	if showcount then
		table.insert(out, "<!-- PortalList count: " .. tostring(#list) .. " -->")
	end

	for _, item in ipairs(list) do
		if item.page then
			count = count + 1
			if limit and count > limit then break end

			local title = mw.title.new(item.page)
			local exists = true

			if validate and title then
				exists = title.exists
			end

			local line = "* [[" .. item.page .. "|" .. (item.label or item.page) .. "]]"

			if item.badge then
				line = line .. " <span class=\"ns-badge ns-badge--" ..
					mw.text.encode(item.badge:lower()) .. "\">" ..
					mw.text.encode(item.badge) .. "</span>"
			end

			if item.excerpt then
				line = line .. " — " .. mw.text.encode(item.excerpt)
			end

			if validate and not exists then
				line = line .. " <span class=\"ns-portal-missing\" title=\"Page does not exist\">⚠︎</span>"
			end

			table.insert(out, line)
		end
	end

	return table.concat(out, "\n")
end

return p