Module:String: Difference between revisions
include more generic replacement function
m>WOSlinker (replace_plain fix) |
m>Dragons flight (include more generic replacement function) |
||
Line 127: | Line 127: | ||
end | end | ||
plain = str._getBoolean( plain ); | |||
local start = mw.ustring.find( source_str, pattern, start_pos, plain ) | local start = mw.ustring.find( source_str, pattern, start_pos, plain ) | ||
Line 145: | Line 138: | ||
--[====[ | --[====[ | ||
replace | |||
This function allows one to replace a target string or pattern within another | This function allows one to replace a target string or pattern within another | ||
Line 151: | Line 144: | ||
Usage: | Usage: | ||
{{#invoke:String|replace_plain|source_str|pattern_string|replace_string| | {{#invoke:String|replace_plain|source_str|pattern_string|replace_string|replacement_count|pattern_flag}} | ||
OR | OR | ||
{{#invoke:String|replace_plain|source=source_str|pattern=pattern_str|replace=replace_string| | {{#invoke:String|replace_plain|source=source_str|pattern=pattern_str|replace=replace_string| | ||
count=replacement_count|plain=pattern_flag}} | |||
Parameters | Parameters | ||
source: The string to search | source: The string to search | ||
pattern: The string or pattern to find within source | |||
replace: The replacement text | replace: The replacement text | ||
count: The number of occurences to replace, defaults to all. | |||
plain: Boolean flag indicating that pattern should be understood as plain | |||
text and not as a Lua style regular expression, defaults to true | |||
]====] | ]====] | ||
function str. | function str.replace( frame ) | ||
local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', ' | local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } ); | ||
local source_str = new_args['source'] or ''; | local source_str = new_args['source'] or ''; | ||
local pattern = new_args['pattern'] or ''; | local pattern = new_args['pattern'] or ''; | ||
local replace = new_args['replace'] or ''; | local replace = new_args['replace'] or ''; | ||
local | local count = tonumber( new_args['count'] ); | ||
local plain = new_args['plain'] or true; | |||
if source_str == '' or pattern == '' then | if source_str == '' or pattern == '' then | ||
return source_str; | return source_str; | ||
end | end | ||
plain = str._getBoolean( plain ); | |||
if plain then | |||
pattern = str._escapePattern( pattern ); | |||
replace = str._escapePattern( replace ); | |||
end | |||
local result; | local result; | ||
if | if count ~= nil then | ||
result = mw.ustring.gsub( source_str, | result = mw.ustring.gsub( source_str, pattern, replace, count ); | ||
else | else | ||
result = mw.ustring.gsub( source_str, | result = mw.ustring.gsub( source_str, pattern, replace ); | ||
end | end | ||
return result; | return result; | ||
Line 208: | Line 208: | ||
return new_args; | return new_args; | ||
end | end | ||
--[====[ | |||
Helper Function to interpret boolean strings | |||
]====] | |||
function str._getBoolean( boolean_str ) | |||
local boolean_value; | |||
if type( boolean_str ) == 'string' then | |||
boolean_str = boolean_str:lower(); | |||
if boolean_str == 'false' or boolean_str == 'no' or boolean_str == '0' then | |||
boolean_value = false; | |||
else | |||
boolean_value = true; | |||
end | |||
elseif type( boolean_str ) == 'boolean' then | |||
boolean_value = boolean_str; | |||
else | |||
error( 'No boolean value found' ); | |||
end | |||
return boolean_value | |||
end | |||
--[====[ | |||
Helper function that escapes all pattern characters so that they will be treated | |||
as plain text. | |||
]====] | |||
function str._escapePattern( pattern_str ) | |||
return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ); | |||
end | |||
return str | return str |