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

local export = {}
local gsub = mw.ustring.gsub

local tt = {
	["ក"] = "k", ["ខ"] = "kh", ["គ"] = "g", ["ឃ"] = "gh", ["ង"] = "ṅ",
	["ច"] = "c", ["ឆ"] = "ch", ["ជ"] = "j", ["ឈ"] = "jh", ["ញ"] = "ñ",
	["ដ"] = "ṭ", ["ឋ"] = "ṭh", ["ឌ"] = "ḍ", ["ឍ"] = "ḍh", ["ណ"] = "ṇ",
	["ត"] = "t", ["ថ"] = "th", ["ទ"] = "d", ["ធ"] = "dh", ["ន"] = "n",
	["ប"] = "p", ["ផ"] = "ph", ["ព"] = "b", ["ភ"] = "bh", ["ម"] = "m",
	["យ"] = "y", ["រ"] = "r", ["ល"] = "l", ["វ"] = "v", ["ឝ"] = "ś",
	["ឞ"] = "ṣ", ["ស"] = "s", ["ហ"] = "h", ["ឡ"] = "ḷ", ["អ"] = "ʼ",

	["ឣ"] = "a", ["ឤ"] = "ā", -- discouraged use
	["ឥ"] = "i", ["ឦ"] = "ī",
	["ឧ"] = "u", ["ឨ"] = "uk", ["ឩ"] = "ū", ["ឪ"] = "uv",
	["ឫ"] = "ṛ", ["ឬ"] = "ṝ", ["ឭ"] = "l̤", ["ឮ"] = "l̤̄",
	["ឯ"] = "e", ["ឰ"] = "ai", ["ឱ"] = "o", ["ឲ"] = "o", ["ឳ"] = "ao",

	["ា"] = "ā", ["ិ"] = "i", ["ី"] = "ī", ["ឹ"] = "ẏ", ["ឺ"] = "ȳ", 
	["ុ"] = "u", ["ូ"] = "ū", ["ួ"] = "ua",
	["ើ"] = "oe", ["ឿ"] = "ẏa", ["ៀ"] = "ia", 
	["េ"] = "e", ["ែ"] = "ae", ["ៃ"] = "ai", ["ោ"] = "o", ["ៅ"] = "au", 
    ["ំ"] = "ṃ", ["ះ"] = "aḥ", ["ៈ"] = "à",
   	["៉"] = "̎", ["៊"] = "̂",
	["់"] = "̍", ["៍"] = "̽", ["៎"] = "ä", ["៏"] = "ă", 
    ["័"] = "ǎ", ["៑"] = "̄", ["៌"] = "ͬ", ["្"] = "", ["'"] = " ",

    ["០"] = "0", ["១"] = "1", ["២"] = "2", ["៣"] = "3", ["៤"] = "4", 
	["៥"] = "5", ["៦"] = "6", ["៧"] = "7", ["៨"] = "8", ["៩"] = "9", 
	["៰"] = "0", ["៱"] = "1", ["៲"] = "2", ["៳"] = "3", ["៴"] = "4",
	["៵"] = "5", ["៶"] = "6", ["៷"] = "7", ["៸"] = "8", ["៹"] = "9",
}

function export.tr(text, lang, sc, debug_mode)

	if type(text) == "table" then -- called directly from a template
		text = text.args[1]
	end
    
    text = text .. " "
    local c = "([កខគឃងចឆជឈញដឋឌឍណតថទធនបផពភមយរលវឝឞសហឡអ])"
    local v = "([aាិីឹឺុូួើឿៀេែៃោៅះៈ៉៊់៌៍៎៏័៑្])"
    local nikkahit = "ំ"
    
    text = gsub(text, c, "%1a")
    text = gsub(text, "a".. v, "%1") 

    text = gsub(text, nikkahit .. c .. "a", "ṃ%1")

    text = gsub(text,  v .. c .. "a ", function(j, k) -- ending
		return j .. k .. " "
	end)

    text = gsub(text, "([៉៊])(')", "%1a%2")
    text = gsub(text, " ", "")

    text = gsub(text, ".", tt)

	return text

end

return export