可在模块:LinkArray/doc创建此模块的帮助文档
local p = {}
local function stringToArray(str, sep)
local arr = {}
for v in mw.text.gsplit(str, '%s*' .. sep .. '%s*') do
if v ~= '' then
table.insert(arr, v)
end
end
if requireSort then
table.sort(arr)
end
return arr
end
local function tableToArray(tbl, requireSort)
local arr = {}
for k, v in pairs(tbl) do
if type(k) == 'number' and v ~= '' then
arr[k] = v
end
end
if requireSort then
table.sort(arr)
end
return arr
end
function p.main()
local args = require('Module:ProcessArgs').merge(true)
return p.linkArray(args)
end
function p.linkArray(args)
local f = mw.getCurrentFrame()
local prefix = args.prefix or ''
local suffix = args.suffix or ''
local namePrefix = args.nameprefix or ''
local nameSuffix = args.namesuffix or ''
local delimiter = args.delimiter
local finalDelimiter = args.finaldelimiter
local sep = args.sep or ','
local linkPrefix = args.directlink and '{{Direct link|' or '[['
local linkSuffix = args.directlink and '}}' or ']]'
local requireSort = args.sort
local firstOnly = args.firstonly
local outputPrefix = args.outputprefix or ''
local outputSuffix = args.outputsuffix or ''
local arr = {}
if args.mode and args.mode ~= 'single' then
arr = tableToArray(args, requireSort)
else
arr = stringToArray(args[1], sep, requireSort)
end
local tbl = {}
for k, v in ipairs(arr) do
if firstOnly and k ~= 1 then
table.insert(tbl, v)
else
table.insert(
tbl, f:preprocess(
linkPrefix .. prefix ..
mw.text.trim(v) .. suffix .. '|' ..
namePrefix .. mw.text.trim(v) ..
nameSuffix .. linkSuffix
)
)
end
end
local result
if args.pretty then
result = mw.text.listToText(tbl, delimiter, finalDelimiter)
else
result = table.concat(tbl, delimiter)
end
return outputPrefix .. result .. outputSuffix
end
return p