Module:Sandbox/Nardog/10

local p = {}

function p._encoding(s, ipaNum, xSampa, tipa, praat, keyman, braille) local data = mw.loadData('Module:Sandbox/Nardog/10d') local conv = require('Module:BaseConvert').convert -- Decode HTML entities and strip dotted circle s = mw.ustring.gsub(mw.text.decode(s), '◌', '') local hex, dec = {}, {} -- Initialize tables unless manually input local newIpaNum = not ipaNum and {} local newXSampa = not xSampa and {} local newTipa = not tipa and {} local newPraat = not praat and {} local newKeyman = not keyman and {} local newBraille = not braille and {} local xSampaVars -- Tracks X-SAMPA variants local keymanVars -- Tracks Keyman variants local tipaTies -- Tracks tie bars for TIPA for char in mw.text.gsplit(s, '') do		table.insert(dec, mw.ustring.codepoint(char)) table.insert(hex, conv{ n = dec[#dec], base = 16, width = 4 }) -- Skip others if all are manually input or have reached a dead end if newIpaNum or newXSampa or newTipa or newPraat or newKeyman or newBraille then local t = data[char] if t then -- Table found in data if newIpaNum then if t.ipaNum then table.insert(newIpaNum, t.ipaNum) else newIpaNum = nil end end if newXSampa then if t.xSampa then -- Check for variants if type(t.xSampa) == 'table' then -- Use the 1st variant for now table.insert(newXSampa, t.xSampa[1]) -- Record the position, using char as the table key xSampaVars = xSampaVars or {} xSampaVars[char] = xSampaVars[char] or {} table.insert(xSampaVars[char], #newXSampa) else table.insert(newXSampa, t.xSampa) end else newXSampa = nil end end if newTipa then if t.tipa then -- If the last entry ends in a bare macro and the new -- entry beginds in a Latin character, insert a space local sp = newTipa[1] and newTipa[#newTipa]:find('\\[%u%l]+$') and t.tipa:find('^[%u%l]') and ' ' or '' table.insert(newTipa, sp .. t.tipa) elseif t.tipaDia then -- Diacritic local td = '\\' .. t.tipaDia if newTipa[1] then -- Remove the space inserted between a macro and a							-- Latin character local tl = newTipa[#newTipa]:gsub('^ ', '') -- Add brackets if the base letter is a macro (e.g.							-- \r*{\*r}), a space if the diacritic ends and the -- base letter begins in a Latin character (\r N), -- and nothing otherwise (\r*m) newTipa[#newTipa] = tl:find('^\\') and td .. '{' .. tl .. '}'								or td:find('[%u%l]$') and tl:find('^[%u%l]') and td .. ' ' .. tl or td .. tl						else -- This is the 1st char, just add empty brackets table.insert(newTipa, td .. '{}') end elseif char == '͡' then -- Record the position tipaTies = tipaTies or {} table.insert(tipaTies, #newTipa) else newTipa = nil end end if newPraat then if t.praat then table.insert(newPraat, t.praat) else newPraat = nil end end if newKeyman then if t.keyman then -- Check for variants if type(t.keyman) == 'table' then -- Use the 1st variant for now table.insert(newKeyman, t.keyman[1]) -- Record the position, using char as the table key keymanVars = keymanVars or {} keymanVars[char] = keymanVars[char] or {} table.insert(keymanVars[char], #newKeyman) else table.insert(newKeyman, t.keyman) end else newKeyman = nil end end if newBraille then if t.braille then for _, v in ipairs(t.braille) do							table.insert(newBraille, v)						end else newBraille = nil end end else -- No table for this char, abort looking in data for the rest of				-- the string newIpaNum, newXSampa, newTipa, newPraat, newKeyman, newBraille = nil, nil, nil, nil, nil, nil end end end ipaNum = ipaNum or newIpaNum and newIpaNum[1] and table.concat(newIpaNum, ' ') if not xSampa and newXSampa and newXSampa[1] then -- Check for variants if xSampaVars then local newXSampas = { newXSampa } -- Go through each char with variants for char, _ in pairs(xSampaVars) do				local newXSampas2 = {} -- Go through each variant for i, variant in ipairs(data[char].xSampa) do					if i ~= 1 then -- Skip the 1st, already used -- Go through each string (stored as a table) for _, t in ipairs(newXSampas) do							local t2 = mw.clone(t) -- Go through each position of variant to replace for _, pos in ipairs(xSampaVars[char]) do								t2[pos] = variant end table.insert(newXSampas2, t2) end end end for _, t in ipairs(newXSampas2) do					table.insert(newXSampas, t)				end end xSampa = {} for _, v in ipairs(newXSampas) do				table.insert(xSampa, table.concat(v)) end xSampa = mw.text.listToText(xSampa, ',  or  ,   or  ') end if tipa and tipa ~= 'hide' then makeRow('TIPA', ' ') end if praat and praat ~= 'hide' then makeRow('Praat', ' ') end if keyman and keyman ~= 'hide' then makeRow('Keyman', ' ' .. keyman .. ' ') end if braille and braille[1] ~= 'hide' then braille.type = 6 braille.size = '25px' braille = mw.getCurrentFrame:expandTemplate{ title = 'Template:Braille cell', args = braille }		makeRow('IPA Braille', braille) end return s end

function p.encoding(frame) local args = {} for k, v in pairs(frame.args) do		args[k] = v ~= '' and v	end if not args[1] then return '' end return p._encoding(args[1], args.ipa_number, args.xsampa, args.tipa,		args.praat, args.braille) end

return p