local p = {}
local getArgs = require('Module:Arguments').getArgs
local frame = nil
local function check_skip(arg)
if mw.ustring.match(arg, "_%d+$", -3) then -- 跳過以“_數字”結尾的參數
return false
elseif mw.ustring.match(arg, "_[zZjJ][hHaA]$", -3) then -- 跳過以“_zh”、“_ja”結尾的參數
return false
elseif mw.ustring.match(arg, "^__") then -- 以“__”開頭的參數供模板使用
return false
end
return true
end
local function check_table(table, value)
if table == nil then
return false
end
for k,v in ipairs(table) do
if v == value then
return true
end
end
return false
end
local function generate_voice(key, args)
local data = {}
local zh_text = args[key .. "_zh"] or args[key .. "_Zh"] or args[key .. "_zH"] or args[key .. "_ZH"] or "''(暫無文本)''"
local ja_text = args[key .. "_ja"] or args[key .. "_Ja"] or args[key .. "_jA"] or args[key .. "_JA"] or "''(暫無文本)''"
local note = args[key .. "_note"] or ""
data["zh_text"] = mw.ustring.format('<div class="textToggleDisplay hidden" data-id="uma-voice-zh">%s</div>', zh_text)
data["ja_text"] = mw.ustring.format('<div class="textToggleDisplay hidden" data-id="uma-voice-ja"><hr><span lang="ja">-{%s}-</span></div>', ja_text)
data["note"] = note
local voice = ""
if args[key] then
voice = mw.ustring.format('<div class="uma-voice-file">%s', frame:preprocess("{{sm2|" .. args[key] .. "}}"))
end
local counter = 0
while counter < 10 do
if args[key .. "_" .. counter] == nil then
break
end
voice = voice .. frame:preprocess('{{sm2|' .. args[key .. "_" .. counter] .. '}}')
counter = counter + 1
end
data["voice"] = voice .. "</div>"
return data
end
function p.main(_frame)
local args = getArgs(_frame, { parentOnly = true })
frame = _frame
return p._main(args)
end
function p._main(args)
local output = ""
for i, k in ipairs(args) do
v = args[k] or ""
if check_skip(k) then
if mw.ustring.match(k, "^_橫欄") then -- 生成橫欄
output = output .. mw.ustring.format('|-\n! colspan="4" | %s\n', v)
else -- 生成語音行
local num = mw.ustring.match(k, "%d+$", -3)-- 僅在數字為1或不存在時處理
if num == "1" then
local name = mw.ustring.sub(k, 1, -mw.ustring.len(num) - 1)
local counter = 1
local output_a = ""
while true do
if check_table(args, name .. counter) == false then
counter = counter - 1
break
end
local data = generate_voice(name .. counter, args)
if data["note"] == "" then
output_a = output_a .. mw.ustring.format('|| %s%s \n|| %s \n|-\n', data["zh_text"], data["ja_text"], data["voice"])
else
output_a = output_a .. mw.ustring.format('|style="border-left:none;margin-left:0;"|<div style="display:flex;align-items:center;"><div class="note">%s</div>%s</div>%s || %s\n|-\n', data["note"], data["zh_text"], data["ja_text"], data["voice"])
end
counter = counter + 1
end
output_a = mw.ustring.sub(output_a, 1, -5)
if counter > 1 then
output = output .. mw.ustring.format('|-\n| rowspan="%d" style="width:0;white-space: nowrap;text-align:center;" | %s \n%s\n', counter, name, output_a)
else
output = output .. mw.ustring.format('|-\n | style="width:0;white-space: nowrap;text-align:center;" | %s \n%s\n', name, output_a)
end
elseif num == nil then
local data = generate_voice(k, args)
if data["note"] == "" then
output = output .. mw.ustring.format('|-\n| style="width:0;white-space: nowrap;text-align:center;" | %s \n|| %s%s || %s\n', k, data["zh_text"], data["ja_text"], data["voice"])
else
output = output .. mw.ustring.format('|-\n| style="width:0;white-space: nowrap;text-align:center;" | %s \n|style="border-left:none;margin-left:0;"| <div style="display:flex;align-items:center;"><div class="note">%s</div>%s</div>%s || %s\n', k, data["note"], data["zh_text"], data["ja_text"], data["voice"])
end
end
end
end
end
return output
end
return p