Module:String: Difference between revisions
Content added Content deleted
Enwikipedia>WOSlinker (replace_plain fix) |
Enwikipedia>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 |
|||
replace_plain |
|||
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. |
|||
firstonly: Boolean flag indicating that only the first occurence found should be replaced |
|||
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; |
|||
firstonly = firstonly:lower(); |
|||
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 |
|||
local pattern_plain = mw.ustring.gsub(pattern, '%%', '%%%%'); |
|||
pattern = str._escapePattern( pattern ); |
|||
local replace_plain = mw.ustring.gsub(replace, '%%', '%%%%'); |
|||
replace = str._escapePattern( replace ); |
|||
⚫ | |||
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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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 |