Moduł:Brudnopis/BartekChom/IPA

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/BartekChom/IPA/opis

return {
	
	preprocess = function (frame)
		local arg = frame:getParent().args[1]
		if not arg  then
			-- missing arguments
			return nil
		end
		
		if #arg == 0 then
			-- empty argument
			return nil
		end
			
		if not mw.ustring.isutf8(arg) then
			-- invalid input is left as is
			return arg
		end
		
		--data
		local ipadata = mw.loadData( 'Moduł:IPA/data' )
		local combining = "̴̵̶̡̢̧̨̛̖̗̘̙̜̝̞̟̠̣̤̥̦̩̪̫̬̭̮̯̰̱̹̺̻̼͇͈͉͍͎͓͔͕͖͙͚̀́̂̃̄̅̆̇̈̉̊̋̌̍̎̏̐̑̒̓̔̽̀́͂̓̈́͆͊͋͌͐͑͒͗᷄᷅᷆᷇᷈᷉̕̚͘ͅ"
		local joining = "͡"
	
		--merging groups
		local parts = {}
		local joined = false
		local current = ""
		for c0 in mw.ustring.gcodepoint( arg ) do
			local c = mw.ustring.char(c0)
			if mw.ustring.match(joining, c) then
				current = current .. c
				joined = true
			elseif mw.ustring.match(combining, c) or joined then
				current = current .. c
				joined = false
			else
				if current ~= "" then
					table.insert(parts, current)
				end
				current = c
			end
		end

		-- append last part
		if current ~= "" then
			table.insert(parts, current)
		end
		
		local i = 1
		local count = #parts
		local result = ""
		while i <= count do
			local part = nil
			if i+1 <= count then
				t = ipadata.sounds[parts[i] .. parts[i+1]]
				if t then
					if t.see then
						t = ipadata.sounds[t.see]
					end
					part = parts[i] .. parts[i+1]
	
					i = i+2
				end
			end
	
			if not part then
				part = parts[i]
				t = ipadata.sounds[parts[i]]
				if t then
					if t.see then
						t = ipadata.sounds[t.see]
					end
					
				else
					t = { link = ipadata.unknownSound }
				end
				
				i = i+1
			end
	
			result = result .. "[[" .. t.link .. "|" .. part .. "]]"
		end
		
		return result
	end,

}