local table = table
local string = string
local ipairs = ipairs
local mw = mw
local p = {}
local config = {} -- 等於 frame.args,在此處聲明是為了全局使用和便於理解
local infoboxText = {}
-- 生成標題欄
local function genTitle()
local snippet = '|- class="infobox-title"\n' ..
'| colspan="3" style="' .. config['title-style'] .. '" | ' .. config['title']
table.insert(infoboxText, tostring(snippet))
end
-- 生成圖片欄
local function genImage()
local snippet = '|- class="infobox-image-container"\n' ..
'| colspan="3" style="' .. config['image-style'] .. '" |'
if config['img-src'] ~= '' then
snippet = snippet .. '[[File:' .. config['img-src'] .. '|class=infobox-image|' .. config['img-size'] .. ']]<br/>'
end
snippet = snippet .. config['img-desc']
table.insert(infoboxText, tostring(snippet))
end
-- 生成 Tabs 欄
local function genTabs()
local snippet = '|-\n' ..
'| colspan="3" | ' .. config['tabs']
table.insert(infoboxText, tostring(snippet))
end
-- 生成子標題欄
local function genSubtitle(subtitle)
local snippet = '| style="' .. config['subtitle-style'] .. '" colspan="3" | ' .. subtitle
table.insert(infoboxText, tostring(snippet))
end
-- 生成雙內容欄
local function genDoubleColumn(leftContent, rightContent)
local snippet = '| style="' .. config['double-l-style'] .. '" | ' .. leftContent .. ' || style="' .. config['double-r-style'] .. '" colspan="2" | ' .. rightContent
table.insert(infoboxText, tostring(snippet))
end
-- 生成單內容欄
local function genSingleColumn(content)
local snippet = '| style="' .. config['single-style'] .. '" colspan="3" | ' .. content
table.insert(infoboxText, tostring(snippet))
end
-- 生成底部欄
local function genBottom()
local snippet = '! colspan="3" style="' .. config['b-style'] .. '" | ' .. config['bottom']
table.insert(infoboxText, tostring(snippet))
end
function p.main(frame)
local parentFrame = frame:getParent()
config = frame.args
-- 處理標題欄、圖片欄等頂部組件
-- 這些組件都是一次性的,不必借用匿名參數
if config['title'] ~= '' then
genTitle()
end
genImage()
if config['tabs'] ~= '' then
genTabs()
end
-- 批量處理中部組件
-- 繞過 Scribunto 的設計哲學,借用匿名參數保持順序,並約定用首個 `::` 分割鍵值
table.insert(infoboxText, '|-')
for i, arg in ipairs(parentFrame.args) do
local key, val = string.match(arg, '^%s*(.-)%s*::%s*(.-)%s*$')
if key ~= nil and val ~= '' then
local prefix = string.sub(key, 1, 1)
if prefix == '-' then
genSubtitle(val)
elseif prefix == '_' then
genSingleColumn(val)
elseif prefix == '+' then
table.insert(infoboxText, val)
else
genDoubleColumn(key, val)
end
table.insert(infoboxText, '|-\n|-')
end
end
-- 處理底部欄等底部組件
if config['bottom'] ~= '' or config['b-style'] ~= '' then
genBottom()
end
-- 套上最外層 table 並返回
return '{|class="moe-infobox ' .. config['class'] .. '" align="right" border="' .. config['border'] .. '" cellpadding="' .. config['cellpadding'] .. '" style="' .. config['style'] .. '"\n' ..
table.concat(infoboxText, '\n') .. '\n' ..
'|}'
end
return p