Module:LinkWrap

From Trans People Together Wiki

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