Module:TruncDesc

From TwogPedia
Revision as of 08:00, 15 January 2026 by Admin (talk | contribs)

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