Moduł:IPA

Z Polska Encyklopedia Numizmatyczna
Przejdź do nawigacji Przejdź do wyszukiwania

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł: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
		
		local ipadata = mw.loadData( 'Moduł:IPA/data' )
	 
		local split = function(text)
			local result = {}
			local joined = false
			local current = ""
			for code in mw.ustring.gcodepoint(text) do
				local c = mw.ustring.char(code)
				if ipadata.joining[code] then
					current = current .. c
					joined = true
				elseif joined or ipadata.combining[code] then
					current = current .. c
					joined = false
				else
					if current ~= "" then
						table.insert(result, current)
					end
					current = c
				end
			end
	 
			-- append last part
			if current ~= "" then
				table.insert(result, current)
			end
	 
			return result
		end
	 
		local decorate = function(parts)
			local last = false
			local currentLink = nil
			local result = {}
			local count = #parts
			local unknown = false
			table.insert(parts,"") -- append empty text to simplify loop below
			local i = 1
			while i <= count do
				local s, n = parts[i]..parts[i+1], 2
				local t = ipadata.sounds[s]
				if not t then
					s, n = parts[i], 1
					t = ipadata.sounds[s]
				end
	 
				local link = nil
				if t then
					t = t.see and ipadata.sounds[t.see] or t
					link = ((t.last and last) and last[t.last] or t.link) or ipadata.unknownSound
					last = t.last and last or t
				elseif mw.ustring.match(s,"[^%s']") then
					unknown = true
					s = mw.text.nowiki(s)
					link = ipadata.unknownSound
					last = false
				else
					last = false
				end
	 
				-- append the optional title
				if link ~= currentLink then
					if currentLink then
						table.insert(result,"]]")
						currentLink = nil
					end
					if link then
						table.insert(result,"[[")
						table.insert(result,link)
						table.insert(result,"|")
						currentLink = link
					end
				end
	 
				-- append the sound
				table.insert(result,s)
	 
				-- move to next sound in the input
				i = i + n
			end
	 
			if currentLink then
				table.insert(result, "]]")
			end
	 
			if unknown
			and (mw.title.getCurrentTitle().namespace == 0)
			then
				table.insert(result,ipadata.checkCat)
			end
			
			return table.concat(result,"")
		end
	 
		local parts = split(arg)
		local result = decorate(parts)
		return result
	end,

}