Module:String2: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(add simple trim function) |
(update from sandbox - merge in strip function from module:stringfunc) |
||
Line 214: | Line 214: | ||
end |
end |
||
-- what follows was merged from Module:StringFunc |
|||
-- helper functions |
|||
p._GetParameters = require('Module:GetParameters') |
|||
-- Argument list helper function, as per Module:String |
|||
p._getParameters = p._GetParameters.getParameters |
|||
-- Escape Pattern helper function so that all characters are treated as plain text, as per Module:String |
|||
function p._escapePattern( pattern_str) |
|||
return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ); |
|||
end |
|||
-- Helper Function to interpret boolean strings, as per Module:String |
|||
p._getBoolean = p._GetParameters.getBoolean |
|||
--[[ |
|||
Strip |
|||
This function Strips characters from string |
|||
Usage: |
|||
{{#invoke:String2|strip|source_string|characters_to_strip|plain_flag}} |
|||
Parameters |
|||
source: The string to strip |
|||
chars: The pattern or list of characters to strip from string, replaced with '' |
|||
plain: A flag indicating that the chars should be understood as plain text. defaults to true. |
|||
Leading and trailing whitespace is also automatically stripped from the string. |
|||
]] |
|||
function p.strip( frame ) |
|||
local new_args = p._getParameters( frame.args, {'source', 'chars', 'plain'} ) |
|||
local source_str = new_args['source'] or ''; |
|||
local chars = new_args['chars'] or '' or 'characters'; |
|||
source_str = mw.text.trim(source_str); |
|||
if source_str == '' or chars == '' then |
|||
return source_str; |
|||
end |
|||
local l_plain = p._getBoolean( new_args['plain'] or true ); |
|||
if l_plain then |
|||
chars = p._escapePattern( chars ); |
|||
end |
|||
local result; |
|||
result = mw.ustring.gsub(source_str, "["..chars.."]", '') |
|||
return result; |
|||
end |
|||
return p |
return p |