Module:TruncDesc: Difference between revisions
From TwogPedia
Created page with "local p = {} function p.get(frame) local manual = frame.args.seo_description or "" local content = frame.args.content or "" local text = (manual ~= "") and manual or content local subbed = mw.ustring.sub(text, 1, 160) subbed = mw.ustring.gsub(subbed, "%s+", " ") return mw.text.trim(subbed) end return p" |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p. | function p.generate(frame) | ||
local manual = frame.args.seo_description or "" | local manual = frame.args.seo_description or "" | ||
local content = frame.args.content or "" | local content = frame.args.content or "" | ||
local text = (manual ~= "") and manual or content | local text = (manual ~= "") and manual or content | ||
local | |||
text = mw.ustring.gsub(text, "%s+", " ") | |||
return mw.text.trim( | text = mw.text.trim(text) | ||
local max = 155 | |||
if mw.ustring.len(text) <= max then | |||
return text | |||
end | |||
local truncated = mw.ustring.sub(text, 1, max) | |||
local lastDot = mw.ustring.find(truncated, "[%.%?!][%s]") or mw.ustring.find(truncated, "[%.%?!]$") | |||
if lastDot then | |||
truncated = mw.ustring.sub(truncated, 1, lastDot) | |||
else | |||
local lastSpace = mw.ustring.find(truncated, " ", -1, true) | |||
if lastSpace then | |||
truncated = mw.ustring.sub(truncated, 1, lastSpace - 1) | |||
end | |||
end | |||
return mw.text.trim(truncated) .. (mw.ustring.len(text) > max and "..." or "") | |||
end | end | ||
return p | return p | ||
Revision as of 08:00, 15 January 2026
Documentation for this module may be created at Module:TruncDesc/doc
local p = {}
function p.generate(frame)
local manual = frame.args.seo_description or ""
local content = frame.args.content or ""
local text = (manual ~= "") and manual or content
text = mw.ustring.gsub(text, "%s+", " ")
text = mw.text.trim(text)
local max = 155
if mw.ustring.len(text) <= max then
return text
end
local truncated = mw.ustring.sub(text, 1, max)
local lastDot = mw.ustring.find(truncated, "[%.%?!][%s]") or mw.ustring.find(truncated, "[%.%?!]$")
if lastDot then
truncated = mw.ustring.sub(truncated, 1, lastDot)
else
local lastSpace = mw.ustring.find(truncated, " ", -1, true)
if lastSpace then
truncated = mw.ustring.sub(truncated, 1, lastSpace - 1)
end
end
return mw.text.trim(truncated) .. (mw.ustring.len(text) > max and "..." or "")
end
return p
No categories