Module:RemoveCountySuffix

From Trans People Together Wiki

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

-- Module:Replace
local p = {}

function p.replace(frame)
    local text  = frame.args[1] or ""
    local state = frame.args[2] or ""
    -- escape any magic characters in state name for Lua patterns:
    local escState = state:gsub("([^%w])", "%%%1")
    -- pattern to strip “ County, State” (case-insensitive on “County”)
    local suffixPatt = "%s*[Cc]ounty%s*,%s*" .. escState

    -- replace every [[target|label]] with [[target|newLabel]]
    local out = mw.ustring.gsub(text,
        "%[%[([^%]]+)%|([^%]]+)%]%]",
        function(target, label)
            local newLabel = mw.ustring.gsub(label, suffixPatt, "")
            return "[[" .. target .. "|" .. newLabel .. "]]"
        end
    )

    return out
end

return p