Module:PatternConv

From Gratisdata

Documentation for this module may be created at Module:PatternConv/doc

local str = {}

function str.replace2( frame )

	local new_args = str._getParameters( frame.args, { 'source', 'pattern', 'replace', 'count', 'plain' } )

	local result = str._replace2( new_args )
	result = mw.uri.encode( result, "PATH" )
	return result;

end

function str._replace2( args )

	local source_str = args.source or '';
	local pattern = args.pattern or '';
	local replace = args.replace or '';
	local count = tonumber( args.count );
	local plain = args.plain or true;

	if source_str == '' then
		return source_str;
	end	
	plain = str._getBoolean( plain );

	source_str = mw.text.unstripNoWiki( source_str )
	source_str = mw.ustring.gsub( source_str, "\\", "\\\\" )

	if plain then
		pattern = str._escapePattern( pattern );
		replace = mw.ustring.gsub( replace, "%%", "%%%%" ); --Only need to escape replacement sequences.
	end
	
	local result;

	if count ~= nil then
		result = mw.ustring.gsub( source_str, pattern, replace, count );
	else
		result = mw.ustring.gsub( source_str, pattern, replace );
	end		
	if (string.sub(result, 0, 1) == "^" and  string.sub(result, -1) == "$") then
		result = result
	else
		result = "^(" .. result .. ")$"
	end
	return result
end


function str._getParameters( frame_args, arg_list )
	local new_args = {};
	local index = 1;
	local value;
	
	for i,arg in ipairs( arg_list ) do
		value = frame_args[arg]
		if value == nil then
			value = frame_args[index];
			index = index + 1;
		end
		new_args[arg] = value;
	end
	
	return new_args;
end		

--[[
Helper function to handle error messages.
]]
function str._error( error_str )
	local frame = mw.getCurrentFrame();
	local error_category = frame.args.error_category or 'Errors reported by Module String';
	local ignore_errors = frame.args.ignore_errors or false;
	local no_category = frame.args.no_category or false;
	
	if str._getBoolean(ignore_errors) then
		return '';
	end
	
	local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>';
	if error_category ~= '' and not str._getBoolean( no_category ) then
		error_str = '[[Category:' .. error_category .. ']]' .. error_str;
	end		
	
	return error_str;
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' 
				or boolean_str == '' 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