More actions
No edit summary |
No edit summary |
||
Line 14: | Line 14: | ||
end | 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) | local content = mw.title.new(pageText) | ||
if not content or not content.exists then | if not content or not content.exists then | ||
return "Page not found. | return "Page not found: " .. pageText | ||
end | end | ||
Line 28: | Line 28: | ||
end | end | ||
-- Strip templates and | -- Strip templates and formatting | ||
text = mw.text.killMarkers(text) | text = mw.text.killMarkers(text) | ||
text = mw.ustring.gsub(text, | text = mw.ustring.gsub(text, "{{.-}}", "") -- templates | ||
text = mw.ustring.gsub(text, | text = mw.ustring.gsub(text, "=%s*.-%s*=", "") -- headers | ||
text = mw.ustring.gsub(text, | text = mw.ustring.gsub(text, "%[%[(.-)|", "") -- remove alt text | ||
text = mw.ustring.gsub(text, | text = mw.ustring.gsub(text, "%[%[", "") -- remove brackets | ||
text = mw.ustring.gsub(text, " | text = mw.ustring.gsub(text, "%]%]", "") | ||
text = mw.ustring.gsub(text, "''+", "") -- bold/italic | |||
text = mw.ustring.gsub(text, " | |||
-- Collapse whitespace | -- Collapse whitespace | ||
Line 44: | Line 42: | ||
text = trim(text) | text = trim(text) | ||
-- | -- Handle empty content after cleanup | ||
if | if text == "" then | ||
return | return "No description available." | ||
end | end | ||
-- Truncate and return summary + read more | |||
local summary = (#text > length) and mw.ustring.sub(text, 1, length) .. "..." or text | |||
return string.format("%s [[%s|Read more]]", summary, pageText) | |||
end | end | ||
return p | return p |
Revision as of 01:16, 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, "{{.-}}", "") -- templates
text = mw.ustring.gsub(text, "=%s*.-%s*=", "") -- headers
text = mw.ustring.gsub(text, "%[%[(.-)|", "") -- remove alt text
text = mw.ustring.gsub(text, "%[%[", "") -- remove brackets
text = mw.ustring.gsub(text, "%]%]", "")
text = mw.ustring.gsub(text, "''+", "") -- bold/italic
-- Collapse whitespace
text = text:gsub("\n", " ")
text = text:gsub("%s+", " ")
text = trim(text)
-- Handle empty content after cleanup
if text == "" then
return "No description available."
end
-- Truncate and return summary + read more
local summary = (#text > length) and mw.ustring.sub(text, 1, length) .. "..." or text
return string.format("%s [[%s|Read more]]", summary, pageText)
end
return p