全站通知:

模块:图标构造

来自斯露德WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

本模块是为了配合模板:图标一起使用所创建,非必要请勿直接使用,建议到此页面查看相关使用方式
若是想看我一个门外汉是怎么用GPT写的这个东西的话...那请便


local p = {}

-- 辅助函数:从参数表中获取参数,若参数不存在或为空,则返回默认值
local function parseArgument(frame, argName, defaultValue)
    local arg = frame.args[argName]
    if arg == nil or arg == "" then
        return defaultValue
    else
        return arg
    end
end

-- 辅助函数:通过传入参数来设置内部参数
local function recordParameters(frame)
    local arg1 = parseArgument(frame, "1", "")
    local arg2 = parseArgument(frame, "2", "")
    local arg3 = parseArgument(frame, "3", "")
    local showText = frame.args.showText
    -- 若showText未输入,则默认为布尔型的开启(true)
    local boolShowText

    if showText == nil or showText == "" then
        boolShowText = true
    else
        -- 检测输入的值是true还是false,并转换为布尔型
        boolShowText = showText == "true" or showText == "1" or showText ==
                           "yes"
    end

    local iconName
    local iconImg
    local iconNum

    -- 若参数1等于其中之一 设置参数2为名称 3为数量 否则 1为名称 2为数量(3废除)
    if arg1 == "道具" or arg1 == "角色" or arg1 == "武装" then
        iconName = arg2
        iconNum = arg3
    else
        iconName = arg1
        iconNum = arg2
    end

    -- 若参数1等于武装或角色 图片名加上后缀
    if arg1 == "武装" or arg1 == "角色" then
        iconImg = iconName .. "_方图"
    else
        iconImg = iconName
    end

    iconImg = replaceIconName(iconImg)

    local iconShowText = '[[' .. iconName .. ']]'

    -- 若非显示文本 则设置文本为空
    if showText == "false" then
        iconShowText = ''
        -- 再若显示类型为数字 则仅绘制x数字
    elseif showText == "num" then
        iconShowText = ' × ' .. iconNum
        -- 再若输入类型为空或无切
    elseif (showText == nil or showText == "") and
        (iconNum ~= nil and iconNum ~= "") then
        iconShowText = iconShowText .. ' × ' .. iconNum
    end

    return iconName, iconImg, iconNum, iconShowText
end

-- 新添加: 为通用拼图图标设置内部参数
local function GeneraRecordParameters(frame)
    local arg1 = parseArgument(frame, "1", "")
    local arg2 = parseArgument(frame, "2", "")
    local arg3 = parseArgument(frame, "3", "")
    local showText = frame.args.showText
    -- 若showText未输入,则默认为布尔型的开启(true)
    local boolShowText

    if showText == nil or showText == "" then
        boolShowText = true
    else
        -- 检测输入的值是true还是false,并转换为布尔型
        boolShowText = showText == "true" or showText == "1" or showText ==
                           "yes"
    end

    local iconName
    local iconImg
    local iconNum

    -- 若参数1等于其中之一 设置参数2为名称 3为数量 否则 1为名称 2为数量(3废除)
    if arg1 == "道具" or arg1 == "角色" or arg1 == "武装" then
        iconName = arg2
        iconNum = arg3
    else
        iconName = arg1
        iconNum = arg2
    end

    -- 若参数1等于武装或角色 图片名加上后缀
    if arg1 == "武装" or arg1 == "角色" then
        iconImg = iconName .. "_方图"
    else
        iconImg = iconName
    end

    iconImg = replaceIconName(iconImg)

    local iconShowText = '[[' .. iconName .. ']]'

    -- 若非显示文本 则设置文本为空
    if showText == "false" then
        iconShowText = ''
        -- 再若显示类型为数字 则仅绘制x数字
    elseif showText == "num" then
        iconShowText = ' × ' .. iconNum
        -- 再若输入类型为空或无切
    elseif (showText == nil or showText == "") and
        (iconNum ~= nil and iconNum ~= "") then
        iconShowText = iconShowText .. ' × ' .. iconNum
    end

    return iconName, iconImg, iconNum, iconShowText
end

-- 检查字符串是否以"意识碎片·"开头并替换第五个字符的"·"
function replaceIconName(iconName)
    local prefix = "意识碎片·"

    if #iconName >= #prefix then
        local firstFiveChars = iconName:sub(1, #prefix)
        if firstFiveChars == prefix then
            local newName = iconName:gsub("·", "_", 1) -- 替换第一个匹配的"·"为"_"
            return newName
        end
    end

    return iconName
end

-- 输出小图标
function p.min(frame)
    local iconName, iconImg, iconNum, iconShowText = recordParameters(frame)
    local bool = true

    local output =
        '<span class="icon-min">[[文件:' .. iconImg .. '.png|30px|' ..
            iconName .. '|link=' .. iconName .. ']]' .. iconShowText ..
            '</span>'

    return output
end

-- 输出大图标
function p.big(frame)
    local iconName, iconImg, iconNum, iconShowText = recordParameters(frame)

    local output =
        '<span class="icon-big">[[文件:' .. iconImg .. '.png|60px|' ..
            iconName .. '|link=' .. iconName .. ']]' .. iconShowText ..
            '</span>'

    return output
end

-- 输出拼图图标
function p.piece(frame)
    local iconName, iconImg, iconNum, iconShowText = recordParameters(frame)
    local showText = frame.args.showText

    local showName
    if showText == "num" then
        showName = ""
    else
        showName = iconName
    end

    local lengthValue
    if mw.ustring.len(iconName) > 5 then
        lengthValue = "font-size:1px;white-space:nowrap;left:0%;"
    else
        lengthValue = "left:6%;"
    end

    -- 若iconNum为nil,则设置默认值为空白
    if iconNum == "" or iconNum == nil then iconNum = "" end

    local output =
        '<div class="icon-img">[[文件:' .. iconImg .. '.png|link=' .. iconName ..
            ']]</div>' .. '<div class="icon-font font-shadow" style="bottom: 0;' ..
            lengthValue .. ' "> ' .. showName .. '</div>' ..
            '<div class="icon-font font-shadow" style="top: 0; right: 6%;">' ..
            iconNum .. '</div>'
    return output
end

-- 新添加: 输出通用拼图图标
function p.GeneralPiece(frame)
    local iconName, iconImg, iconNum, iconShowText = GeneraRecordParameters(frame)
    local showText = frame.args.showText

    local showName
    if showText == "num" then
        showName = ""
    else
        showName = iconName
    end

    local lengthValue
    if mw.ustring.len(iconName) > 5 then
        lengthValue = "font-size:1px;white-space:nowrap;left:0%;"
    else
        lengthValue = "left:6%;"
    end

    -- 若iconNum为nil,则设置默认值为空白
    if iconNum == "" or iconNum == nil then iconNum = "" end

    local output =
        '<div class="icon-img">[[文件:' .. iconImg .. '.png|link=' .. iconName ..
            ']]</div>' .. '<div class="icon-font font-shadow" style="bottom: 0;' ..
            lengthValue .. ' "> ' .. showName .. '</div>' ..
            '<div class="icon-font font-shadow" style="top: 0; right: 6%;">' ..
            iconNum .. '</div>'
    return output
end

return p