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

Module:PageSummary:修订间差异

武外梗百科 爱国好学自强图新的百科全书
无编辑摘要
无编辑摘要
第1行: 第1行:
local p = {}
local p = {}


-- 实际的逻辑处理函数
function p.getSummaryAndImage(frame)
function p.getSummaryAndImage(frame)
     -- 如果你是通过模板调用,使用 getParent().args;如果是直接调用,使用 args
     -- 获取条目内容
    local args = frame:getParent().args
     local pageTitle = frame.args[1] or mw.title.getCurrentTitle().text
     local pageTitle = args[1] or 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 entityId = mw.wikibase.getEntityIdForTitle(pageTitle)
    local content = title:getContent()


     if not entityId then
     -- 1. 自动提取第一张图
         return '<div class="error">未找到关联的 Wikibase 项目。</div>'
    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
     end


     -- 1. 获取描述
     -- 2. 自动清理并提取首段
     local description = mw.wikibase.getDescription(entityId) or "暂无描述"
     local summary = content
 
    summary = mw.ustring.gsub(summary, "<ref[^>]*>.-</ref>", "") -- 删脚注
     -- 2. 获取图片 (P18)
     summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "")        -- 删一级模板
     local image = ""
    summary = mw.ustring.gsub(summary, "{{[^{}]-}}", "")         -- 删嵌套模板
     local claims = mw.wikibase.getBestStatements(entityId, 'P18')
     summary = mw.ustring.gsub(summary, "%[%[([^%]]-)|?([^%]]-)%]%]", "%2") -- 提取链接文字
     if claims[1] and claims[1].mainsnak.datavalue then
   
         image = claims[1].mainsnak.datavalue.value
    -- 寻找第一个非空的段落
     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
     end


     -- 3. 构建简易 HTML
     -- 3. 输出 UI
     local container = mw.html.create('div')
     local container = mw.html.create('div'):cssText('display:flex; gap:10px; border:1px solid #aaa; padding:10px;')
        :cssText('display: flex; gap: 10px; border: 1px solid #ccc; padding: 10px; border-radius: 4px;')
   
     if image ~= "" then
     if image ~= "" then
         container:tag('div'):wikitext(string.format('[[File:%s|80px]]', image))
         container:tag('div'):wikitext(string.format('[[File:%s|100px]]', image))
     end
     end
      
     container:tag('div'):tag('b'):wikitext(pageTitle):done():tag('p'):wikitext(firstPara .. "...")
    local text = container:tag('div')
    text:tag('b'):wikitext(pageTitle):done()
    text:tag('p'):css('margin', '0'):wikitext(description)


     return tostring(container)
     return tostring(container)
end
end


-- 关键:确保导出名称完全一致
return p
return p

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