• Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/Chi ZJ2/old

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [创建] [刷新]
local table = table
local string = string
local ipairs = ipairs

local p = {}

local config = {}   -- 等于 frame.args,在此处声明是为了全局使用和便于理解
local infoboxHtml = {}

-- 生成标题栏
local function genTitle()
    local snippet = mw.html.create('tr')
        :addClass('infobox-title')
        :tag('td')
            :attr('colspan', '3')
            :cssText(config['title-style'])
            :wikitext(config['title'])
            :done()
    table.insert(infoboxHtml, tostring(snippet))
end

-- 生成图片栏
local function genImage()
    local snippet = mw.html.create('tr')
        :addClass('infobox-image-container')
        :tag('td')
            :attr('colspan', '3')
            :cssText(config['image-style'])
    if config['img-src'] ~= '' then
        snippet
            :wikitext('[[File:' .. config['img-src'] .. '|class=infobox-image|' .. config['img-size'] .. ']]')
            :tag('br'):done()
    end
    snippet
        :wikitext(config['img-desc'])
        :done()
    table.insert(infoboxHtml, tostring(snippet))
end

-- 生成 Tabs 栏
local function genTabs()
    local snippet = mw.html.create('tr')
        :tag('td')
            :attr('colspan', '3')
            :wikitext(config['tabs'])
            :done()
    table.insert(infoboxHtml, tostring(snippet))
end

-- 生成子标题栏
local function genSubtitle(subtitle)
    local snippet = mw.html.create('tr')
        :tag('td')
            :attr('colspan', '3')
            :cssText(config['subtitle-style'])
            :wikitext(subtitle)
            :done()
    table.insert(infoboxHtml, tostring(snippet))
end

-- 生成双内容栏
local function genDoubleColumn(leftContent, rightContent)
    local snippet = mw.html.create('tr')
        :tag('td')
            :cssText(config['double-l-style'])
            :wikitext(leftContent)
            :done()
        :tag('td')
            :attr('colspan', '2')
            :cssText(config['double-r-style'])
            :wikitext(rightContent)
            :done()
    table.insert(infoboxHtml, tostring(snippet))
end

-- 生成单内容栏
local function genSingleColumn(content)
    local snippet = mw.html.create('tr')
        :tag('td')
            :attr('colspan', '3')
            :cssText(config['single-style'])
            :wikitext(content)
            :newline()
            :done()
    table.insert(infoboxHtml, tostring(snippet))
end

-- 生成底部栏
local function genBottom()
    local snippet = mw.html.create('tr')
        :tag('th')
            :attr('colspan', '3')
            :cssText(config['b-style'])
            :wikitext(config['bottom'])
            :done()
    table.insert(infoboxHtml, 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 的设计哲学,借用匿名参数保持顺序,并约定用首个 `::` 分割键值
    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
        end
    end

    -- 处理底部栏等底部组件
    genBottom()

    -- 套上最外层 div 并返回
    return mw.html.create('table')
        :addClass('moe-infobox infobox2')
        :attr({
            align = "right",
            border = "0",
            cellpadding = "1"
        })
        :cssText(config['style'])
        :node(table.concat(infoboxHtml))
end

return p