-- 模块: llsifascardshow
-- 将原先模板 {{llsifascardshow}} 的功能迁移到 Lua。
-- 用法示例:
-- {{#invoke:llsifascardshow|main
-- | type=类型说明
-- | ur=名称1、名称2
-- | ur-id=10001、10002
-- | ur-cl=分类1、分类2
-- | sr=名称A、名称B
-- | sr-id=20001、20002
-- | sr-cl=分类A、分类B
-- | path_ur1_a=自定义文件名1_a.png
-- | path_ur1_b=自定义文件名1_b.png
-- | path_sr2_a=自定义文件名2_a.png
-- | path_sr2_b=自定义文件名2_b.png
-- }}
-- 说明:
-- 1. “、” 与 “,” 都视为分隔符;允许首尾及多余空格。
-- 2. 若未提供自定义 path_ 前缀的文件名,则按默认:LLAS_Card_<id>_a.png / _b.png。
-- 3. 仍保留对模板 {{Hover}}、{{#img:}}、{{filepath:}}、{{llsifasitem}} 的转调用(不在此模块内重写)。
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- 分割字符串为数组;空或 nil 返回空表
local function splitParam(raw)
if not raw then return {} end
raw = mw.text.trim(raw)
if raw == '' then return {} end
-- 统一分隔符:把 '、' 换成 ','
raw = raw:gsub('、', ',')
local result = {}
for part in mw.text.gsplit(raw, ',', true) do
part = mw.text.trim(part)
if part ~= '' then
table.insert(result, part)
end
end
return result
end
-- 构建 Hover 组件 wikitext
local function buildHover(frame, kind, idx, id, args)
if not id or id == '' then
return ''
end
-- 允许自定义路径:path_ur1_a / path_ur1_b / path_sr2_a ...
local aKey = mw.ustring.format('path_%s%d_a', kind, idx)
local bKey = mw.ustring.format('path_%s%d_b', kind, idx)
local fileA = args[aKey] or mw.ustring.format('LLAS_Card_%s_a.png', id)
local fileB = args[bKey] or mw.ustring.format('LLAS_Card_%s_b.png', id)
-- 先获取文件真实路径(filepath 解析器函数),再传给 #img
local fileAPath = frame:callParserFunction('filepath', fileA)
local fileBPath = frame:callParserFunction('filepath', fileB)
local before = frame:callParserFunction('#img', { fileAPath, style = 'width:300px;' })
local after = frame:callParserFunction('#img', { fileBPath, style = 'width:300px;' })
-- Hover 模板
return frame:expandTemplate{ title = 'Hover', args = {
width = '300px',
height = '150px',
before = before,
after = after
}}
end
-- 在指定行上追加表头单元格并返回该行 tag
local function buildHeaderCells(frame, rowTag, kind, names, cls)
for i, name in ipairs(names) do
local classVal = cls[i] or ''
local displayName = (name ~= '' and name or '-')
local item = frame:expandTemplate{ title = 'llsifasitem', args = { kind, classVal, displayName } }
rowTag:tag('th'):wikitext(item):done()
end
return rowTag
end
-- 在指定行上追加图片单元格并返回该行 tag
local function buildImageRow(frame, rowTag, kind, ids, args)
for i, id in ipairs(ids) do
rowTag:tag('td'):css('width','300px'):wikitext(buildHover(frame, kind, i, id, args)):done()
end
return rowTag
end
function p.main(frame)
local args = getArgs(frame, { parentFirst = true })
-- 读取参数并解析
local urNames = splitParam(args.ur)
local urIds = splitParam(args['ur-id'])
local urCls = splitParam(args['ur-cl'])
local srNames = splitParam(args.sr)
local srIds = splitParam(args['sr-id'])
local srCls = splitParam(args['sr-cl'])
local actType = args.type or ''
local urCount = #urNames
local srCount = #srNames
if urCount == 0 and srCount == 0 then
return '<div class="error">(llsifascardshow) 未提供 UR 或 SR 数据。</div>'
end
local root = mw.html.create('table')
:addClass('wikitable')
:addClass('mw-collapsible')
:addClass('mw-collapsed')
:css('text-align', 'center')
-- 第一行标题
root:tag('tr')
:tag('th')
:attr('colspan', tostring(urCount + srCount + 1))
:wikitext(mw.ustring.format("'''活动%sUR / SR卡面'''", actType))
:done()
:done()
-- 第二行:UR / SR 标题(模仿原模板结构,插入分隔单元格)
local headerRow = root:tag('tr')
if urCount > 0 then
buildHeaderCells(frame, headerRow, 'ur', urNames, urCls)
end
-- 分隔列(rowspan=2 在原模板中,用一个空 th 实现分隔)
headerRow:tag('th'):attr('rowspan', '2'):wikitext(' '):done()
if srCount > 0 then
buildHeaderCells(frame, headerRow, 'sr', srNames, srCls)
end
-- 第三行:图片 Hover 行
local imgRow = root:tag('tr')
if urCount > 0 then
buildImageRow(frame, imgRow, 'ur', urIds, args)
end
if srCount > 0 then
buildImageRow(frame, imgRow, 'sr', srIds, args)
end
return tostring(root)
end
return p