Pergi ke kandungan

Modul:IPA/example

Daripada Wikikamus
local export = {}

local sub = mw.ustring.sub
local find = mw.ustring.find
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local U = mw.ustring.char

function export.example(frame)
	local output = {}
	
	local m_links = require('Module:links')
	local m_languages = require('Module:languages')
	
	table.insert(
		output,
[[
{| class="wikitable"
! Term !! IPA !! Generated X-SAMPA !! Regenerated IPA !! Matched?
]]
	)
	local row =
[[
|-
| link || IPA || XSAMPA || regenerated_IPA || matched
]]
	
	local examples = mw.text.split(frame.args[1], ",%s*")
	
	local m_XSAMPA = require("Module:IPA/X-SAMPA")
	
	for _, example in pairs(examples) do
		local lang, word = match(example, "(%l%l%l?):(.+) [/%[]")
		
		if lang then
			lang = m_languages.getByCode(lang) or error('"' .. lang .. '" is not a valid language code.')
		end
		
		local IPA = match(example, "/[^/]+/")
			or match(example, "%[[^%]]+%]")
			or error('No IPA transcription found in "' .. example .. '".')
		local XSAMPA = m_XSAMPA.IPA_to_XSAMPA(IPA)
		local regenerated_IPA = m_XSAMPA.XSAMPA_to_IPA(XSAMPA)
		
		content = {
			link = lang and word and m_links.full_link{ term = word, lang = lang },
			matched = IPA == regenerated_IPA
				and '<span style="color: green;">yes</span>'
				or '<span style="color: red;">no</span>',
			IPA = '<span class="IPA">' .. IPA .. '</span>',
			XSAMPA = '<code>' .. XSAMPA .. '</code>',
			regenerated_IPA = '<span class="IPA">' .. regenerated_IPA .. '</span>'
		}
		
		local function add_content(item)
			return content[item] or ""
		end
		local row = gsub(row, "[%a_]+", add_content)
		table.insert(output, row)
	end
	
	table.insert(output, "|}")
	
	return table.concat(output)
end

return export