全站通知:

模块:ISteamNews

来自UnturnedWIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

这个模块解析了这个页面的 JSON数据 并且 沙盒 调用了这个模块


local p = {}
local json = require("mw.text").jsonDecode
local fetch = require("mw.title").new

local patterns = {
    h1 = "%[h1%]",
    h1_end = "%[/h1%]",
    h2 = "%[h2%]",
    h2_end = "%[/h2%]",
    b = "%[b%]",
    b_end = "%[/b%]",
    i = "%[i%]",
    i_end = "%[/i%]",
    list = "%[list%]",
    list_end = "%[/list%]",
    list_item = "%[%*%]",
    url = "%[url=(.-)%](.-)%[/url%]",
    img = "%[img%](.-)%[/img%]",
    steam_prefix = "{STEAM_CLAN_IMAGE}/6119952/"
}

local divStyle = '<div style="border: 2px solid #000; margin: 10px 0; background-color: #0005;">\n'
local innerDivStyle = '<div style="border: 1px solid #000; padding: 10px">\n'

-- Helper function to convert custom formatting to Wikitext
local function convertToWikitext(contents, title)
    if not contents then
        return "无内容"
    end
    local imageCounter = 0

    contents = contents
        :gsub(patterns.h1, "=== "):gsub(patterns.h1_end, " ===")
        :gsub(patterns.h2, "==== "):gsub(patterns.h2_end, " ====")
        :gsub(patterns.b, "'''"):gsub(patterns.b_end, "'''")
        :gsub(patterns.i, "''"):gsub(patterns.i_end, "''")
        :gsub(patterns.list, ""):gsub(patterns.list_end, "")
        :gsub(patterns.list_item, "*")
        :gsub(patterns.url, function(url, text)
            return text:match("^%s*$") and string.format("[%s %s]\n", url, url) or string.format("[%s %s]\n", url, text)
        end)
        :gsub(patterns.img, function(url)
            url = url:gsub(patterns.steam_prefix, "")
            local version = title:match("(%d+%.%d+%.%d+%.%d+)") or title:match("(%d+%.%d+%.%d+)") or "unknown_version"
            local function isValidVersion(v)
                return v:match("^%d+%p%d+%p%d+%p%d+$")
            end
            if not isValidVersion(version) then
                version = title
            end
            imageCounter = imageCounter + 1
            local newFileName = string.format("%s-%d.png", version, imageCounter)
            return string.format("\n[[File:%s]]\n", newFileName)
        end)
    return contents
end

-- Fetch and parse JSON data with caching
local cache = {}

local function fetchAndParseJSON(url)
    if cache[url] then
        return cache[url]
    end
    local title = fetch(url)
    local content = title and title:getContent()
    if not content then
        return nil, "无法获取 JSON 数据"
    end
    local data = json(content)
    if not data or not data.appnews then
        return nil, "无法解析 JSON 数据"
    end
    cache[url] = data.appnews.newsitems
    return data.appnews.newsitems
end

-- Main function to show patch notes
function p.showPatchNotes(frame)
    local newsItems, err = fetchAndParseJSON("用户:30175732/新闻沙盒/ISteamNews.json")
    if not newsItems then
        return err
    end
    local result = {}
    for i = 1, math.min(#newsItems, 10) do
        local item = newsItems[i]
        local title = item.title or "无标题"
        local url = item.url or "#"
        local author = item.author or "未知作者"
        local gid = item.gid or "#"
        local contents = convertToWikitext(item.contents, title)
        local date = os.date("%Y-%m-%d", item.date) or "无日期"
        local version = title:match("(%d+%.%d+%.%d+%.%d+)") or title:match("(%d+%.%d+%.%d+)")
        local headerImage = version and string.format("%s.png", version) or string.format("%s.png", item.gid) or "unknown_version.png"
       
        result[#result + 1] = divStyle
        result[#result + 1] = string.format('[[File:%s]]\n', headerImage)
        result[#result + 1] = innerDivStyle
        result[#result + 1] = string.format('== %s ==\n', title)
        result[#result + 1] = string.format('* 日期: %s\n', date)
        result[#result + 1] = string.format('* 作者: %s\n', author)
        result[#result + 1] = string.format('* 标题: %s\n', title)
        result[#result + 1] = string.format('* GID: %s\n', gid)
        result[#result + 1] = string.format('* 来源: [%s 查看原文]\n', url)
        result[#result + 1] = '\n' .. contents .. '\n\n'
        result[#result + 1] = '</div></div>\n\n'
    end
    return table.concat(result)
end

return p