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