Jump to content

Module:Wp/isv/Alphabet

From Wikimedia Incubator

Documentation for this module may be created at Module:Wp/isv/Alphabet/doc

--
-- Module adds an ability to convert Interslavic text between two alphabets
-- It should be used in templates and modules to add an ability
-- to use them in Cyrillic alphabet
--
local p = {}
local data = mw.loadData( 'Module:Wp/isv/Alphabet/data' )

-- Convert text between alphabets with care
local function convertText( text, dataTable )
	text = mw.text.decode( text )
	
	-- Replacement function
	local function replaceText( str )
		for i, value in ipairs( dataTable ) do
			str = ( mw.ustring.gsub( str, value[ 1 ], value[ 2 ] ) )
			str = ( mw.ustring.gsub(
				str,
				mw.ustring.lower( value[ 1 ] ),
				mw.ustring.lower( value[ 2 ] )
			) )
		end
		return str
	end
	
	-- Return simple cases
	local pos = mw.ustring.find( text, '\127', 1, true )
	if not pos then
		return replaceText( text )
	end
	
	-- Conserve the strip markers
	-- Source: [[w:Module:Convert]]
	local result = ''
	local stripMarker = '(\127[^\127]*UNIQ[^\127]*%-%a+[^\127]*\127)(%s*)(.*)'
	local before = mw.ustring.sub( text, 1, pos - 1 )
	local remainder = mw.ustring.sub( text, pos )
	while #remainder > 0 do
		local prev = #remainder
		local marker, spaces
		marker, spaces, remainder = mw.ustring.match( remainder, stripMarker )
		if marker then
			result = result
				.. replaceText( before )
				.. marker .. spaces
		else
			local after = mw.ustring.sub( text, #text - prev, #text )
			result = result .. replaceText( after )
		end
		if remainder == nil then
			break
		end
	end
	
	return result
end

-- Cyrillic to Latin (use in other modules)
function p._latn( text )
	return convertText( text, data.latn )
end

-- Cyrillic/Latin to ASCII Latin (use in other modules)
function p._ascii_latn( text )
	text = p._latn( text )
	
	return convertText( text, data.ascii_latn )
end

-- Latin to Cyrillic (use in other modules)
function p._cyrl( text )
	return convertText( text, data.cyrl )
end

-- Global Latin to Cyrillic (use in other modules)
function p._global_cyrl( text )
	if data.global_cyrl then
		return p._cyrl( text )
	end
	
	return text
end

-- Global Latin to Cyrl info (use in other modules)
function p._global_cyrl_info()
	return data.global_cyrl
end

-- Cyrillic to Latin conversion
function p.latn(frame)
	local text = frame.args[ 1 ]
	if text == nil or text == '' then
		return ''
	end
	
	return p._latn( text )
end

-- Cyrillic/Latin to ASCII Latin conversion
function p.ascii_latn( frame )
	local text = frame.args[ 1 ]
	if text == nil or text == '' then
		return ''
	end
	
	return p._ascii_latn( text )
end

-- Latin to Cyrillic conversion
function p.cyrl( frame )
	local text = frame.args[ 1 ]
	if text == nil or text == '' then
		return ''
	end
	
	return p._cyrl( text )
end

-- Global Latin to Cyrillic conversion
function p.global_cyrl( frame )
	local text = frame.args[ 1 ]
	if text == nil or text == '' then
		return ''
	end
	
	return p._global_cyrl( text )
end

-- Global Latin to Cyrl info
function p.global_cyrl_info( frame )
	return data.global_cyrl
end

return p