local p = {}
-- 辅助函数:安全获取参数值
local function getValue(val, index, default)
if val and val[index] then
return mw.text.trim(val[index]) or default
end
return default
end
-- 主函数
function p.main(frame)
local args = frame.args
local parentArgs = frame:getParent().args
-- 收集所有参数
local allArgs = {}
for k, v in pairs(parentArgs) do
if type(k) == 'number' then
allArgs[k] = v
end
end
-- 提取学生英文名
local studentName = ""
if allArgs[1] then
local parts = mw.text.split(allArgs[1], '@')
if #parts >= 2 and mw.text.trim(parts[1]) == 'name' then
studentName = mw.text.trim(parts[2])
end
end
-- 构建表格内容
local rows = {}
-- 表格标题行
table.insert(rows, '{| class="wikitable mw-collapsible mw-collapsed" style="background:#DDEEFF; width:100%;"')
table.insert(rows, '|-')
table.insert(rows, '! colspan=3 style="color:white;-webkit-text-fill-color:white;background:#375375"|学生台词与语音')
table.insert(rows, '|-')
table.insert(rows, '! 场合 !! 台词 !! 语音')
table.insert(rows, '|-')
-- 处理每个参数行
for i = 2, #allArgs do
if allArgs[i] and allArgs[i] ~= '' then
local parts = mw.text.split(allArgs[i], '@')
if #parts >= 4 then
local occasion = getValue(parts, 1, "")
local textC = getValue(parts, 2, "")
local textJ = getValue(parts, 3, "")
local mediaSuffix = getValue(parts, 4, "")
-- 构建行内容
local row = "| " .. occasion .. "\n"
row = row .. "| " .. textC
if textJ and textJ ~= "" then
row = row .. "<hr/>" .. frame:expandTemplate{title = "lj", args = {textJ}}
end
row = row .. "\n| "
-- 添加语音文件(如果存在)
if studentName ~= "" and mediaSuffix ~= "" then
local filename = "BA_V_" .. studentName .. "_" .. mediaSuffix .. ".ogg"
row = row .. frame:callParserFunction("#if", frame:callParserFunction("filepath", filename), frame:expandTemplate{title = "sm2", args = {filename}})
end
table.insert(rows, row)
table.insert(rows, "|-")
end
end
end
table.insert(rows, "|}")
return table.concat(rows, "\n")
end
return p