More actions
No edit summary |
No edit summary |
||
Line 31: | Line 31: | ||
text = mw.text.killMarkers(text) | text = mw.text.killMarkers(text) | ||
-- Remove | -- Remove any template that starts with {{Infobox ...}} (supports multiline) | ||
text = mw.ustring.gsub(text, "{{Infobox.-}}", "") | |||
text = mw.ustring.gsub(text, " | |||
-- Remove any remaining inline templates | -- Remove any remaining inline templates | ||
text = mw.ustring.gsub(text, "{{.-}}", "") | text = mw.ustring.gsub(text, "{{.-}}", "") | ||
-- | -- Clean up link alt text (e.g., [[Page|Text]] → Text) | ||
text = mw.ustring.gsub(text, "%[%[(.-)%|(.-)%]%]", "%2") | text = mw.ustring.gsub(text, "%[%[(.-)%|(.-)%]%]", "%2") | ||
-- | -- Remove remaining link brackets | ||
text = mw.ustring.gsub(text, "%[%[", "") | text = mw.ustring.gsub(text, "%[%[", "") | ||
text = mw.ustring.gsub(text, "%]%]", "") | text = mw.ustring.gsub(text, "%]%]", "") | ||
Line 48: | Line 47: | ||
text = mw.ustring.gsub(text, "''+", "") | text = mw.ustring.gsub(text, "''+", "") | ||
-- Remove | -- Remove headers (e.g., == Header ==) | ||
text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "") | text = mw.ustring.gsub(text, "=+%s*(.-)%s*=+", "") | ||
Line 61: | Line 60: | ||
end | end | ||
-- Truncate to | -- Truncate to desired length | ||
local truncated = mw.ustring.sub(text, 1, length) | local truncated = mw.ustring.sub(text, 1, length) | ||
if #text > length then | if #text > length then |
Revision as of 16:59, 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 any template that starts with {{Infobox ...}} (supports multiline)
text = mw.ustring.gsub(text, "{{Infobox.-}}", "")
-- Remove any remaining inline templates
text = mw.ustring.gsub(text, "{{.-}}", "")
-- Clean up link alt text (e.g., [[Page|Text]] → Text)
text = mw.ustring.gsub(text, "%[%[(.-)%|(.-)%]%]", "%2")
-- Remove remaining link brackets
text = mw.ustring.gsub(text, "%[%[", "")
text = mw.ustring.gsub(text, "%]%]", "")
-- Remove bold/italics
text = mw.ustring.gsub(text, "''+", "")
-- Remove 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 desired length
local truncated = mw.ustring.sub(text, 1, length)
if #text > length then
truncated = truncated .. "..."
end
return truncated
end
return p