Jump to content

Module:IPAc-en/documentation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
fix sort error
update to reflect data module changes
Line 29: Line 29:
end
end


local function makeCodeTable(oldData, headers, rowCallback, sortCallback)
local function makeCodeTable(data, headers, callback)
-- 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'}
local headerRow = {'Code', 'Aliases'}
for i, header in ipairs(headers) do
for i, header in ipairs(headers) do
headerRow[#headerRow + 1] = header
headerRow[#headerRow + 1] = header
end
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._code < t2._code
end
table.sort(sortedRows, sortFunc)
local rows = {}
local rows = {}
for i, t in ipairs(sortedRows) do
for i, t in ipairs(data) do
local code = t._code
t._code = nil
local aliases = {}
local aliases = {}
if t.aliases then
if t.aliases then
Line 62: Line 43:
end
end
aliases = table.concat(aliases, ', ')
aliases = table.concat(aliases, ', ')
rows[#rows + 1] = {makeCode(code), aliases, rowCallback(t)}
rows[#rows + 1] = {makeCode(t.code), aliases, callback(t)}
end
end
return buildTable{
return buildTable{
Line 82: Line 63:
function (t)
function (t)
return t.label, t.tooltip or '', t.tooltip and 'phoneme' or 'separator'
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
)
)

Revision as of 11:43, 19 June 2015

-- 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(data, headers, callback)
	local headerRow = {'Code', 'Aliases'}
	for i, header in ipairs(headers) do
		headerRow[#headerRow + 1] = header
	end
	local rows = {}
	for i, t in ipairs(data) do
		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(t.code), aliases, callback(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
	)
end

return p