Documentation for this module may be created at Module:Cakm-translit/doc

local export = {}
local u = require("Module:string/char")

-- Stage 1: IAST-style
local consonants = {
--consonants
-- 'โ“จ' is a convenience for language specific modifications. 
	['๐‘„ƒ']='',
	['๐‘„‡']='k', ['๐‘„ˆ']='kh', ['๐‘„‰']='g', ['๐‘„Š']='gh', ['๐‘„‹']='แน…',
	['๐‘„Œ']='c', ['๐‘„']='ch', ['๐‘„Ž']='j', ['๐‘„']='jh', ['๐‘„']='รฑ', 
	['๐‘„‘']='แนญ', ['๐‘„’']='แนญh', ['๐‘„“']='แธ', ['๐‘„”']='แธh', ['๐‘„•']='แน‡', 
	['๐‘„–']='t', ['๐‘„—']='th', ['๐‘„˜']='d', ['๐‘„™']='dh', ['๐‘„š']='n', 
	['๐‘„›']='p', ['๐‘„œ']='ph', ['๐‘„']='b', ['๐‘„ž']='bh', ['๐‘„Ÿ']='m',
	['๐‘„ ']='y', ['๐‘„ก']='โ“จ', ['๐‘„ข']='r', ['๐‘„ฃ']='l', ['๐‘„ค']='v', ['๐‘…‡']='v', 
--	['๐‘€ฐ']='ล›', ['๐‘€ฑ']='แนฃ',
	['๐‘„ฅ']='s', ['๐‘„ฆ']='h', ['๐‘…„']='แธท',
}

local diacritics = {
--matras
	['๐‘……']='ฤ', ['๐‘„จ']='i', ['๐‘„ฉ']='ฤซ', ['๐‘„ช']='u', ['๐‘„ซ']='ลซ',
--	['๐‘€พ']='แน›', ['๐‘€ฟ']='แน', ['๐‘€']='lฬฅ', ['๐‘']='lฬฅฬ„',
	['๐‘„ฌ']='e', ['๐‘„ญ']='ai', ['๐‘„ฎ']='o', ['๐‘„ฏ']='au',  ['๐‘„ด']='',  ['๐‘„ณ']='',
    -- Oddities
    ['๐‘„ง']='ฤƒ', [u(0x11130)]='oi', [u(0x11146)]='ei',
}

local tt = {

--vowels
	['๐‘„ƒ']='a', ['๐‘„„']='i', ['๐‘„…']='u', ['๐‘„†']='e', 
	-- chandrabindu    
	['๐‘„€']='mฬ', --until a better method is found
	-- anusvara    
	['๐‘„']='แนƒ', --until a better method is found
	-- visarga    
	['๐‘„‚']='แธฅ',
	--numerals
	['๐‘„ถ']='0', ['๐‘„ท']='1', ['๐‘„ธ']='2', ['๐‘„น']='3', ['๐‘„บ']='4', ['๐‘„ป']='5', ['๐‘„ผ']='6', ['๐‘„ฝ']='7', ['๐‘„พ']='8', ['๐‘„ฟ']='9',
	--punctuation        
	['๐‘…']='.', --danda
    ['๐‘…‚']='.', --double danda
    ['๐‘…ƒ']='?', -- question mark
}

function export.tr(text, lang, sc)
	if type(lang) == "table" then
		lang = lang:getFullCode()
	end
	
	text = mw.ustring.gsub(
		text,
		'([๐‘„ƒ๐‘„‡-๐‘„ฆ๐‘…„๐‘…‡])'.. -- consonant
		'([๐‘„ง-๐‘„ด'..u(0x11145,0x11146)..']?)'.. -- vowel, joiner or no vowel
		'([๐‘€…-๐‘€’]?)', -- independent vowel 
		function(c, d, e)
			if d == "" and e ~= "" then        
				if tt[e] == "i" or tt[e] == "u" then return consonants[c] .. 'a' .. tt[e] .. 'ฬˆ'
				else return consonants[c] .. 'a' .. tt[e] end
			elseif e ~= "" then
				return consonants[c] .. diacritics[d] .. tt[e]
			elseif d == "" then        
				return consonants[c] .. 'a'
			else
				return consonants[c] .. diacritics[d]
			end
		end)

	text = mw.ustring.gsub(text, '.', tt)
	if (lang == 'ccp') then
		text = mw.ustring.gsub(text, '.',
			{
				['ฤƒ']='a', ['a']='ฤ', ['ฤ']='A',
--				['แนƒ']='แน…',
				['v']='w',
				['y']='แบ' ,['โ“จ']='y',
			})
		text = mw.ustring.gsub(text, 'แนƒ$', 'แน…')
	elseif (lang == 'pi') then
		text = mw.ustring.gsub(text, 'aแธฅ', 'ฤ')
	end
	if (lang == 'sa' or lang == 'pi') and mw.ustring.match(text, 'lฬฅ') then
		text = mw.ustring.gsub(text, 'lฬฅ', 'แธท')
		text = mw.ustring.toNFC(text)
	end

	return text
end
 
return export