模块:Hatnote
来自星露谷物语扩展百科
更多操作
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- 辅助函数:检查字符串是否为维基链接
local function checkLink(s)
return s:find('[[', 1, true) and s:find(']]', 1, true)
end
-- 辅助函数:获取页面类型
local function getPageType(title, hasSection)
if hasSection then
return '章节'
end
local nsText = title.nsText
if nsText == '模板' then
return '模板'
elseif nsText == '模块' then
return '模块'
else
return '条目'
end
end
-- 辅助函数:格式化链接
local function formatLink(text)
if isWikilink(text) then
return "“'''" .. text .. "'''”"
else
return "“'''[[" .. text .. "]]'''”"
end
end
-- 检查是否为连接词
local function isConnector(text)
return text == '和' or text == '或' or text == '、'
end
-- 获取最大数字索引
local function getMaxIndex(args)
local maxIndex = 0
for key, _ in pairs(args) do
local index = tonumber(key)
if index and index > 0 and math.floor(index) == index then
if index > maxIndex then
maxIndex = index
end
end
end
return maxIndex
end
-- 处理描述和链接序列
local function processDescriptionLinks(args, startIndex, maxIndex, pagename)
local parts = {}
local state = '' -- 状态: '' | 'link' | 'end'
for i = startIndex, maxIndex do
local value = args[i]
if value then
if state == 'link' then
table.insert(parts, formatLink(value))
state = 'end'
elseif isConnector(value) then
table.insert(parts, value)
else
if state == 'end' then
table.insert(parts, ';')
end
local description = (value ~= '') and value or '其他用法'
table.insert(parts, '关于' .. description .. ',请见')
state = 'link'
end
end
end
-- fallback
if state == 'link' then
table.insert(parts, formatLink(pagename .. '(消歧义)'))
end
return parts, state
end
-- For
function p.about(frame)
local args = getArgs(frame, {
trim = true,
removeBlanks = true,
parentOnly = true
})
local maxIndex = getMaxIndex(args)
local title = mw.title.getCurrentTitle()
local pagename = title.text
local contentParts = {
'[[File:Disambig gray.svg|25px|link=]] '
}
if args[1] then
local pageType = getPageType(title, args.section)
table.insert(contentParts, '本' .. pageType .. '介绍的是' .. args[1])
end
local subsequentParts = processDescriptionLinks(args, 2, maxIndex, pagename)
if maxIndex < 2 and args[1] then
table.insert(subsequentParts, '关于其他用法,请见' .. formatLink(pagename .. '(消歧义)'))
end
local finalParts = { table.concat(contentParts, '') }
if #subsequentParts > 0 then
if args[1] then
table.insert(finalParts, '。 ')
end
table.insert(finalParts, table.concat(subsequentParts, ' '))
table.insert(finalParts, '。')
elseif args[1] then
table.insert(finalParts, '。')
end
local finalContent = table.concat(finalParts, '')
return frame:expandTemplate{ title = 'hatnote', args = { finalContent } }
end
-- For
function p.for2(frame)
local args = getArgs(frame, {
trim = true,
removeBlanks = true,
parentOnly = true
})
local maxIndex = getMaxIndex(args)
local title = mw.title.getCurrentTitle()
local pagename = title.text
local contentParts = {
'[[File:Disambig gray.svg|25px|link=]] '
}
if args.R then
table.insert(contentParts, args.R)
end
local subsequentParts = processDescriptionLinks(args, 1, maxIndex, pagename)
if maxIndex < 1 then
table.insert(subsequentParts, '关于其他用法,请见')
table.insert(subsequentParts, formatLink(pagename .. '(消歧义)'))
end
local finalParts = { table.concat(contentParts, '') }
if #subsequentParts > 0 then
if args.R then
table.insert(finalParts, ' ')
end
table.insert(finalParts, table.concat(subsequentParts, ' '))
table.insert(finalParts, '。')
end
local finalContent = table.concat(finalParts, '')
return frame:expandTemplate{ title = 'hatnote', args = { finalContent } }
end
p['for'] = p.for2
return p