Minecraft Wiki

除另有声明,转载时均必须注明出处若簡繁轉換出錯,請以遊戲內為準请勇于扩充与修正内容有兴趣逛逛我们的微博沟通交流,欢迎到社区专页需要协助,请在告示板留言

了解更多

Minecraft Wiki
Advertisement

此模块可将一个Lua table或一个字符串按给定的分隔符输出语言自然的列表。

local p = {}
local f = mw.getCurrentFrame()

local function stringToArray(str, separator, wrapTag, keepBlank)
	local arr = {}
	local i = 1
	for v in mw.text.gsplit(str, separator) do
		if v ~= '' or keepBlank then
			if wrapTag ~= '' then
				arr[i] = f:extensionTag(wrapTag, v)
			else
				arr[i] = v
			end
		end
		i = i + 1
	end
	return arr
end

local function tableToArray(tbl, wrapTag, keepBlank)
	local arr = {}
	for k, v in pairs(tbl) do
		if type(k) == 'number' and (v ~= '' or keepBlank) then
			if wrapTag ~= '' then
				arr[k] = f:extensionTag(wrapTag, v)
			else
				arr[k] = v
			end
		end
	end
	table.sort(arr)
	return arr
end

function p.main()
	local args = require('Module:ProcessArgs').merge(true)
	return p.array(args)
end
	
function p.array(args)
	local wrapTag = args.wrap or ''
	local delimiter = args.delimiter or args.d or ''
	local keepBlank = args.keepblank
	local arr
	if args.mode == 'single' then
		arr = stringToArray(args[1], args.sep or ',', wrapTag, keepBlank)
	else
		arr = tableToArray(args, wrapTag, keepBlank)
	end
	
	if delimiter == 'pretty' then
		return mw.text.listToText(arr)
	elseif delimiter == 'newline' or delimiter == 'br' then
		return table.concat(arr, '<br>')
	elseif delimiter == 'list' or delimiter == 'list-newline'
		or delimiter == 'bullet' or delimiter == '*' then
			local listLevel = args['list-level'] or '**'
			return table.concat(arr, '\n' .. listLevel)
	else
		return table.concat(arr, delimiter)
	end
end

return p
Advertisement