打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:CardData

来自夜幕之下
Rin留言 | 贡献2026年3月11日 (三) 21:17的版本

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

local p = {}

local API = "https://data.saltedkiss.org/items/cards"

-- 解析JSON
local function parseJson(str)
    local ok, data = pcall(mw.text.jsonDecode, str)
    if ok then
        return data
    end
    return nil
end

-- HTTP请求
local function request(url)
    local res = mw.http.fetch(url)
    if res.status == 200 then
        return res.body
    end
    return nil
end

function p.card(frame)

    local name = frame.args[1] or frame.args.name
    if not name then
        return "未提供角色名"
    end

    local fields = table.concat({
        "stylename",
        "stories.*",
        "stories.story.*"
    }, ",")

    local url = API ..
        "?fields=" .. mw.uri.encode(fields) ..
        "&filter[stylename][_eq]=" .. mw.uri.encode(name)

    local raw = request(url)
    if not raw then
        return "API请求失败"
    end

    local json = parseJson(raw)
    if not json or not json.data or not json.data[1] then
        return "未获取到数据"
    end

    local card = json.data[1]

    local html = {}

    table.insert(html, '<div class="card-story">')
    table.insert(html, '<h2>小传</h2>')

    if card.stories then

        for _,storyGroup in ipairs(card.stories) do

            if storyGroup.story then

                for _,story in ipairs(storyGroup.story) do

                    if story.text then

                        local text = mw.text.nowiki(story.text)
                        text = text:gsub("\n","<br>")

                        table.insert(html,
                            '<p class="story-text">' .. text .. '</p>'
                        )

                    end

                end

            end

        end

    else
        table.insert(html, "<p>未获取到数据</p>")
    end

    table.insert(html, '</div>')

    return table.concat(html,"\n")

end

return p