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

local export = {}

local u = mw.ustring.char

local macron = u(0x0304)

local tt = {
	["А"]='A', ["а"]='a',
	["Ӓ"]="’a", ["ӓ"]="’a",
	["Б"]='B', ["б"]='b',
	["В"]='V', ["в"]='v',
	["Г"]='G', ["г"]='g',
	["Д"]='D', ["д"]='d',
	["Е"]='Je', ["е"]='je',
	["Ё"]="Jo", ["ё"]="jo",
	["Ж"]='Ž', ["ж"]='ž',
	["З"]='Z', ["з"]='z',
	["Һ"]="H", ["һ"]="h", ["ʼ"]="h",
	["И"]='I', ["и"]='i',
	["Ӣ"]="Ī", ["ӣ"]="ī",
	["Ӥ"]="’Ï", ["ӥ"]="’ï",		
	["Й"]="J", ["й"]="j",
	["Ј"]="J̥", ["ј"]="j̥", ["Ҋ"]="J̥", ["ҋ"]="j̥",
	["К"]='K', ["к"]='k',
	["Л"]='L', ["л"]='l',
	["Ӆ"]='L̥', ["ӆ"]='l̥',
	["М"]='M', ["м"]='m',
	["Ӎ"]="M̥", ["ӎ"]="m̥",
	["Н"]='N', ["н"]='n',
	["Ӊ"]="N̥", ["ӊ"]="n̥",
	["Ӈ"]="Ŋ", ["ӈ"]="ŋ",
	["О"]='O', ["о"]='o',
	["П"]='P', ["п"]='p', 
	["Р"]='R', ["р"]='r',
	["Ҏ"]="R̥", ["ҏ"]="r̥",
	["С"]='S', ["с"]='s',
	["Т"]='T', ["т"]='t',
	["У"]='U', ["у"]='u', 
	["Ӯ"]="Ū", ["ӯ"]="ū",
	["Ф"]='F', ["ф"]='f',
	["Х"]='H', ["х"]='x',
	["Ц"]='C', ["ц"]='c',
	["Ч"]='Č', ["ч"]='č',
	["Ш"]='Š', ["ш"]='š',
	["Щ"]="Šč", ["щ"]="šč",
	["Ы"]="Ï", ["ы"]="ï",
	["Ӹ"]="’Ï", ["ӹ"]="’ï",	
	["Ъ"]="", ["ъ"]="",
	["Ь"]="ʼ", ["ь"]="ʼ", ["Ҍ"]= "ʼ", ["ҍ"]="ʼ",
	["Э"]="E", ["э"]="e",
	["Ӭ"]="’E", ["ӭ"]="’e",
	["Ю"]="Ju", ["ю"]="ju",
	["Я"]="Ja", ["я"]="ja",
}

local vowel = "аӓеёиӥӣоуӯыӹэӭюяАӒЕЁӤӢОУӮЫӸЭӬЮЯ"
local consonant = "БбВвГгДдЖжЗзКкЛлӅӆМмӍӎНнӉӊӇӈПпРрҎҏСсТтХхҺһЦцЧчШшЩщ"

