Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:SummarySnippet: Difference between revisions

From The Fortune Bringers
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
-- Trims whitespace from a string
local function trim(s)
return s:match("^%s*(.-)%s*$")
end


function p.fromPage(frame)
function p.fromPage(frame)
local page = frame.args[1]
    local input = frame.args[1] or ""
local length = tonumber(frame.args[2]) or 150
   
 
    -- Extract and clean up the title from [[Page]] or [[Page|Display]]
if not page or page == '' then
    local raw = mw.ustring.match(input, "%[%[(.-)%]%]") or input
return "No page name provided."
    local titleText = mw.text.split(raw, "|")[1]
end
   
 
    -- Resolve title object
local title = mw.title.new(page)
    local title = mw.title.new(titleText)
if not title or not title.exists then
   
return "Page parameter: " .. tostring(page)
    if not title then
end
        return "Invalid page title: " .. titleText
 
    end
local text = title:getContent()
if not text then
return "No content for page: " .. page
end
 
-- Strip templates and formatting
text = mw.text.killMarkers(text)
text = mw.ustring.gsub(text, '{{.-}}', '')
text = mw.ustring.gsub(text, '%[%[.-%|', '')
text = mw.ustring.gsub(text, '%[%[', '')
text = mw.ustring.gsub(text, '%]%]', '')
text = mw.ustring.gsub(text, "''+", "")
text = text:gsub("\n", " ")
text = text:gsub("%s+", " ")
text = trim(text)


if #text > length then
    -- Get content and return preview if it exists
return mw.ustring.sub(text, 1, length) .. "..."
    if title.exists then
else
        local content = title:getContent()
return text
        if content then
end
            local preview = mw.ustring.sub(content, 1, 150)
            return preview .. "..."
        else
            return "No content on: " .. title.fullText
        end
    else
        return "Page does not exist: " .. titleText
    end
end
end


return p
return p

Revision as of 01:05, 25 May 2025

Documentation for this module may be created at Module:SummarySnippet/doc

local p = {}

function p.fromPage(frame)
    local input = frame.args[1] or ""
    
    -- Extract and clean up the title from [[Page]] or [[Page|Display]]
    local raw = mw.ustring.match(input, "%[%[(.-)%]%]") or input
    local titleText = mw.text.split(raw, "|")[1]
    
    -- Resolve title object
    local title = mw.title.new(titleText)
    
    if not title then
        return "Invalid page title: " .. titleText
    end

    -- Get content and return preview if it exists
    if title.exists then
        local content = title:getContent()
        if content then
            local preview = mw.ustring.sub(content, 1, 150)
            return preview .. "..."
        else
            return "No content on: " .. title.fullText
        end
    else
        return "Page does not exist: " .. titleText
    end
end

return p