45,645
edits
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local export = {} | local export = {} | ||
function pattern_escape(pattern_str) | |||
local invoked = false | |||
if type(pattern_str) == "table" then | |||
if pattern_str.args then | |||
local frame = pattern_str | |||
invoked = true | |||
if frame.args[1] then | |||
pattern_str = frame.args[1] | |||
else | |||
pattern_str = frame:getParent().args[1] | |||
end | |||
else | |||
error("First argument to pattern_escape should be a string, a number, or a frame object.") | |||
end | |||
elseif not (type(pattern_str) == "string" or type(pattern_str) == "number") then | |||
error("First argument to pattern_escape should be a string or a number.") | |||
end | |||
if invoked then | |||
local escaped = mw.ustring.gsub(pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1") | |||
return escaped | |||
else | |||
return mw.ustring.gsub(pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1"); | |||
end | |||
end | |||
-- A helper function to escape magic characters in a string | -- A helper function to escape magic characters in a string | ||
-- Magic characters: ^$()%.[]*+-? | -- Magic characters: ^$()%.[]*+-? | ||
local plain = | local plain = pattern_escape | ||
-- A helper function that removes empty numeric indexes in a table, | -- A helper function that removes empty numeric indexes in a table, |