Jump to content

Module:IPAc-en/documentation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
start documentation module for Module:IPAc-en
 
basic functionality complete for pronunciation and phonemes
Line 1: Line 1:
-- This module generates automatic documentation for [[Template:IPAc-en]].
-- This module generates automatic documentation for [[Template:IPAc-en]].


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

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


local function buildTable(options)
local function buildTable(options)
local ret = {}
local ret = {}
ret[#ret + 1] = '{|' .. (options.class and (' class="' .. options.class .. '') or '')
ret[#ret + 1] = '{| class="wikitable"'
if options.headerRow then
if options.headerRow then
for i, header in ipairs(options.headerRow) do
for i, header in ipairs(options.headerRow) do
Line 24: Line 29:
end
end


local function makeCodeTable(oldData, headers, rowCallback, sortCallback)
function p.pronunciation()
-- Copy data from mw.loadData into a new table
local rows = {}
local data = {}
for code, t in pairs(data.pronunciation) do
for k1, t in pairs(oldData) do
rows[#rows + 1] = {code, t.text}
data[k1] = {}
for k2, v in pairs(t) do
data[k1][k2] = v
end
end
end
local headerRow = {'Code', 'Aliases'}
table.sort(rows, function (t1, t2)
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]
return t1[1] < t2[1]
end)
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{
return buildTable{
class = 'wikitable',
headerRow = headerRow,
headers = {'Code', 'Output'},
rows = rows
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
end



Revision as of 06:10, 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(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