ข้ามไปเนื้อหา

มอดูล:Pagelist

จากวิกิพีเดีย สารานุกรมเสรี
Documentation icon คู่มือการใช้งานมอดูล[ดู] [แก้] [ประวัติ] [ล้างแคช]

การใช้งาน

[แก้]
{{#invoke:Pagelist|function_name}}
{{#invoke:Pagelist|nav|separator=separator (wikitext)|nav delim=navigator delimiter|nav edelim=navigator end delimiter|page name 1|displayed text 1|page name 2|displayed text 2|page name 3|displayed text 3|...}}

ตัวอย่าง

[แก้]
  1. {{#invoke:pagelist|main|A|B|C|iOS}} --> A, B, C และ iOS
  2. {{#invoke:pagelist|nav|separator= - | nav delim=<<<|nav edelim=>>>|nspace=all| A|A1| B|| iOS|| iOS|iOS (ระบบปฏิบัติการ)| มอดูล:Pagelist|| C }} --> A1-B-ไอโอเอส-iOS (ระบบปฏิบัติการ)-<<<มอดูล:Pagelist>>>-C
  3. {{#invoke:pagelist|nav|separator= - | nav delim=<<<|nav edelim=>>>|nspace=wcat|nav page=Pagelist| A|A1| B|| iOS|| iOS|iOS (ระบบปฏิบัติการ)| มอดูล:Pagelist|| C }} --> A1(c)-B(c)-ไอโอเอส(c)-iOS (ระบบปฏิบัติการ)(c)-<<<มอดูล:Pagelist(c)>>>-C(c)

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 separators[sep] 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 = '([[:Category:' .. pagename .. '|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|%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. {{br separated entries|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
		args[i] = origArgs[v]
		-- Trim whitespace.
		if type(args[i]) == 'string' then
			args[i] = mw.text.trim(args[i])
		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
			args[i] = mw.text.trim(v)
		else
			args[i] = 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 origArgs[s] and origArgs[s] ~= '' then
	--		args[s] = origArgs[s]
	--	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('{{FULLPAGENAME}}')
		args['nav page'] = mw.title.getCurrentTitle().prefixedText
	end

	return p._nav(args)
end

return p