More actions
Created page with "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 content = mw.title.new(page) if not content or not content.exists then return "Page not found." end local text = content:getContent() if not text then..." |
No edit summary |
||
Line 14: | Line 14: | ||
end | end | ||
local title = mw.title.new(page) | |||
local | if not title or not title.exists then | ||
if not | return "Page not found: " .. page | ||
return "Page not found. | |||
end | end | ||
local text = | local text = title:getContent() | ||
if not text then | if not text then | ||
return "No content. | return "No content for page: " .. page | ||
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, '{{.-}}', '') | ||
text = mw.ustring.gsub(text, '%[%[.-%|', '') | text = mw.ustring.gsub(text, '%[%[.-%|', '') | ||
text = mw.ustring.gsub(text, '%[%[', '') | text = mw.ustring.gsub(text, '%[%[', '') | ||
text = mw.ustring.gsub(text, '%]%]', '') | text = mw.ustring.gsub(text, '%]%]', '') | ||
text = mw.ustring.gsub(text, "''+", "") | text = mw.ustring.gsub(text, "''+", "") | ||
text = text:gsub("\n", " ") | text = text:gsub("\n", " ") | ||
text = text:gsub("%s+", " ") | text = text:gsub("%s+", " ") | ||
text = trim(text) | text = trim(text) | ||
if #text > length then | if #text > length then | ||
return mw.ustring.sub(text, 1, length) .. "..." | return mw.ustring.sub(text, 1, length) .. "..." |
Revision as of 00:56, 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
local title = mw.title.new(page)
if not title or not title.exists then
return "Page not found: " .. page
end
local text = title:getContent()
if not text then
return "No content for page: " .. page
end
-- Strip templates and formatting
text = mw.text.killMarkers(text)
text = mw.ustring.gsub(text, '{{.-}}', '')
text = mw.ustring.gsub(text, '%[%[.-%|', '')
text = mw.ustring.gsub(text, '%[%[', '')
text = mw.ustring.gsub(text, '%]%]', '')
text = mw.ustring.gsub(text, "''+", "")
text = text:gsub("\n", " ")
text = text:gsub("%s+", " ")
text = trim(text)
if #text > length then
return mw.ustring.sub(text, 1, length) .. "..."
else
return text
end
end
return p