Module:RemoveCountySuffix: Difference between revisions

From Trans People Together Wiki
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:


function p.replace(frame)
function p.replace(frame)
     local str = frame.args[1]
     local text  = frame.args[1] or ""
     local pattern = ' County, ' .. frame.args[2]
     local state = frame.args[2] or ""
     local replacement = ''
    -- escape any magic characters in state name for Lua patterns:
     return mw.ustring.gsub(str, pattern, replacement)
    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
end


return p
return p

Latest revision as of 10:59, 14 May 2025

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