มอดูล:Pagelist

local p = {}
local separators = {
	dot = true,
	pipe = true,
	comma = true,
	'tpt-languages' = true
}
local function getSeparator(sep)
	if type(sep) ~= 'string' then
		return nil
	end
	if separatorssep then
		return mw.message.new(sep .. '-separator'):plain()
	else
		return sep
	end
end
local function isRedir(pagename)
	-- this could throw error, since it is expensive
	return pagename.isRedirect
end
local function generateLink(page, nspace, delim, endDelim, text, redir, navDelim, navEndDelim, navPage)
	if not page then
		return nil
	end
	local pagename = mw.title.new(page)
	if not pagename then
		-- Default to the args we were passed if our page
		-- object was nil.
		pagename = page
	else
		-- not checking pagename.isRedirect will cause every non-redirect pages be listed
		-- as transcluded page, however pagename.isRedirect is counted as expensive call,
		-- while pagename.redirectTarget not (but cause page be listed as transcluded page)
		--if redir and pagename.redirectTarget then
		--if redir and pagename.isRedirect then
		if redir then
			local r, m = pcall(isRedir, pagename)
			if not r then
				m = pagename.redirectTarget
			end
			if m then
				page = pagename.redirectTarget.prefixedText
				pagename = pagename.redirectTarget.text
			else
				pagename = pagename.text
			end
		else
			pagename = pagename.text
		end
	end
	delim = delim or 
	endDelim = endDelim or delim
	nspace = nspace or 
	if nspace == 'all' then
		nspace = 
		pagename = page
	elseif nspace == 'wcat' then
		--nspace = 'wcat'
	elseif nspace ~=  then
		nspace = ':' .. nspace
	end
	if nspace == 'wcat' then
		if navPage and pagename == navPage then
			delim = navDelim or delim
			endDelim = navEndDelim or endDelim
		end
		endDelim = '(c)' .. endDelim
		nspace = 
	else
		if navPage and nspace .. ':' .. pagename == ':' .. navPage then
			delim = navDelim or delim
			endDelim = navEndDelim or endDelim
		end
	end
	local outStr = string.format( 
		'%s%s%s',
		delim, nspace, pagename, text or page, endDelim
	)
	return outStr
end
function p._main(args)
	local t = {}
	local separator = getSeparator(args.separator)
	local conjunction = getSeparator(args.conjunction)
	for i, v in ipairs(args) do
		table.insert(t, generateLink(
			v, args.nspace, args.delim, args.edelim
		))
	end
	return mw.text.listToText(t, separator, conjunction)
end
function p._nav(args)
	local t = {}
	local separator = getSeparator(args.separator)
	do local func, static, var = ipairs(args) while true do
		local i, v = func(static, var) var = i
		if var == nil then break end
		local i, v2 = func(static, var) var = i
		table.insert(t, generateLink(
			v, args.nspace, args.delim, args.edelim, v2~= and v2, true,
			args'nav delim', args'nav edelim', args'nav page'
		))
		if var == nil then break end
	end end
	return table.concat(t, separator)
end
function p.main(frame)
	local origArgs = require('Module:Arguments').getArgs(frame, {
		trim = false,
		removeBlanks = false,
		wrappers = 'Template:Pagelist'
	})
	-- Process integer args. Allow for explicit positional arguments that are
	-- specified out of order, e.g. 3=entry3.
	-- After processing, the args can be accessed accurately from ipairs.
	local args = {}
	for k, v in pairs(origArgs) do
		if type(k) == 'number' and 
			k >= 1 and
			math.floor(k) == k and
			string.match(v, '%S') then -- Remove blank or whitespace values.
			table.insert(args, k)
		end
	end
	table.sort(args)
	for i, v in ipairs(args) do
		argsi = origArgsv
		-- Trim whitespace.
		if type(argsi) == 'string' then
			argsi = mw.text.trim(argsi)
		end
	end
	-- Get old named args. We don't need to remove blank values
	-- as for the nspace and edelim parameters the behaviour is different
	-- depending on whether the parameters are blank or absent, and for
	-- the delim parameter the default should be the blank string anyway.
	args.delim = origArgs.delim
	args.edelim = origArgs.edelim
	args.nspace = origArgs.nspace
	-- Get new named args, "separator" and "conjunction", and strip blank values.
	if origArgs.separator and origArgs.separator ~=  then
		args.separator = origArgs.separator
	end
	if origArgs.conjunction and origArgs.conjunction ~=  then
		args.conjunction = origArgs.conjunction
	end
	return p._main(args)
end
function p.nav(frame)
	local origArgs = require('Module:Arguments').getArgs(frame, {
		trim = false,
		removeBlanks = false,
		wrappers = 'Template:Navlist'
	})
	local args = {}
	for i, v in ipairs(origArgs) do
		-- Trim whitespace.
		if type(v) == 'string' then
			argsi = mw.text.trim(v)
		else
			argsi = v
		end
	end
	-- Get old named args. We don't need to remove blank values
	-- as for the nspace and edelim parameters the behaviour is different
	-- depending on whether the parameters are blank or absent, and for
	-- the delim parameter the default should be the blank string anyway.
	args.delim = origArgs.delim
	args.edelim = origArgs.edelim
	args'nav delim' = origArgs'nav delim'
	args'nav edelim' = origArgs'nav edelim'
	args'nav page' = origArgs'nav page'
	args.nspace = origArgs.nspace
	-- Get new named args, "separator" and "conjunction", and strip blank values.
	if origArgs.separator and origArgs.separator ~=  then
		args.separator = origArgs.separator
	end
	--local function getNamedArgs(args, origArgs, s)
	--	if origArgss and origArgss ~=  then
	--		argss = origArgss
	--	end
	--end
	--getNamedArgs(args, origArgs, 'separator')
	--getNamedArgs(args, origArgs, 'nav delim')
	--getNamedArgs(args, origArgs, 'nav edelim')
	if not args'nav page' then
		--args'nav page' = frame:preprocess()
		args'nav page' = mw.title.getCurrentTitle().prefixedText
	end
	return p._nav(args)
end
return p