More actions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Trims whitespace from a string | |||
local function trim(s) | |||
return s:match("^%s*(.-)%s*$") | |||
end | |||
function p.fromPage(frame) | function p.fromPage(frame) | ||
local page = frame.args[1] | |||
local length = tonumber(frame.args[2]) or 150 | |||
local raw = mw.ustring.match( | if not page or page == '' then | ||
local | return "No page name provided." | ||
end | |||
-- Fetch the raw wikitext | |||
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." | |||
end | |||
local text = content:getContent() | |||
if not text then | |||
return "No content." | |||
end | |||
-- Strip templates and most formatting (basic) | |||
text = mw.text.killMarkers(text) | |||
text = mw.ustring.gsub(text, '{{.-}}', '') -- remove templates | |||
text = mw.ustring.gsub(text, '%[%[.-%|', '') -- remove link alt text | |||
text = mw.ustring.gsub(text, '%[%[', '') -- remove double bracket | |||
text = mw.ustring.gsub(text, '%]%]', '') -- remove double bracket | |||
text = mw.ustring.gsub(text, "''+", "") -- remove italics/bold | |||
-- Collapse whitespace | |||
text = text:gsub("\n", " ") | |||
text = text:gsub("%s+", " ") | |||
text = trim(text) | |||
-- Return truncated string | |||
if #text > length then | |||
return mw.ustring.sub(text, 1, length) .. "..." | |||
else | |||
return text | |||
end | |||
end | end | ||
return p | return p |
Revision as of 01:09, 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
-- Fetch the raw wikitext
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."
end
local text = content:getContent()
if not text then
return "No content."
end
-- Strip templates and most formatting (basic)
text = mw.text.killMarkers(text)
text = mw.ustring.gsub(text, '{{.-}}', '') -- remove templates
text = mw.ustring.gsub(text, '%[%[.-%|', '') -- remove link alt text
text = mw.ustring.gsub(text, '%[%[', '') -- remove double bracket
text = mw.ustring.gsub(text, '%]%]', '') -- remove double bracket
text = mw.ustring.gsub(text, "''+", "") -- remove italics/bold
-- Collapse whitespace
text = text:gsub("\n", " ")
text = text:gsub("%s+", " ")
text = trim(text)
-- Return truncated string
if #text > length then
return mw.ustring.sub(text, 1, length) .. "..."
else
return text
end
end
return p