Модул:MonthReplace

Iz Vojne Enciklopedije
Пређи на навигацију Пређи на претрагу

Документацију овог модула можете да направите на страници Модул:MonthReplace/док

-- Module for different search and replace operations on strings.
local p = {}

-- Takes one string parameter, and returns the string with all characters with special meaning for Lua patterns escaped with a preceding `%`.
function p.escape_pattern(text)
    -- Replaces each occurrence of any of ().%+-*?[^$ with a `%` and then the character.
    local r = mw.ustring.gsub(text, "[%(%)%.%%%+%-%*%?%[%^%$]", "%%%1")
    return r
end

-- Returns the first parameter, with all occurrences of the second parameter replaced with the third parameter.
-- All special characters are ignored: {{#invoke:StringReplace|replace_all|test.a%1$foo|%1|bar}} results in `test.abarfoo`.
function p.replace_all(frame)
    local str = frame.args[1]
    local strToFind = frame.args[2]
    local strToreplaceWith = frame.args[3]
    local r = mw.ustring.gsub(str, p.escape_pattern(strToFind), p.escape_pattern(strToreplaceWith))
    return r
end

p['convert months into English'] = function( frame )
	local x = ''
	x = mw.ustring.gsub( 
		frame.args[1] or '', 
		'јануар', 
		{ 
			['јануар'] = 'January',
		} 
	)
	x = mw.ustring.gsub(x, 
		'фебруар', 
		{ 
			['фебруар'] = 'February',
		} 
	)
	x = mw.ustring.gsub(x, 
		'март', 
		{ 
			['март'] = 'March',
		} 
	)
	x = mw.ustring.gsub(x, 
		'април', 
		{ 
			['април'] = 'April',
		} 
	)
	x = mw.ustring.gsub(x, 
		'мај', 
		{ 
			['мај'] = 'May',
		} 
	)
	x = mw.ustring.gsub(x, 
		'јун', 
		{ 
			['јун'] = 'June',
		} 
	)
	x = mw.ustring.gsub(x, 
		'јул', 
		{ 
			['јул'] = 'July',
		} 
	)
	x = mw.ustring.gsub(x, 
		'август', 
		{ 
			['август'] = 'August',
		} 
	)
	x = mw.ustring.gsub(x, 
		'септембар', 
		{ 
			['септембар'] = 'September',
		} 
	)
	x = mw.ustring.gsub(x, 
		'октобар', 
		{ 
			['октобар'] = 'October',
		} 
	)
	x = mw.ustring.gsub(x, 
		'новембар', 
		{ 
			['новембар'] = 'November',
		} 
	)
	x = mw.ustring.gsub(x, 
		'децембар', 
		{ 
			['децембар'] = 'December',
		} 
	)
	return mw.text.trim( x )
end

p['convert months into Serbian'] = function( frame )
	local y = ''
	y = mw.ustring.gsub( 
		frame.args[1] or '', 
		'January', 
		{ 
			['January'] = 'јануар',
		} 
	)
	y = mw.ustring.gsub(y, 
		'February', 
		{ 
			['February'] = 'фебруар',
		} 
	)
	y = mw.ustring.gsub(y, 
		'March', 
		{ 
			['March'] = 'март',
		} 
	)
	y = mw.ustring.gsub(y, 
		'April', 
		{ 
			['April'] = 'април',
		} 
	)
	y = mw.ustring.gsub(y, 
		'May', 
		{ 
			['May'] = 'мај',
		} 
	)
	y = mw.ustring.gsub(y, 
		'June', 
		{ 
			['June'] = 'јун',
		} 
	)
	y = mw.ustring.gsub(y, 
		'July', 
		{ 
			['July'] = 'јул',
		} 
	)
	y = mw.ustring.gsub(y, 
		'August', 
		{ 
			['August'] = 'август',
		} 
	)
	y = mw.ustring.gsub(y, 
		'September', 
		{ 
			['September'] = 'септембар',
		} 
	)
	y = mw.ustring.gsub(y, 
		'October', 
		{ 
			['October'] = 'октобар',
		} 
	)
	y = mw.ustring.gsub(y, 
		'November', 
		{ 
			['November'] = 'новембар',
		} 
	)
	y = mw.ustring.gsub(y, 
		'December', 
		{ 
			['December'] = 'децембар',
		} 
	)
	return mw.text.trim( y )
end

return p