跳转到内容

模組:TemplateParameters2

维基百科,自由的百科全书
local p = {}
local mYesno
local mTemplateParameters

local function yesno(value)
	if not mYesno then
		mYesno = require('Module:Yesno')
	end
	return mYesno(value)
end

local function assertGet(args, keys)
	for _, key in ipairs(keys) do
		local value = args[key]
		if value ~= nil then
			return value
		end
	end
	error('arguments "' .. keys[1] .. '" missing. Checked keys: ' .. table.concat(keys, ', '))
end

local function collectGroup(args, prefix, startIndex, i, numInGroup)
	local groupStartIndex = startIndex + i * numInGroup
	if args[prefix .. tostring(groupStartIndex)] == nil then
		return false
	end

	local result = {}
	for j = 0, numInGroup - 1 do
		local key = prefix .. tostring(groupStartIndex + j)
		table.insert(result, args[key] or '')
	end
	return result
end

function p.FormatingArguments2(frame)
	-- For calling from #invoke.
	local args
	if frame == mw.getCurrentFrame() then
		-- We're being called via #invoke. The args are passed through to the module
		-- from the template page, so use the args that were passed into the template.
		args = frame.args
	else
		-- We're being called from another module or from the debug console, so assume
		-- the args are passed in directly.
		if type(args) ~= type({}) then args = {frame} end
	end

	local formatTemplate = assertGet(args, {'format', 'Format', '格式'})
	local numInGroup = tonumber(args.count) or 1
	local startIndex = tonumber(args.start) or 1
	local prefix = args.prefix or ''

	if yesno(args.delnowiki) then
		formatTemplate = mw.text.unstripNoWiki(formatTemplate)
	end

	if yesno(args.delmsgnw) then
		formatTemplate = mw.text.decode(formatTemplate)
	end

	if yesno(args.usingConditionalExpressions) then
		if not mTemplateParameters then
			mTemplateParameters = require('Module:TemplateParameters')
		end
		formatTemplate = mTemplateParameters._get_escape(formatTemplate)
	end
	
	local parentFrame = mw.getCurrentFrame():getParent()
	local parentArgs
	if parentFrame then
		parentArgs = parentFrame.args
	else
		local exampleArgs = args.example
		if exampleArgs ~= nil and exampleArgs ~= '' then
			-- 示範模式
			parentArgs = p._unwrapArgsSpstr(exampleArgs)
		else
			-- 沒有東西好示範的,返回空白
			return ''
		end
	end

	-- 收集所有組別
	local groups = {}
	local i = 0
	while true do
		local group = collectGroup(parentArgs, prefix, startIndex, i, numInGroup)
		if not group then
			break
		end
		table.insert(groups, group)
		i = i + 1
	end
	
	local groupCount = #groups
	if not groupCount then
		-- 根本沒有組別,直接返回空白
		return ''
	end

	-- 實際展開
	local result = {}
	for j = 1, #groups do
		local group = groups[j]

		local value = formatTemplate
		value = mw.ustring.gsub(value, '{{{(%d+)}}}', group)
		value = mw.ustring.gsub(value, '{{{isFirst}}}', j == 1 and '1' or '')
		value = mw.ustring.gsub(value, '{{{isLast}}}', j == groupCount and '1' or '')
		value = mw.ustring.gsub(value, '{{{count}}}', tostring(groupCount))
		value = mw.ustring.gsub(value, '{{{argGroupID}}}', tostring(j))
		table.insert(result, value)
	end

	return mw.getCurrentFrame():preprocess(table.concat(result))
end

function p._unwrapArgsSpstr(value)
	-- see TemplateParameters#pass_spstr
	local args = {}
	for keyValuePair in mw.text.gsplit(value, "<參數分隔/>", true) do
		if value ~= "" then
			local kv = mw.text.split(value, "<參數值/>", true)
			if #kv >= 2 then
				args[kv[1]] = kv[2]
			end
		end
	end
	return args
end

return p