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 27: Line 27:
return "No content."
return "No content."
end
end
 
-- Strip templates and formatting
-- Strip templates and formatting
-- Strip templates and formatting
text = mw.text.killMarkers(text)
text = mw.text.killMarkers(text)
-- Remove all top-of-page templates (Infoboxes etc.)
-- Match multiline blocks like {{Infobox Something\n...}} or {{AnyTemplate\n...}}
text = mw.ustring.gsub(text, '^%s*({{.-}})', function(tmpl)
  if mw.ustring.find(tmpl, '\n') then
    return ''
  else
    return tmpl
  end
end)


-- Remove any remaining inline templates (single-line or nested)
-- Remove top-of-page multi-line templates (e.g., infoboxes)
text = mw.ustring.gsub(text, '{{.-}}', '')
-- This looks for one or more templates at the top and removes them
text = mw.ustring.gsub(text, "^%s*({{.-}}\n*)+", "")
 
-- Remove any remaining inline templates
text = mw.ustring.gsub(text, "{{.-}}", "")


text = mw.ustring.gsub(text, '%[%[.-%|', '')               -- remove alt text part of links
-- Strip link alt text (e.g., [[Page|Text]])
text = mw.ustring.gsub(text, '%[%[', '')                   -- remove remaining [[
text = mw.ustring.gsub(text, "%[%[(.-)%|(.-)%]%]", "%2")
text = mw.ustring.gsub(text, '%]%]', '')                   -- remove ]]
 
text = mw.ustring.gsub(text, "''+", "")                   -- remove bold/italics
-- Strip remaining link brackets
text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "")         -- remove headers
text = mw.ustring.gsub(text, "%[%[", "")
text = mw.ustring.gsub(text, "%]%]", "")
 
-- Remove bold/italics
text = mw.ustring.gsub(text, "''+", "")
 
-- Remove section headers (e.g., == Header ==)
text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "")


-- Normalize whitespace
-- Normalize whitespace
Line 61: Line 61:
end
end


local truncated = mw.ustring.sub(text, 1, length)
-- Truncate to requested length
local truncated = mw.ustring.sub(text, 1, length)
if #text > length then
if #text > length then
truncated = truncated .. "..."
truncated = truncated .. "..."
end
end


return string.format(
return truncated
'%s',
truncated
)
 
end
end


return p
return p

Revision as of 16:56, 30 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)

	-- Remove top-of-page multi-line templates (e.g., infoboxes)
	-- This looks for one or more templates at the top and removes them
	text = mw.ustring.gsub(text, "^%s*({{.-}}\n*)+", "")

	-- Remove any remaining inline templates
	text = mw.ustring.gsub(text, "{{.-}}", "")

	-- Strip link alt text (e.g., [[Page|Text]])
	text = mw.ustring.gsub(text, "%[%[(.-)%|(.-)%]%]", "%2")

	-- Strip remaining link brackets
	text = mw.ustring.gsub(text, "%[%[", "")
	text = mw.ustring.gsub(text, "%]%]", "")

	-- Remove bold/italics
	text = mw.ustring.gsub(text, "''+", "")

	-- Remove section headers (e.g., == Header ==)
	text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "")

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

	-- Fallback if nothing left
	if text == "" then
		return 'No description available.'
	end

	-- Truncate to requested length
	local truncated = mw.ustring.sub(text, 1, length)
	if #text > length then
		truncated = truncated .. "..."
	end

	return truncated
end

return p