توضیحات پودمان[ایجاد] [پاکسازی]
require('strict')
local p = {}

local lects = mw.loadData('Module:IPA/data/export')

local function addNote(cell, note)
	cell:wikitext(mw.ustring.format(' <small>(%s)</small>', note))
end

function p.main()
	local t = mw.html.create('table'):addClass('wikitable sortable mw-collapsible')
		:tag('caption'):addClass('nowrap'):wikitext('مروری بر داده‌ها'):done()
		:tag('tr')
			:tag('th'):attr('colspan', 4):wikitext('زبان'):done()
			:tag('th'):attr('colspan', 4):wikitext('گویش'):done()
			:done()
		:tag('tr')
			:tag('th'):wikitext('شناسه'):done()
			:tag('th'):wikitext('نام'):done()
			:tag('th'):wikitext('پیوند'):done()
			:tag('th'):wikitext('کلید'):done()
			:tag('th'):wikitext('شناسه'):done()
			:tag('th'):wikitext('نام'):done()
			:tag('th'):wikitext('پیوند'):done()
			:tag('th'):wikitext('کلید'):done()
			:done()
	local langRow, langCells, dialectCount
	for i, lect in ipairs(lects) do
		local row
		local cells = {}
		if lect.parent then
			dialectCount = dialectCount + 1
			if dialectCount == 1 then
				row = langRow
			else
				row = t:tag('tr')
				for _, cell in ipairs(langCells) do
					cell:attr('rowspan', dialectCount)
				end
			end
		else
			row = t:tag('tr')
			langRow = row
			langCells = cells
			dialectCount = 0
		end
		for _ = 1, 4 do
			table.insert(cells, row:tag('td'))
		end
		if not lect.parent and not (lects[i + 1] and lects[i + 1].parent) then
			for _ = 1, 4 do
				row:tag('td'):css('background', '#ececec')
			end
		end
		cells[1]:wikitext('<code>' .. mw.ustring.gsub(lect.code, '^[^-]+%-', '') .. '</code>')
		if lect.aliases then
			local aliases = {}
			for _, alias in ipairs(lect.aliases) do
				table.insert(aliases, alias)
			end
			addNote(cells[1], mw.ustring.format(
				'همچنین <code>%s</code>',
				table.concat(aliases, '</code>, <code>')
			))
		end
		do
			local notes = {}
			if lect.name then
				if lect.name == lect.extName then
					table.insert(notes, 'زائد')
				elseif lect.extName then
					table.insert(notes, mw.ustring.format('جای «%s» را می‌گیرد', lect.extName))
				end
			end
			if lect.text then
				table.insert(notes, mw.ustring.format('برچسب: "%s"', lect.text))
			end
			cells[2]:wikitext(lect.name or lect.extName)
			if #notes > 0 then
				addNote(cells[2], table.concat(notes, '؛ '))
			end
		end
		do
			local note
			local intLink = lect.link or lect.generatedLink
			if intLink then
				if intLink == lect.extLink then
					note = 'زائد'
				elseif lect.extLink then
					note = mw.ustring.format('جای [[%s]] را می‌گیرد', lect.extLink)
					local intTitle = mw.title.new(intLink)
					intTitle = intTitle.redirectTarget or intTitle
					local extTitle = mw.title.new(lect.extLink)
					extTitle = extTitle.redirectTarget or extTitle
					if intTitle ~= extTitle then
						note = note .. '، که یک مقالهٔ متفاوت است'
					end
				end
			end
			local s = intLink or lect.extLink
			s = lect.generatedLink and '([[' .. s .. ']])' or '[[' .. s .. ']]'
			cells[3]:wikitext(s)
			if note then
				addNote(cells[3], note)
			end
		end
		cells[4]:wikitext(
			lect.key and '[[' .. lect.key .. ']]' or
				'([[' .. (lect.parent and lect.parent.key or 'راهنما:الفبای آوانگاری بین‌المللی') .. ']])'
		)
	end
	return tostring(t)
end

function p.keys()
	local t = mw.html.create('table'):addClass('wikitable sortable mw-collapsible')
		:tag('caption'):addClass('nowrap'):wikitext('زبان‌های دارای کلیدهای اختصاصی'):done()
		:tag('tr')
			:tag('th'):wikitext('کلی'):done()
			:tag('th'):wikitext('زبان'):done()
			:tag('th'):wikitext('شناسه'):done()
			:done()
	local byKey, keys = {}, {}
	for _, lect in ipairs(lects) do
		local key = lect.key or lect.parent and lect.parent.key
		if key then
			local asciiKey = mw.ustring.gsub(mw.ustring.toNFD(key), '[^ -~]', '')
			if not byKey[asciiKey] then
				byKey[asciiKey] = { name = key }
				table.insert(keys, asciiKey)
			end
			table.insert(byKey[asciiKey], {
				code = lect.code,
				name = lect.name or lect.extName,
				link = lect.link or lect.generatedLink or lect.extLink
			})
		end
	end
	table.sort(keys)
	for _, asciiKey in ipairs(keys) do
		local row = t:tag('tr')
		local keyLects = byKey[asciiKey]
		local keyCell = row:tag('td'):wikitext('[[' .. keyLects.name .. ']]')
		local lang
		if #keyLects > 1 then
			keyCell:attr('rowspan', #keyLects)
			table.sort(keyLects, function (a, b) return a.name < b.name end)
			local keyName = mw.ustring.gsub(keyLects.name, '^[^/]*/', '')
			for _, lect in ipairs(keyLects) do
				if lect.name == keyName then
					lang = lect
					break
				end
			end
			if not lang then
				for _, lect in ipairs(keyLects) do
					if mw.ustring.find(lect.name, '^زبان‌های ') then
						lang = lect
						break
					end
				end
			end
			if not lang then
				for _, lect in ipairs(keyLects) do
					if not mw.ustring.find(lect.code, '-') then
						lang = lect
						break
					end
				end
			end
		end
		lang = lang or keyLects[1]
		local prev = {
			name = lang.name,
			count = 1,
			cell = row:tag('td'):wikitext(
				mw.ustring.format('[[%s|%s]]', lang.link, lang.name)
			)
		}
		row:tag('td'):wikitext('<code>' .. lang.code .. '</code>')
		for _, lect in ipairs(keyLects) do
			if lect ~= lang then
				local subRow = t:tag('tr')
				if prev.name == lect.name then
					prev.count = prev.count + 1
					prev.cell:attr('rowspan', prev.count)
				else
					prev = {
						name = lect.name,
						count = 1,
						cell = subRow:tag('td'):wikitext(
							mw.ustring.format('[[%s|%s]]', lect.link, lect.name)
						)
					}
				end
				subRow:tag('td'):wikitext('<code>' .. lect.code .. '</code>')
			end
		end
	end
	return tostring(t)
end

return p