function export.tr(text, lang, sc)
	-- initial palatal n + long vowel
	text = mw.ustring.gsub(text, "([Нн])я" .. macron, "%1jā")
	text = mw.ustring.gsub(text, "([Нн])ёа" .. macron, "%1jåå")	
	text = mw.ustring.gsub(text, "([Нн])ёа", "%1jå")	

	-- digraphs ie and ea
	text = mw.ustring.gsub(text, "([" .. consonant .. "])е" .. macron, "%1ie")
	text = mw.ustring.gsub(text, "([" .. consonant .. "])я" .. macron, "%1ea")
	
	-- initial ни → njï, if followed by two consonants
	text = mw.ustring.gsub(text, "^ни([".. consonant .."][".. consonant .."])", "njï%1")
	
	-- combinations
	text = mw.ustring.gsub(text, "^айй([".. consonant .."])а", "ajj%1a")
	text = mw.ustring.gsub(text, "^аҋҋ([".. consonant .."])", "aj̥j̥%1")	
	text = mw.ustring.gsub(text, "^([".. consonant .."])айй([".. consonant .."])а", "%1ajj%2a")	
	text = mw.ustring.gsub(text, "^([".. consonant .."])аҋҋ([".. consonant .."])", "%1aj̥j̥%2")	
	text = mw.ustring.gsub(text, "^([".. consonant .."])а$", "%1a")
	
	-- initial х → h
	text = mw.ustring.gsub(text, "^" .. "х", "h")
	text = mw.ustring.gsub(text, "^" .. "Х", "H")
	text = mw.ustring.gsub(text, " " .. "х", " h")
	text = mw.ustring.gsub(text, " " .. "Х", " H")
	
	-- initial j + vowels
	text = mw.ustring.gsub(text, "^" .. "ю" .. macron, "jū")
	text = mw.ustring.gsub(text, "^" .. "я" .. macron, "jā")
	text = mw.ustring.gsub(text, "^" .. "ӣ", "ji")
	text = mw.ustring.gsub(text, "^" .. "е" .. macron, "jie")
	text = mw.ustring.gsub(text, "^" .. "ё" .. macron, "jō")	
	text = mw.ustring.gsub(text, "^" .. "Ю" .. macron, "Jū")
	text = mw.ustring.gsub(text, "^" .. "Я" .. macron, "Jā")
	text = mw.ustring.gsub(text, "^" .. "Ӣ", "Ji")
	text = mw.ustring.gsub(text, "^" .. "Е" .. macron, "Jie")
	text = mw.ustring.gsub(text, "^" .. "ё" .. macron, "Jō")		
	text = mw.ustring.gsub(text, " " .. "ю" .. macron, " jū")
	text = mw.ustring.gsub(text, " " .. "я" .. macron, " jea")
	text = mw.ustring.gsub(text, " " .. "ӣ", " ji")
	text = mw.ustring.gsub(text, " " .. "e" .. macron, " jie")
	text = mw.ustring.gsub(text, " " .. "ё" .. macron, " jō")	
	text = mw.ustring.gsub(text, " " .. "Ю" .. macron, " jū")
	text = mw.ustring.gsub(text, " " .. "Я" .. macron, " jea")
	text = mw.ustring.gsub(text, " " .. "Ӣ", " ji")
	text = mw.ustring.gsub(text, " " .. "E" .. macron, " jie")
	text = mw.ustring.gsub(text, " " .. "Ё" .. macron, " jō")	

	text = mw.ustring.gsub(text, "че", "če")
	text = mw.ustring.gsub(text, "йе", "jje")
	
	-- hard sign between same consonants
	text = mw.ustring.gsub(text, "във", "v'v")
	text = mw.ustring.gsub(text, "лъл", "l'l")
	text = mw.ustring.gsub(text, "мъм", "m'm")	
	text = mw.ustring.gsub(text, "нън", "n'n")	
	text = mw.ustring.gsub(text, "със", "s's")	
	text = mw.ustring.gsub(text, "тът", "t't")	

	-- soft/semi-soft/hard signs
	text = mw.ustring.gsub(text, "([".. consonant .."])([ьҍъ])е", "%1%2je")
	text = mw.ustring.gsub(text, "([".. consonant .."])([ьҍъ])ё", "%1%2jo")
	text = mw.ustring.gsub(text, "([".. consonant .."])([ьҍъ])ю", "%1%2ju")	
	text = mw.ustring.gsub(text, "([".. consonant .."])([ьҍъ])я", "%1%2ja")	
	
	-- long a
	text = mw.ustring.gsub(text, "а" .. macron, "ā")
	text = mw.ustring.gsub(text, "А" .. macron, "Ā")	

    -- final а	
	text = mw.ustring.gsub(text, "([".. consonant .."])%1" .. "а" .. "$", "%1%1a")
	text = mw.ustring.gsub(text, "([".. consonant .."])%1([".. consonant .."])" .. "а" .. "$", "%1%1%2a")	
	text = mw.ustring.gsub(text, "йй([".. consonant .."])" .. "а" .. "$", "jj%1a")	
	text = mw.ustring.gsub(text, "ҋҋ([".. consonant .."])" .. "а" .. "$", "j̥j̥%1a")		
	text = mw.ustring.gsub(text, "йя" .. "$", "jja")		
	
    -- endings
	text = mw.ustring.gsub(text, "адҍ" .. "$", "ad’")	    
	text = mw.ustring.gsub(text, "эдҍ" .. "$", "âd’")
	text = mw.ustring.gsub(text, "чедҍ" .. "$", "čâd’")	
	text = mw.ustring.gsub(text, "йедҍ" .. "$", "jâd’")	
	text = mw.ustring.gsub(text, "([БбВвГгДдЖжЗзКкЛлӅӆМмӍӎӉӊӇӈПпРрҎҏСсТтХхЦцШшЩщ])ем" .. "$", "%1’âm")			
	text = mw.ustring.gsub(text, "([БбВвГгДдЖжЗзКкЛлӅӆМмӍӎӉӊӇӈПпРрҎҏСсТтХхЦцШшЩщ])эм" .. "$", "%1âm")	
	text = mw.ustring.gsub(text, "чем" .. "$", "čâm")
	text = mw.ustring.gsub(text, "эл" .. "$", "âl")	
	text = mw.ustring.gsub(text, "ас" .. "$", "as")		
	text = mw.ustring.gsub(text, "ань" .. "$", "anj")
	text = mw.ustring.gsub(text, "ант" .. "$", "ant")	
	
	-- palatalization
	text = mw.ustring.gsub(text, "([БбВвГгДдЖжЗзКкЛлӅӆМмӍӎӇӈПпРрҎҏСсТтХхЦцШшЩщ])е", "%1’e")
	text = mw.ustring.gsub(text, "([БбВвГгДдЖжЗзКкЛлӅӆМмӍӎӇӈПпРрҎҏСсТтХхЦцШшЩщ])ё", "%1’o")
	text = mw.ustring.gsub(text, "([БбВвГгДдЖжЗзКкЛлӅӆМмӍӎӇӈПпРрҎҏСсТтХхЦцШшЩщ])ю", "%1’u")
	text = mw.ustring.gsub(text, "([БбВвГгДдЖжЗзКкЛлӅӆМмӍӎӇӈПпРрҎҏСсТтХхЦцШшЩщ])я", "%1’a")	

	-- palatal н
	text = mw.ustring.gsub(text, "([НнӉӊ])е", "%1je")	
	text = mw.ustring.gsub(text, "([НнӉӊ])ё", "%1jo")
	text = mw.ustring.gsub(text, "([НнӉӊ])ю", "%1ju")
	text = mw.ustring.gsub(text, "([НнӉӊ])я", "%1ja")
	
	-- дз → ʒ
	text = mw.ustring.gsub(text, "дз", "ʒ")
	-- дж → ǯ
	text = mw.ustring.gsub(text, "дж", "ǯ")
	-- oa 
	text = mw.ustring.gsub(text, "оа", "oa")	
	-- а (before final consonants and й)
	text = mw.ustring.gsub(text, "а([".. consonant .."])" .. "$", "a%1")	
	text = mw.ustring.gsub(text, "ай" .. "$", "aj")	
	-- replacing the remaining (cyrillic) a's with â's
	text = mw.ustring.gsub(text, "а", "â")
	
	return (mw.ustring.gsub(text, '.', tt))
end

return export