全站通知:

模块:Infobox3Base

来自无畏契约WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:Infobox3Base/doc创建

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
            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
        -- 子标题栏和单内容栏不需要 key,故也允许用户直接传入 `-val` 和 `_val`
        else
            local tok, val = string.match(arg, '^([%-_])%s*(.-)%s*$')
            if tok ~= nil and val:sub(-2) ~= "::" and val ~= "" then
                if tok== '-' then
                    genSubtitle(val)
                elseif tok == '_' then
                    genSingleColumn(val)
                end
            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