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