打开/关闭菜单
60
73
29
1.1K
武外梗百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Module:PageSummary

武外梗百科 爱国好学自强图新的百科全书
240d:c010:c8:4::40留言2026年2月18日 (三) 13:06的版本

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

local p = {}

function p.getSummaryAndImage(frame)
    -- 获取条目内容
    local pageTitle = frame.args[1] or mw.title.getCurrentTitle().text
    local title = mw.title.new(pageTitle)
    if not title or not title.exists then return "条目不存在" end
    local content = title:getContent()

    -- 1. 自动提取第一张图
    local image = ""
    for img in mw.ustring.gmatch(content, "%[%[%s*[Ff][Ii][Ll][Ee]%s*:([^|%]]+)") do
        -- 过滤掉 SVG 图标
        if not mw.ustring.match(img, "%.svg") then
            image = mw.text.trim(img)
            break
        end
    end

    -- 2. 自动清理并提取首段
    local summary = content
    summary = mw.ustring.gsub(summary, "<ref[^>]*>.-</ref>", "") -- 删脚注
    summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "")         -- 删一级模板
    summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "")         -- 删嵌套模板
    summary = mw.ustring.gsub(summary, "%[%[([^%]]-)|?([^%]]-)%]%]", "%2") -- 提取链接文字
    
    -- 寻找第一个非空的段落
    local _, firstPara = mw.ustring.match(summary, "\n\n%s*([^%*#\n][^\n]+)")
    if not firstPara then
        firstPara = mw.ustring.sub(summary, 1, 150) -- 保底截取
    else
        firstPara = mw.ustring.sub(firstPara, 1, 150)
    end

    -- 3. 输出 UI
    local container = mw.html.create('div'):cssText('display:flex; gap:10px; border:1px solid #aaa; padding:10px;')
    if image ~= "" then
        container:tag('div'):wikitext(string.format('[[File:%s|100px]]', image))
    end
    container:tag('div'):tag('b'):wikitext(pageTitle):done():tag('p'):wikitext(firstPara .. "...")

    return tostring(container)
end

return p