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

Module:PageSummary

武外梗百科 爱国好学自强图新的百科全书
240d:c010:132:2::f4留言2026年2月18日 (三) 09:02的版本

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

local p = {}

local function stripImages(text)
    if not text or text == "" then return "" end
    text = text:gsub("%[%[(文件|Image|图像):", "[[File:")
           :gsub("%[%[(分类|Category):", "[[Category:")
    local out = {}
    local i = 1
    local len = #text
    while i <= len do
        if text:byte(i) == 91 and text:byte(i+1) == 91 then
            local chunk = text:sub(i, i+15)
            if chunk:match("^%[%[File:") or chunk:match("^%[%[Category:") then
                local depth = 1
                local j = i + 2
                while j <= len and depth > 0 do
                    if text:byte(j) == 91 and text:byte(j+1) == 91 then
                        depth = depth + 1
                        j = j + 2
                    elseif text:byte(j) == 93 and text:byte(j+1) == 93 then
                        depth = depth - 1
                        j = j + 2
                    else
                        j = j + 1
                    end
                end
                i = j
            else
                table.insert(out, text:sub(i, i+1))
                i = i + 2
            end
        else
            table.insert(out, text:sub(i, i))
            i = i + 1
        end
        if i > 1200 then break end
    end
    return table.concat(out)
end

local function cleanFinal(text)
    if not text or text == "" then return "" end
    text = text
        :gsub("{{[^{}]*}}", "")
        :gsub("<ref.-</?ref[^>]*>", "")
        :gsub("<!%-%-.-%-%->", "")
        :gsub("\n==+[^=]+==+", " ")
        :gsub("\n%s*[*#:;]+", " ")
        :gsub("%s*\n%s*", " ")
        :gsub("%s+", " ")
    return mw.text.trim(text)
end

function p.getSummaryAndImage(frame)
    local pageName = mw.text.trim(frame.args[1] or "")
    if pageName == "" then return "" end

    local title = mw.title.new(pageName)
    if not title or not title.exists then return "" end

    local content = title:getContent()
    if not content or content == "" then return "" end

    local rawExcerpt = mw.ustring.sub(content, 1, 800) or ""

    -- 改用更可靠的方式提取第一张 File/文件 图片
    local firstImage = nil
    local prefixes = {"File:", "文件:"}
    for _, prefix in ipairs(prefixes) do
        local startPos = mw.ustring.find(rawExcerpt, "%[%[" .. prefix)
        if startPos then
            local endPos = mw.ustring.find(rawExcerpt, "|", startPos) or mw.ustring.find(rawExcerpt, "%]%]", startPos)
            if endPos then
                local candidate = mw.ustring.sub(rawExcerpt, startPos + #prefix + 2, endPos - 1)
                candidate = mw.text.trim(candidate)
                if candidate ~= "" and mw.ustring.find(candidate, "%.") then  -- 必须有扩展名
                    firstImage = prefix .. candidate
                    break
                end
            end
        end
    end

        local step1     = stripImages(rawExcerpt) or ""
    local cleanText = cleanFinal(step1) or ""

        local targetLen = 120
    local summary = cleanText or ""

    -- 先强制清理任何可能导致编码问题的隐藏字符
    summary = mw.ustring.gsub(summary, "[%z\127-\159]", "")  -- 移除控制字符、BOM等

    local ulen = mw.ustring.len(summary)
    if ulen > targetLen then
        -- 截取到 targetLen + 一点缓冲,避免边界问题
        local temp = mw.ustring.sub(summary, 1, targetLen + 20)
        
        -- 从后往前找第一个安全的断点(空格、中文标点)
        local breakPos = nil
        for i = #temp, 1, -1 do
            local char = mw.ustring.sub(temp, i, i)
            if char:match("[ %s,。!?;:,…、]") then
                breakPos = i
                break
            end
        end
        
        if breakPos and breakPos > targetLen - 30 then
            summary = mw.ustring.sub(temp, 1, breakPos)
        else
            summary = mw.ustring.sub(temp, 1, targetLen)
        end
        
        summary = mw.text.trim(summary) .. "…"
    else
        summary = mw.text.trim(summary)
    end

    local container = mw.html.create('div')
        :css('margin-bottom', '25px')
        :css('display', 'flow-root')

    container:tag('div')
        :css('font-size', '1.3em')
        :css('font-weight', 'bold')
        :css('margin-bottom', '6px')
        :css('border-bottom', '1px solid #eee')
        :wikitext('[[' .. pageName .. ']]')

    local textDiv = container:tag('div')
        :css('line-height', '1.6')
        :css('color', '#222')
        :css('font-size', '14px')

    -- 严格只在有有效图片时输出
    if firstImage and firstImage ~= "" then
        textDiv:wikitext('[[' .. firstImage .. '|120px|right|link=' .. pageName .. ']]')
    end

    textDiv:wikitext(summary)

    return tostring(container)
end

return p