Module:User:Kephir/untranslit

local export = {}

-- "untranslit" is a misnomer, really. -- given a piece of Cyrillic text and its transliteration with stress marked, -- this function tries to place the stress mark on the correct Cyrillic letter. -- very, very ugly... but I think it will do the job. function export.untranslit(text, tr) if type(text) == 'table' then text, tr = text.args[1], text.args[2] end tr = mw.ustring.toNFD(tr)

local vo = {} for offset in mw.ustring.gmatch(text, '[аеёюяуоыиэ]') do		table.insert(vo, offset) end

local i, delta = 1, 0 for accent in mw.ustring.gmatch(tr, '[aeiouy][\204\140]?([\204\128\204\129]?)') do		if accent ~= "" then text = mw.ustring.sub(text, 1, vo[i] + delta) .. accent .. mw.ustring.sub(text, vo[i] + delta + 1) delta = delta + 1 end i = i + 1 end text = mw.ustring.gsub(text, "ё[\204\128\204\129]", "ё") -- ё should not have an explicit stress marker return text end

return export