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
Tag: Reverted
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Trims whitespace from a string
-- Trim leading/trailing whitespace
local function trim(s)
local function trim(s)
return s:match("^%s*(.-)%s*$")
return s:match("^%s*(.-)%s*$")
Line 14: Line 14:
end
end


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


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


local text = content:getContent()
-- Use MediaWiki parser to preprocess and expand the page content
if not text then
local expanded = frame:preprocess('{{:' .. pageText .. '}}')
if not expanded then
return "No content."
return "No content."
end
end
 
-- Strip templates and formatting
-- Attempt to skip content before the first section (e.g., infobox/template blob)
text = mw.text.killMarkers(text)
local body = mw.ustring.match(expanded, "==.*") or expanded
text = mw.ustring.gsub(text, '{{.-}}', '')                 -- remove templates
 
text = mw.ustring.gsub(text, '%[%[.-%|', '')               -- remove alt text part of links
-- Clean up formatting
text = mw.ustring.gsub(text, '%[%[', '')                   -- remove remaining [[
body = mw.text.killMarkers(body)
text = mw.ustring.gsub(text, '%]%]', '')                   -- remove ]]
body = mw.ustring.gsub(body, '%[%[(.-)%|(.-)%]%]', '%2') -- [[Page|Label]] → Label
text = mw.ustring.gsub(text, "''+", "")                   -- remove bold/italics
body = mw.ustring.gsub(body, '%[%[', '')                 -- [[Page]] → Page
text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "")         -- remove headers
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
-- Normalize whitespace
text = text:gsub("\n", " ")
body = body:gsub("\n", " ")
text = text:gsub("%s+", " ")
body = body:gsub("%s+", " ")
text = trim(text)
body = trim(body)


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


-- Truncate if needed
local truncated = mw.ustring.sub(body, 1, length)
local truncated = text
if #body > length then
local addEllipsis = false
truncated = truncated .. "..."
if #text > length then
truncated = mw.ustring.sub(text, 1, length)
addEllipsis = true
end
end
local link = string.format(" [[%s|Read more]]", pageText)
local finalText = truncated .. (addEllipsis and "..." or "") .. link
return string.format(
  '%s<span class="read-more"> [[%s|Read more]]</span>',
  mw.ustring.sub(text, 1, length),
  pageText
)


return truncated
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