local p = {}
local getArgs = require("Module:Arguments").getArgs
-- 主函数
function p.main(frame)
local args = getArgs(frame)
local id = args.id or ""
local name = args.name or args[1] or ""
local hide = args.hide
local tag = args.tag or "h6"
-- 生成链接字符串
local links = ""
if args.from and args.to then
-- 数字范围模式
local fromNum = tonumber(args.from)
local toNum = tonumber(args.to)
local prefix = args.prefix or ""
local suffix = args.suffix or ""
local separator = args.separator or args.sep or " "
if fromNum and toNum then
for i = fromNum, toNum do
local linkText = prefix .. i .. suffix
local linkTarget = "#" .. prefix .. i .. suffix
if id ~= "" then
linkTarget = linkTarget .. "_" .. id
end
links = links .. "[[" .. linkTarget .. "|" .. linkText .. "]]"
if i < toNum then
links = links .. separator
end
end
end
else
-- 参数列表模式
local separator = args.separator or args.sep or " "
local index = 1
while args["item" .. index] do
local value = args["item" .. index]
local linkTarget = "#" .. value
if id ~= "" then
linkTarget = linkTarget .. "_" .. id
end
links = links .. "[[" .. linkTarget .. "|" .. value .. "]]"
if args["item" .. (index + 1)] then
links = links .. separator
end
index = index + 1
end
end
-- 生成唯一ID
local ntid = name
if id ~= "" then
ntid = ntid .. "_" .. id
end
-- 替换链接中的当前项
local pattern = "%[%[#" .. ntid .. "%|" .. name:gsub("([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1") .. "%]%]"
local replacement
if hide == "hide" then
replacement = '<span id="' .. ntid .. '">' .. name .. '</span>'
else
replacement = '<' .. tag .. ' class="navtitle" style="padding:0;font-weight:bold;' .. (args.activestyle or '') .. '">' .. name .. '</' .. tag .. '>'
end
local content = links:gsub(pattern, replacement)
-- 构建最终输出
local output = '<div style="overflow-wrap:break-word;font-weight:bold;border-bottom:1px solid #0074f9;' .. (args.style or '') .. '">'
output = output .. content
output = output .. '</div>'
-- 添加空段落解决独占一行问题
output = output .. '<p></p>'
return output
end
return p