bugfix1001.2

欢迎来到Star Engine 星引擎 WIKI!
Wiki交流QQ群:1090018247
点击成为魔法少女! 点击成为星趴高手!

由于游戏更新,Wiki正在更新页面,当前内容可能并非最新版本,欢迎进群督促!

全站通知:

模块:Test

来自吉星派对WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

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

-- Module:Test 
local p = {}

local function trim(s)
    if not s then return '' end
    return mw.text.trim(s)
end

local function extractImageUrl(html)
    -- 方法1: 匹配 img src
    local src = html:match('<img[^>]*src="([^"]+)"')
    if not src then
        src = html:match("<img[^>]*src='([^']+)'")
    end
    -- 方法2: 匹配 [[文件:...]] 原始标记
    if not src then
        src = html:match("%[%[文件:([^|%]]+)")
        if src then
            -- 这里需要根据您的 wiki 域名调整,示例使用 patchwiki 的路径规则
            src = "https://patchwiki.biligame.com/images/starengine/" .. src:gsub(" ", "_")
        end
    end
    return src
end

local function extractTooltip(html)
    local tooltip = html:match('data%-tooltip="([^"]+)"')
    if tooltip then
        tooltip = tooltip:gsub("&#39;", "'"):gsub("&quot;", '"'):gsub("&amp;", "&")
        return tooltip
    end
    return nil
end

local function extractName(html, tooltip)
    if tooltip then
        local name = mw.ustring.match(tooltip, "^([^::]+)[::]")
        if name then
            return trim(name)
        end
    end
    local alt = html:match('<img[^>]*alt="([^"]+)"')
    if alt then
        return trim(alt)
    end
    return nil
end

function p.buildData(frame)
    local args = {}
    -- 合并父框架参数(优先使用父框架)
    for k, v in pairs(frame.args) do args[k] = v end
    local parent = frame:getParent()
    if parent then
        for k, v in pairs(parent.args) do args[k] = v end
    end

    local rows = tonumber(args.height) or tonumber(args.rows) or 5
    local cols = tonumber(args.width) or tonumber(args.cols) or 5

    local data = {}
    -- 遍历所有参数
    for k, v in pairs(args) do
        local letter, num = k:match("^([A-Z])(%d+)$")
        if letter and num then
            local row = string.byte(letter) - 64
            local col = tonumber(num)
            if row <= rows and col <= cols then
                if not data[row] then data[row] = {} end
                local parsed = frame:preprocess(v)
                local imageUrl = extractImageUrl(parsed)
                local tooltip = extractTooltip(parsed)
                local name = extractName(parsed, tooltip)

                if imageUrl then
                    data[row][col] = {
                        type = "image",
                        url = imageUrl,
                        tooltip = tooltip,
                        name = name
                    }
                else
                    local text = extractText(parsed)
                    -- 如果文本为空但有名称,用名称代替
                    if text == "" and name then
                        text = name
                    end
                    data[row][col] = {
                        type = "text",
                        text = text,
                        tooltip = tooltip,
                        name = name
                    }
                end
            end
        end
    end

    -- 补全空单元格
    for i = 1, rows do
        if not data[i] then data[i] = {} end
        for j = 1, cols do
            if not data[i][j] then
                data[i][j] = { type = "text", text = "", tooltip = nil, name = nil }
            end
        end
    end

    -- 安全编码 JSON
    local success, json = pcall(mw.text.jsonEncode, data)
    if success then
        return json
    else
        return '{"error":"JSON encoding failed"}'
    end
end

-- 辅助函数 extractText 保留原样
local function extractText(html)
    local text = html:gsub("<[^>]+>", "")
    text = text:gsub("&[a-zA-Z]+;", {
        ["&amp;"] = "&", ["&lt;"] = "<", ["&gt;"] = ">", ["&quot;"] = '"', ["&nbsp;"] = " "
    })
    return trim(text)
end

-- buildRowHeaders 和 buildColHeaders 保持不变(略)
function p.buildRowHeaders(frame)
    local args = {}
    for k, v in pairs(frame.args) do args[k] = v end
    local parent = frame:getParent()
    if parent then
        for k, v in pairs(parent.args) do args[k] = v end
    end
    local rows = tonumber(args.height) or tonumber(args.rows) or 5
    local headers = {}
    for i = 1, rows do
        local custom = args["row" .. i]
        if custom and custom ~= "" then
            headers[i] = custom
        else
            if i <= 26 then
                headers[i] = string.char(64 + i)
            else
                local first = math.floor((i - 1) / 26)
                local second = (i - 1) % 26 + 1
                headers[i] = string.char(64 + first) .. string.char(64 + second)
            end
        end
    end
    local success, json = pcall(mw.text.jsonEncode, headers)
    return success and json or "[]"
end

function p.buildColHeaders(frame)
    local args = {}
    for k, v in pairs(frame.args) do args[k] = v end
    local parent = frame:getParent()
    if parent then
        for k, v in pairs(parent.args) do args[k] = v end
    end
    local cols = tonumber(args.width) or tonumber(args.cols) or 5
    local headers = {}
    for j = 1, cols do
        local custom = args["col" .. j]
        if custom and custom ~= "" then
            headers[j] = custom
        else
            headers[j] = tostring(j)
        end
    end
    local success, json = pcall(mw.text.jsonEncode, headers)
    return success and json or "[]"
end

return p