Module:背景故事

来自缺氧 Wiki
跳转到导航 跳转到搜索

可在Module:背景故事/doc创建此模块的帮助文档

-- Module:背景故事
local p = {}
local fstr = mw.ustring.format -- shortcut for formattig a string
local ModulePo = require([[Module:Po]])
local utils = require([[Module:Utils]])
local getArgs = require('Dev:Arguments').getArgs
local eq = "="

local function po(k)
    local out = ModulePo.po(k)
    if utils.isDefaultT(k, out) then
        out = fstr("<span class='missed-string-key'>%s</span>", k)
    end
    return out
end

local function renderSection(e, titleLevel)
    local ct = e.content
    local eOut = {}
    if ct.title then
        table.insert(eOut, fstr("%s %s %s", eq:rep(titleLevel), po(ct.title),
                                eq:rep(titleLevel)))
    end
    local dlcIds = nil
    if ct.dlcIds then dlcIds = table.concat(ct.dlcIds, " ") end
    -- TODO dlcIds

    if ct.contentContainers ~= nil then
        for _, ecc in ipairs(ct.contentContainers) do
            local contentLayout = ecc.contentLayout and "contentLayout_" ..
                                      ecc.contentLayout or ""
            local contentId = ecc.lockID or ""
            local eccOut = {}
            for _, content in ipairs(ecc.content) do
                if content.tag == "!CodexText" then
                    table.insert(eccOut,
                                 fstr("<span class='codex-text-%s'>%s</span>",
                                      content.nodes.style,
                                      po(content.nodes.stringKey)))
                elseif content.tag == "!CodexDividerLine" then
                    table.insert(eccOut,
                                 "<span class='codex-divider-line'></span>")
                else
                    error(fstr('Unexpected tag: "%s"', content.tag))
                end
            end
            table.insert(eOut,
                         fstr('<span class="%s" id="%s">%s</span>',
                              contentLayout, contentId,
                              table.concat(eccOut, "\n")))
        end
    end
    return table.concat(eOut, "\n")
end

-- test by: = p._main({"Emails"})
-- test by: = p._main({"Investigations"})
-- test by: = p._main({"Notices", title="打印舱宣传广告"})
function p._main(args)
    local codexData = mw.loadData("Module:Data/Codex")
    local entries = nil
    local catId = args[1]
    local titleLevel = tonumber(args[2]) or 2
    local title = args["title"]
    for _, cat in ipairs(codexData.dirs) do
        if cat.name == catId then
            entries = cat.content.files
            break
        end
    end
    if entries == nil then error(fstr("Can not find '%s'.", catId)) end

    if title ~= nil then
        for _, e in ipairs(entries) do
            if e.content.title ~= nil and po(e.content.title) == title then
                return renderSection(e, titleLevel)
            end
        end
        error(fstr("No title found: %s", title))
    else
        local out = {}
        for _, e in ipairs(entries) do
            table.insert(out, renderSection(e, titleLevel))
        end

        out = table.concat(out, "\n")
        if not args.nocat then out = out .. "\n[[Category:背景故事]]" end

        return out
    end
end

function p.main(frame) return frame:preprocess(p._main(getArgs(frame))) end

return p