模块:Navbox

来自WikiFur
跳转至: 导航搜索
-- 模块:Navbox -- 由 模块:Navbox/beta 测试通过后提升:完全复刻旧版 模板:Navbox 与 模板:Navbox subgroup 的 HTML 观感, -- 仅将硬编码的分组行改为 Lua 循环动态生成,突破行数限制(旧版 Navbox 15 行 / subgroup 13 行)。 -- 行数上限 100。 local p = {} local MAX_GROUPS = 100 local function trim(s) return mw.text.trim(s or '') end local function getArgs(frame) local parent = frame:getParent() return (parent and parent.args) or frame.args or {} end -- 返回实际用到的最大 groupN / listN 编号 local function getMaxGroup(args) local maxN = 0 for i = 1, MAX_GROUPS do if trim(args['group' .. i]) ~= '' or trim(args['list' .. i]) ~= '' then maxN = i end end return maxN end -- 旧版 navbar(查看·讨论·编辑),等价于 {{tnavbar|tname=...}} local function renderNavbar(frame, name) if trim(name) == '' then return '' end local ok, result = pcall(function() return frame:expandTemplate{ title = '模板:Tnavbar', args = { tname = name } } end) if ok then return result else return '' end end -- 主导航框:复刻旧版 模板:Navbox function p.navbox(frame) local args = getArgs(frame) local tbl = mw.html.create('table') tbl:attr('align', 'center') :addClass('toccolours') :addClass('collapsible') :addClass('autocollapse') :attr('style', 'text-align: left;') -- 标题行 local titleRow = tbl:tag('tr') :attr('style', 'background-color: #ccccff; text-align: center;') titleRow:tag('th') :attr('colspan', '2') :attr('style', 'background-color: #ccccff; width: 80em') :wikitext(renderNavbar(frame, trim(args.name))) :wikitext(' ') :wikitext(args.title or '') -- 分组行:旧版行为 —— 第 1 行恒渲染,第 2+ 行仅当 groupN 非空 local maxN = math.max(1, getMaxGroup(args)) for i = 1, maxN do local group = trim(args['group' .. i]) if i == 1 or group ~= '' then local row = tbl:tag('tr') if group ~= '' then row:tag('th') :attr('style', 'background-color: #ccccff;') :tag('div') :attr('style', 'font-size:90%') :wikitext(group) end row:tag('td') :tag('div') :attr('style', 'font-size:90%') :wikitext(args['list' .. i] or '') end end return tostring(tbl) end -- 子分组:复刻旧版 模板:Navbox subgroup function p.subgroup(frame) local args = getArgs(frame) local tbl = mw.html.create('table') tbl:addClass('navbox') :addClass('nowraplinks') :attr('style', 'background:transparent; font-size:100%; padding:0; border:0; margin:-3px; ' .. trim(args.bodystyle)) local maxN = getMaxGroup(args) for i = 1, maxN do local list = args['list' .. i] if trim(list) ~= '' then local group = trim(args['group' .. i]) local row = tbl:tag('tr') local tdStyle = 'text-align: left; ' .. trim(args.liststyle) .. (i % 2 == 1 and trim(args.oddstyle) or trim(args.evenstyle)) if group ~= '' then -- 旧版第 8 行组头有 width:25% 的历史遗留样式,保留以保持一致 local thStyle = 'background:#ddf;' .. trim(args.groupstyle) if i == 8 then thStyle = 'width:25%;' .. thStyle end row:tag('th'):attr('style', thStyle):wikitext(group) row:tag('td'):attr('style', tdStyle):attr('colspan', '1'):wikitext(list) else row:tag('td'):attr('style', tdStyle):attr('colspan', '2'):wikitext(list) end end end return tostring(tbl) end return p