Module:Util
供其他模块使用的工具方法。
util.addCategory(str, category, sortkey)(function)- 向指定字符串(以表形式表示)中添加分类。 仅在当前页面位于主名字空间时生效。
- Parameters:
util.parseCommaList(list)(function)- 将顿号分隔列表解析为独立项目。
- Parameter:
list以顿号分隔的项目列表 (string) - Returns: 解析后的项目数组(已去除首尾空白) (table)
util.formatBulletList(lines)(function)- 将若干行格式化为项目符号列表或单个项目。
- Parameter:
lines不含项目符号的各行内容 (table) - Returns: 添加项目符号后的文本 (string)
--- 供其他模块使用的工具方法。
-- @module util
-- @author KockaAdmiralac
require('strict')
local util = {}
-- 模块变量。
local title = mw.title.getCurrentTitle()
--- 向指定字符串(以表形式表示)中添加分类。
-- 仅在当前页面位于主名字空间时生效。
--
-- @function util.addCategory
-- @param {table} str 要插入分类的字符串表
-- @param {string} category 要添加的分类名称
-- @param {string|nil} sortkey 该分类使用的排序键
function util.addCategory(str, category, sortkey)
if sortkey == nil and title.text == category then
sortkey = '*'
end
if title.namespace == 0 and title.text ~= '物品' then
table.insert(str, '[[Category:')
table.insert(str, category)
if sortkey ~= nil and sortkey ~= '' then
table.insert(str, '|')
table.insert(str, sortkey)
end
table.insert(str, ']]')
end
end
--- 将顿号分隔列表解析为独立项目。
--
-- @function util.parseCommaList
-- @param {string} list 以顿号分隔的项目列表
-- @returns {table} 解析后的项目数组(已去除首尾空白)
function util.parseCommaList(list)
local parsedList = {}
for _, item in ipairs(mw.text.split(list, '、', true)) do
table.insert(parsedList, mw.text.trim(item))
end
return parsedList
end
--- 将若干行格式化为项目符号列表或单个项目。
--
-- @function util.formatBulletList
-- @param {table} lines 不含项目符号的各行内容
-- @returns {string} 添加项目符号后的文本
function util.formatBulletList(lines)
if #lines <= 1 then
return table.concat(lines)
end
return '* ' .. table.concat(lines, '\n* ')
end
return util