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
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
-- Trim leading/trailing whitespace
local function trim(s)
return s:match("^%s*(.-)%s*$")
end


function p.fromPage(frame)
function p.fromPage(frame)
    local input = frame.args[1] or ""
local page = frame.args[1]
   
local length = tonumber(frame.args[2]) or 150
    -- Extract and clean up the title from [[Page]] or [[Page|Display]]
 
    local raw = mw.ustring.match(input, "%[%[(.-)%]%]") or input
if not page or page == '' then
    local titleText = mw.text.split(raw, "|")[1]
return "No page name provided."
   
end
    -- Resolve title object
 
    local title = mw.title.new(titleText)
-- Parse link-style input
   
local raw = mw.ustring.match(page, "%[%[(.-)%]%]") or page
    if not title then
local pageText = mw.text.split(raw, "|")[1]
        return "Invalid page title: " .. titleText
 
    end
local title = mw.title.new(pageText)
if not title or not title.exists then
return "Page not found: " .. pageText
end
 
-- Use MediaWiki parser to preprocess and expand the page content
local expanded = frame:preprocess('{{:' .. pageText .. '}}')
if not expanded then
return "No content."
end
 
-- Attempt to skip content before the first section (e.g., infobox/template blob)
local body = mw.ustring.match(expanded, "==.*") or expanded
 
-- Clean up formatting
body = mw.text.killMarkers(body)
body = mw.ustring.gsub(body, '%[%[(.-)%|(.-)%]%]', '%2') -- [[Page|Label]] → Label
body = mw.ustring.gsub(body, '%[%[', '')                -- [[Page]] → Page
body = mw.ustring.gsub(body, '%]%]', '')
body = mw.ustring.gsub(body, "''+", "")                  -- remove bold/italics
body = mw.ustring.gsub(body, "=+%s*(.-)%s*=+", "")      -- remove section headers
body = mw.ustring.gsub(body, "{{.-}}", "")              -- remove remaining inline templates
 
-- Normalize whitespace
body = body:gsub("\n", " ")
body = body:gsub("%s+", " ")
body = trim(body)
 
if body == "" then
return "No description available."
end
 
local truncated = mw.ustring.sub(body, 1, length)
if #body > length then
truncated = truncated .. "..."
end


    -- Get content and return preview if it exists
return truncated
    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
end


return p
return p

Latest revision as of 17:02, 30 May 2025

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

local p = {}

-- Trim leading/trailing whitespace
local function trim(s)
	return s:match("^%s*(.-)%s*$")
end

function p.fromPage(frame)
	local page = frame.args[1]
	local length = tonumber(frame.args[2]) or 150

	if not page or page == '' then
		return "No page name provided."
	end

	-- Parse link-style input
	local raw = mw.ustring.match(page, "%[%[(.-)%]%]") or page
	local pageText = mw.text.split(raw, "|")[1]

	local title = mw.title.new(pageText)
	if not title or not title.exists then
		return "Page not found: " .. pageText
	end

	-- Use MediaWiki parser to preprocess and expand the page content
	local expanded = frame:preprocess('{{:' .. pageText .. '}}')
	if not expanded then
		return "No content."
	end

	-- Attempt to skip content before the first section (e.g., infobox/template blob)
	local body = mw.ustring.match(expanded, "==.*") or expanded

	-- Clean up formatting
	body = mw.text.killMarkers(body)
	body = mw.ustring.gsub(body, '%[%[(.-)%|(.-)%]%]', '%2') -- [[Page|Label]] → Label
	body = mw.ustring.gsub(body, '%[%[', '')                 -- [[Page]] → Page
	body = mw.ustring.gsub(body, '%]%]', '')
	body = mw.ustring.gsub(body, "''+", "")                  -- remove bold/italics
	body = mw.ustring.gsub(body, "=+%s*(.-)%s*=+", "")       -- remove section headers
	body = mw.ustring.gsub(body, "{{.-}}", "")               -- remove remaining inline templates

	-- Normalize whitespace
	body = body:gsub("\n", " ")
	body = body:gsub("%s+", " ")
	body = trim(body)

	if body == "" then
		return "No description available."
	end

	local truncated = mw.ustring.sub(body, 1, length)
	if #body > length then
		truncated = truncated .. "..."
	end

	return truncated
end

return p