Module:LinkWrap

From Trans People Together Wiki
Revision as of 18:34, 8 May 2025 by Admin (talk | contribs) (Created page with "-- Module:LinkWrap local p = {} -- wrap: -- • If the input is blank: return empty string. -- • If there are no semicolons: trim and return "". -- • If there are semicolons: split on ";", trim each segment, -- wrap each in "" and join with ", ". function p.wrap(frame) local input = frame.args[1] or '' -- blank? if input == '' then return '' end -- helper to trim whitespace local function trim(s) return mw...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:LinkWrap/doc

-- Module:LinkWrap
local p = {}

-- wrap:
--  • If the input is blank: return empty string.
--  • If there are no semicolons: trim and return "[[…]]".
--  • If there are semicolons: split on ";", trim each segment,
--    wrap each in "[[…]]" and join with ", ".
function p.wrap(frame)
    local input = frame.args[1] or ''
    -- blank?
    if input == '' then
        return ''
    end

    -- helper to trim whitespace
    local function trim(s)
        return mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1')
    end

    -- if semicolons present, split and wrap each
    if mw.ustring.find(input, ';', 1, true) then
        local parts = mw.text.split(input, ';', true)
        for i, part in ipairs(parts) do
            parts[i] = '[[' .. trim(part) .. ']]'
        end
        return table.concat(parts, ', ')
    else
        -- single segment
        return '[[' .. trim(input) .. ']]'
    end
end

return p