Пређи на садржај

Модул:TranslTags39LatReplace

С Википедије, слободне енциклопедије

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

-- 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['remove'] = function( frame )
	local x = ''
	x = mw.ustring.gsub( 
		frame.args[1] or '', 
		'%-%{', 
		{ 
			['-{'] = '',
		} 
	)
	x = mw.ustring.gsub(x, 
		'%}%-', 
		{ 
			['}-'] = '',
		} 
	)
	x = mw.ustring.gsub(x, 
		'\'', 
		{ 
			['\''] = '',
		} 
	)
	x = mw.ustring.gsub(x, 
		'А', 
		{ 
			['А'] = 'A',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Б', 
		{ 
			['Б'] = 'B',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ц', 
		{ 
			['Ц'] = 'C',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ч', 
		{ 
			['Ч'] = 'Č',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ћ', 
		{ 
			['Ћ'] = 'Ć',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Д', 
		{ 
			['Д'] = 'D',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Џ', 
		{ 
			['Џ'] = 'Dž',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ђ', 
		{ 
			['Ђ'] = 'Đ',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Е', 
		{ 
			['Е'] = 'E',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ф', 
		{ 
			['Ф'] = 'F',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Г', 
		{ 
			['Г'] = 'G',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Х', 
		{ 
			['Х'] = 'H',
		} 
	)
	x = mw.ustring.gsub(x, 
		'И', 
		{ 
			['И'] = 'I',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ј', 
		{ 
			['Ј'] = 'J',
		} 
	)
	x = mw.ustring.gsub(x, 
		'К', 
		{ 
			['К'] = 'K',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Л', 
		{ 
			['Л'] = 'L',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Љ', 
		{ 
			['Љ'] = 'Lj',
		} 
	)
	x = mw.ustring.gsub(x, 
		'М', 
		{ 
			['М'] = 'M',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Н', 
		{ 
			['Н'] = 'N',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Њ', 
		{ 
			['Њ'] = 'Nj',
		} 
	)
	x = mw.ustring.gsub(x, 
		'О', 
		{ 
			['О'] = 'O',
		} 
	)
	x = mw.ustring.gsub(x, 
		'П', 
		{ 
			['П'] = 'P',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Р', 
		{ 
			['Р'] = 'R',
		} 
	)
	x = mw.ustring.gsub(x, 
		'С', 
		{ 
			['С'] = 'S',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ш', 
		{ 
			['Ш'] = 'Š',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Т', 
		{ 
			['Т'] = 'T',
		} 
	)
	x = mw.ustring.gsub(x, 
		'У', 
		{ 
			['У'] = 'U',
		} 
	)
	x = mw.ustring.gsub(x, 
		'В', 
		{ 
			['В'] = 'V',
		} 
	)
	x = mw.ustring.gsub(x, 
		'З', 
		{ 
			['З'] = 'Z',
		} 
	)
	x = mw.ustring.gsub(x, 
		'Ж', 
		{ 
			['Ж'] = 'Ž',
		} 
	)
	x = mw.ustring.gsub(x, 
		'а', 
		{ 
			['а'] = 'a',
		} 
	)
	x = mw.ustring.gsub(x, 
		'б', 
		{ 
			['б'] = 'b',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ц', 
		{ 
			['ц'] = 'c',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ч', 
		{ 
			['ч'] = 'č',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ћ', 
		{ 
			['ћ'] = 'ć',
		} 
	)
	x = mw.ustring.gsub(x, 
		'д', 
		{ 
			['д'] = 'd',
		} 
	)
	x = mw.ustring.gsub(x, 
		'џ', 
		{ 
			['џ'] = 'dž',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ђ', 
		{ 
			['ђ'] = 'đ',
		} 
	)
	x = mw.ustring.gsub(x, 
		'е', 
		{ 
			['е'] = 'e',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ф', 
		{ 
			['ф'] = 'f',
		} 
	)
	x = mw.ustring.gsub(x, 
		'г', 
		{ 
			['г'] = 'g',
		} 
	)
	x = mw.ustring.gsub(x, 
		'х', 
		{ 
			['х'] = 'h',
		} 
	)
	x = mw.ustring.gsub(x, 
		'и', 
		{ 
			['и'] = 'i',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ј', 
		{ 
			['ј'] = 'j',
		} 
	)
	x = mw.ustring.gsub(x, 
		'к', 
		{ 
			['к'] = 'k',
		} 
	)
	x = mw.ustring.gsub(x, 
		'л', 
		{ 
			['л'] = 'l',
		} 
	)
	x = mw.ustring.gsub(x, 
		'љ', 
		{ 
			['љ'] = 'lj',
		} 
	)
	x = mw.ustring.gsub(x, 
		'м', 
		{ 
			['м'] = 'm',
		} 
	)
	x = mw.ustring.gsub(x, 
		'н', 
		{ 
			['н'] = 'n',
		} 
	)
	x = mw.ustring.gsub(x, 
		'њ', 
		{ 
			['њ'] = 'nj',
		} 
	)
	x = mw.ustring.gsub(x, 
		'о', 
		{ 
			['о'] = 'o',
		} 
	)
	x = mw.ustring.gsub(x, 
		'п', 
		{ 
			['п'] = 'p',
		} 
	)
	x = mw.ustring.gsub(x, 
		'р', 
		{ 
			['р'] = 'r',
		} 
	)
	x = mw.ustring.gsub(x, 
		'с', 
		{ 
			['с'] = 's',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ш', 
		{ 
			['ш'] = 'š',
		} 
	)
	x = mw.ustring.gsub(x, 
		'т', 
		{ 
			['т'] = 't',
		} 
	)
	x = mw.ustring.gsub(x, 
		'у', 
		{ 
			['у'] = 'u',
		} 
	)
	x = mw.ustring.gsub(x, 
		'в', 
		{ 
			['в'] = 'v',
		} 
	)
	x = mw.ustring.gsub(x, 
		'з', 
		{ 
			['з'] = 'z',
		} 
	)
	x = mw.ustring.gsub(x, 
		'ж', 
		{ 
			['ж'] = 'ž',
		} 
	)
	return mw.text.trim( x )
end

return p