缺氧 wiki 编辑团队提示:注册账号并登录后体验更佳,且可通过参数设置定制优化您的浏览体验!

该站点为镜像站点,如果你想帮助这个由玩家志愿编辑的 wiki 站点,请前往原站点参与编辑,
同时欢迎加入编辑讨论群 851803695 与其他编辑者一起参与建设!

全站通知:

模块:植物导航

来自缺氧WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索
-- Module:植物导航
local p = {}
local fstr = mw.ustring.format -- shortcut for formattig a string
local getArgs = require('Module:Dev/Arguments').getArgs
local navbox = require('Module:导航栏')
local sepEntry = "@@@@@@@@"
local sepSec = "@@$$"

local OTHER_ORDER = "其他植物"
local catsOrder = {
    ["食用性植物"] = 1,
    ["装饰性植物"] = 2,
    ["工业性植物"] = 3,
    ["功能性植物"] = 4
}

function setGroup(group, name, cate)
    if group[cate] == nil then
        group[cate] = {
            cate = cate,
            sort = catsOrder[cate] or 100,
            pages = {}
        }
    end
    table.insert(group[cate].pages, fstr("{{物品|%s}}", name))
end

function p.getGruops(category)
    local dplArgs = {
        uses = "Template:植物信息框",
        category = category,
        namespace = "",
        format = ","..sepSec.."%TITLE%,"..sepEntry.."%CATNAMES%",
        includesubpages = "false",
        skipthispage = "no",
        redirects = "include",
        addcategories=true
    }
    local dplResult = mw.getCurrentFrame():callParserFunction('#dpl:', dplArgs)
    local fResult = mw.text.split(dplResult, sepSec, true)

    local groups = {}
    if fResult ~= nil and #fResult > 0 then
        for _, fText in ipairs(fResult) do
            local textTable = mw.text.split(fText, sepEntry, true)
            if textTable ~= nil and #textTable == 2 then 
                local name = textTable[1]
                local cateText = textTable[2]
                if cateText ~= nil then
                    local cates = mw.text.split(cateText, ", ", ture)
                    if cates ~= nil and #cates > 0 then
                        -- 有分类的植物
                        local useCate = nil
                        for _, cate in ipairs(cates) do
                            if catsOrder[cate] ~= nil then
                                useCate = cate
                                break
                            end
                        end
                        setGroup(groups, name, useCate or OTHER_ORDER)
                    else
                        setGroup(groups, name, OTHER_ORDER)
                    end
                else
                    setGroup(groups, name, OTHER_ORDER)
                end
            end
        end
    end
    return groups
end

function p._main()
    local out = {"[[植物]]"}
    local groups = p.getGruops()
    local cateNames = {OTHER_ORDER}
    for cate, index in pairs(catsOrder) do
        table.insert(cateNames, cate)
    end
    table.sort(cateNames, function(a, b)
        if not catsOrder[a] then return false end
        if not catsOrder[b] then return true end
        return catsOrder[a] < catsOrder[b]
    end)
    for _, cate in ipairs(cateNames) do
        local group = groups[cate]
        if group ~= nil then
            local plants = group.pages
            local cat = group.cate
            if plants ~= nil and #plants > 0 then 
                table.sort(plants)
                table.insert(out, fstr("[[:Category:%s|%s]]", cat, cat))
                table.insert(out, table.concat(plants, " {{*}} "))
            end
        end
    end
    return out
end

-- test by: = p.main(require("Module:debug").frame({},{debug=1}))
function p.main(frame)
    local args = getArgs(frame)
    local out = p._main()
    if args.debug then
        mw.logObject(out)
    end
    return navbox.main(out)
    
    --mw.getCurrentFrame():expandTemplate{
    --    title = "PortableNavbox",
    --    args = out
    --}
end

return p