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 28: Line 28:
end
end
-- Strip templates and most formatting (basic)
-- Strip templates and formatting
text = mw.text.killMarkers(text)
text = mw.text.killMarkers(text)
text = mw.ustring.gsub(text, '{{.-}}', '') -- remove templates
text = mw.ustring.gsub(text, '{{.-}}', '')                 -- remove templates
text = mw.ustring.gsub(text, '%[%[.-%|', '') -- remove link alt text
text = mw.ustring.gsub(text, '%[%[.-%|', '')               -- remove alt text part of links
text = mw.ustring.gsub(text, '%[%[', '') -- remove opening brackets
text = mw.ustring.gsub(text, '%[%[', '')                   -- remove remaining [[
text = mw.ustring.gsub(text, '%]%]', '') -- remove closing brackets
text = mw.ustring.gsub(text, '%]%]', '')                   -- remove ]]
text = mw.ustring.gsub(text, "''+", "") -- remove italics/bold
text = mw.ustring.gsub(text, "''+", "")                   -- remove bold/italics
text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "") -- remove headers
text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "")         -- remove headers
 
-- Collapse whitespace
-- Normalize whitespace
text = text:gsub("\n", " ")
text = text:gsub("\n", " ")
text = text:gsub("%s+", " ")
text = text:gsub("%s+", " ")
text = trim(text)
text = trim(text)
 
-- Handle empty content after cleanup
-- Fallback if nothing left
if text == "" then
if text == "" then
return "No description available."
return '<span class="snippet-text">No description available.</span>'
end
end


-- Tag on a snippet-text class otherwise size gets overwritten and tack on a final read more
-- Truncate if needed
return string.format('<span class="snippet-text">%s...</span>', mw.text.nowiki(text))
local truncated = text
local addEllipsis = false
if #text > length then
truncated = mw.ustring.sub(text, 1, length)
addEllipsis = true
end


local link = string.format(" [[:%s|Read more]]", pageText)
local finalText = truncated .. (addEllipsis and "..." or "") .. link
return string.format('<span class="snippet-text">%s</span>', mw.text.nowiki(finalText))
end
end


return p
return p

Revision as of 01:53, 25 May 2025

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

local p = {}

-- Trims whitespace from a string
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 input: extract page name from link-style input if needed
	local raw = mw.ustring.match(page, "%[%[(.-)%]%]") or page
	local pageText = mw.text.split(raw, "|")[1]

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

	local text = content:getContent()
	if not text then
		return "No content."
	end
	
	-- Strip templates and formatting
	text = mw.text.killMarkers(text)
	text = mw.ustring.gsub(text, '{{.-}}', '')                  -- remove templates
	text = mw.ustring.gsub(text, '%[%[.-%|', '')               -- remove alt text part of links
	text = mw.ustring.gsub(text, '%[%[', '')                   -- remove remaining [[
	text = mw.ustring.gsub(text, '%]%]', '')                   -- remove ]]
	text = mw.ustring.gsub(text, "''+", "")                    -- remove bold/italics
	text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "")         -- remove headers

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

	-- Fallback if nothing left
	if text == "" then
		return '<span class="snippet-text">No description available.</span>'
	end

	-- Truncate if needed
	local truncated = text
	local addEllipsis = false
	if #text > length then
		truncated = mw.ustring.sub(text, 1, length)
		addEllipsis = true
	end

	local link = string.format(" [[:%s|Read more]]", pageText)
	local finalText = truncated .. (addEllipsis and "..." or "") .. link
	return string.format('<span class="snippet-text">%s</span>', mw.text.nowiki(finalText))
end

return p