Jump to content

Module:IPAc-en/documentation

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 06:10, 19 June 2015 (basic functionality complete for pronunciation and phonemes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module generates automatic documentation for [[Template:IPAc-en]].

local pronunciationData = mw.loadData('Module:IPAc-en/pronunciation')
local phonemeData = mw.loadData('Module:IPAc-en/phonemes')
local p = {}

local function makeCode(s)
	return string.format('<code>%s</code>', s)
end

local function buildTable(options)
	local ret = {}
	ret[#ret + 1] = '{| class="wikitable"'
	if options.headerRow then
		for i, header in ipairs(options.headerRow) do
			ret[#ret + 1] = '! ' .. header
		end
	end
	if options.rows then
		for i, t in ipairs(options.rows) do
			ret[#ret + 1] = '|-'
			for j, data in ipairs(t) do
				ret[#ret + 1] = '| ' .. data
			end
		end
	end
	ret[#ret + 1] = '|}'
	return table.concat(ret, '\n')
end

local function makeCodeTable(oldData, headers, rowCallback, sortCallback)
	-- Copy data from mw.loadData into a new table
	local data = {}
	for k1, t in pairs(oldData) do
		data[k1] = {}
		for k2, v in pairs(t) do
			data[k1][k2] = v
		end
	end
	local headerRow = {'Code', 'Aliases'}
	for i, header in ipairs(headers) do
		headerRow[#headerRow + 1] = header
	end
	local sortedRows = {}
	for code, t in pairs(data) do
		t._code = code
		sortedRows[#sortedRows + 1] = t
	end
	local sortFunc = sortCallback or function (t1, t2)
		return t1[1] < t2[1]
	end
	table.sort(sortedRows, sortFunc)
	local rows = {}
	for i, t in ipairs(sortedRows) do
		local code = t._code
		t._code = nil
		local aliases = {}
		if t.aliases then
			for i, alias in ipairs(t.aliases) do
				aliases[#aliases + 1] = makeCode(alias)
			end
		end
		aliases = table.concat(aliases, ', ')
		rows[#rows + 1] = {makeCode(code), aliases, rowCallback(t)}
	end
	return buildTable{
		headerRow = headerRow,
		rows = rows
	}
end

function p.pronunciation()
	return makeCodeTable(pronunciationData, {'Output'}, function (t)
		return t.text
	end)
end

function p.phonemes()
	return makeCodeTable(
		phonemeData,
		{'Label', 'Tooltip', 'Type'},
		function (t)
			return t.label, t.tooltip or '', t.tooltip and 'phoneme' or 'separator'
		end,
		function (t1, t2)
			if t1.tooltip and t2.tooltip or not t1.tooltip and not t2.tooltip then
				return t1._code < t2._code
			elseif t1.tooltip then
				return true
			else
				return false
			end
		end
	)
end

return p