模块:Array
来自The Land of StarLight
此模块的文档可以在模块:Array/doc创建
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