模块:Hatnote:修订间差异
来自星露谷物语扩展百科
更多操作
删除的内容 添加的内容
创建页面 |
无编辑摘要 |
||
| (未显示同一用户的1个中间版本) | |||
| 第2行: | 第2行: | ||
local p = {} |
local p = {} |
||
-- 获取页面类型 |
|||
-- 辅助函数:检查字符串是否为维基链接 |
|||
local function |
local function getPageType(title, hasSection) |
||
if hasSection then return '章节' end |
|||
return s:find('[[', 1, true) and s:find(']]', 1, true) |
|||
end |
|||
local nsText = title.nsText |
|||
-- 辅助函数:获取页面类型 |
|||
if nsText == '模板' then |
|||
local function getPageType(title, hasSection) |
|||
return '模板' |
|||
if hasSection then |
|||
elseif nsText == '模块' then |
|||
return '章节' |
|||
return '模块' |
|||
end |
|||
else |
|||
return '条目' |
|||
local nsText = title.nsText |
|||
end |
|||
if nsText == '模板' then |
|||
return '模板' |
|||
elseif nsText == '模块' then |
|||
return '模块' |
|||
else |
|||
return '条目' |
|||
end |
|||
end |
end |
||
-- |
-- 格式化链接 |
||
local function formatLink(text) |
local function formatLink(text) |
||
if text:find('[[', 1, true) and text:find(']]', 1, true) then |
|||
return "“'''" .. text .. "'''”" |
|||
else |
|||
return "“'''[[" .. text .. "]]'''”" |
|||
end |
|||
end |
end |
||
-- 检查是否为连接词 |
-- 检查是否为连接词 |
||
local function isConnector(text) |
local function isConnector(text) |
||
return text == '和' or text == '或' or text == '、' |
|||
end |
end |
||
-- 获取最大数字索引 |
-- 获取最大数字索引 |
||
local function getMaxIndex(args) |
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 |
|||
maxIndex = index |
|||
end |
|||
return maxIndex |
|||
end |
|||
end |
|||
return maxIndex |
|||
end |
end |
||
-- 处理描述和链接序列 |
-- 处理描述和链接序列 |
||
local function processDescriptionLinks(args, startIndex, maxIndex, pagename) |
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, ';') |
|||
table.insert(parts, '关于' .. description .. ',请见') |
|||
end |
|||
state = 'link' |
|||
local description = (value ~= '') and value or '其他用法' |
|||
end |
|||
table.insert(parts, '关于' .. description .. ',请见') |
|||
end |
|||
state = 'link' |
|||
end |
|||
end |
|||
-- fallback |
|||
end |
|||
if state == 'link' then |
|||
table.insert(parts, formatLink(pagename .. '(消歧义)')) |
|||
-- fallback |
|||
end |
|||
if state == 'link' then |
|||
table.insert(parts, formatLink(pagename .. '(消歧义)')) |
|||
return parts, state |
|||
end |
|||
return parts, state |
|||
end |
end |
||
-- For |
-- For |
||
function p.about(frame) |
function p.about(frame) |
||
local args = getArgs(frame, |
|||
{trim = true, removeBlanks = false, parentOnly = true}) |
|||
trim = true, |
|||
removeBlanks = true, |
|||
local maxIndex = getMaxIndex(args) |
|||
parentOnly = true |
|||
local title = mw.title.getCurrentTitle() |
|||
}) |
|||
local pagename = title.text |
|||
local maxIndex = getMaxIndex(args) |
|||
local contentParts = {'[[File:Disambig gray.svg|25px|link=]] '} |
|||
local title = mw.title.getCurrentTitle() |
|||
local pagename = title.text |
|||
if args[1] then |
|||
local pageType = getPageType(title, args.section) |
|||
local contentParts = { |
|||
table.insert(contentParts, |
|||
'[[File:Disambig gray.svg|25px|link=]] ' |
|||
'本' .. pageType .. '介绍的是' .. args[1]) |
|||
} |
|||
end |
|||
if args[1] then |
|||
local subsequentParts = processDescriptionLinks(args, 2, maxIndex, pagename) |
|||
local pageType = getPageType(title, args.section) |
|||
table.insert(contentParts, '本' .. pageType .. '介绍的是' .. args[1]) |
|||
if maxIndex < 2 and args[1] then |
|||
end |
|||
table.insert(subsequentParts, '关于其他用法,请见' .. |
|||
formatLink(pagename .. '(消歧义)')) |
|||
local subsequentParts = processDescriptionLinks(args, 2, maxIndex, pagename) |
|||
end |
|||
if maxIndex < 2 and args[1] then |
|||
local finalParts = {table.concat(contentParts, '')} |
|||
table.insert(subsequentParts, '关于其他用法,请见' .. formatLink(pagename .. '(消歧义)')) |
|||
end |
|||
if #subsequentParts > 0 then |
|||
if args[1] then table.insert(finalParts, '。 ') end |
|||
table.insert(finalParts, table.concat(subsequentParts, ' ')) |
|||
table.insert(finalParts, '。') |
|||
if #subsequentParts > 0 then |
|||
elseif args[1] then |
|||
table.insert(finalParts, '。') |
|||
end |
|||
table.insert(finalParts, table.concat(subsequentParts, ' ')) |
|||
local finalContent = table.concat(finalParts, '') |
|||
return frame:expandTemplate{title = 'hatnote', args = {finalContent}} |
|||
elseif args[1] then |
|||
table.insert(finalParts, '。') |
|||
end |
|||
local finalContent = table.concat(finalParts, '') |
|||
return frame:expandTemplate{ title = 'hatnote', args = { finalContent } } |
|||
end |
end |
||
-- For |
-- For |
||
function p.for2(frame) |
function p.for2(frame) |
||
local args = getArgs(frame, |
|||
{trim = true, removeBlanks = false, parentOnly = true}) |
|||
trim = true, |
|||
removeBlanks = true, |
|||
local maxIndex = getMaxIndex(args) |
|||
parentOnly = true |
|||
local title = mw.title.getCurrentTitle() |
|||
}) |
|||
local pagename = title.text |
|||
local maxIndex = getMaxIndex(args) |
|||
local contentParts = {'[[File:Disambig gray.svg|25px|link=]] '} |
|||
local title = mw.title.getCurrentTitle() |
|||
local pagename = title.text |
|||
if args.R then table.insert(contentParts, args.R) end |
|||
local contentParts = { |
|||
local subsequentParts = processDescriptionLinks(args, 1, maxIndex, pagename) |
|||
'[[File:Disambig gray.svg|25px|link=]] ' |
|||
} |
|||
if maxIndex < 1 then |
|||
table.insert(subsequentParts, '关于其他用法,请见') |
|||
if args.R then |
|||
table.insert(subsequentParts, formatLink(pagename .. '(消歧义)')) |
|||
end |
|||
local finalParts = {table.concat(contentParts, '')} |
|||
local subsequentParts = processDescriptionLinks(args, 1, maxIndex, pagename) |
|||
if #subsequentParts > 0 then |
|||
if args.R then table.insert(finalParts, ' ') end |
|||
table.insert(finalParts, table.concat(subsequentParts, ' ')) |
|||
table.insert(finalParts, '。') |
|||
end |
|||
end |
|||
local finalParts = { table.concat(contentParts, '') } |
|||
local finalContent = table.concat(finalParts, '') |
|||
return frame:expandTemplate{title = 'hatnote', args = {finalContent}} |
|||
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 |
end |
||
2025年10月14日 (二) 19:44的最新版本
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- 获取页面类型
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 text:find('[[', 1, true) and text:find(']]', 1, true) 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 = false, 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 = false, 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