Module:Sandbox/Nardog/5dd

require('strict')

local data = mw.loadData('Module:Sandbox/Nardog/5d') local p = {}

local function getNameAndLink(code) local res = require('Module:Lang')._name_from_tag({ code, link = 'yes' }) local name = res:match('([^%[|%]]+)%]%]$') local link = res:match('^%[%[([^|%]]+)') return name, link end

local function formatNameOrLink(internal, external, link, name) local note local generated = not internal and name and name ~= '' and name .. ' language' if internal or generated then if (internal or generated) == external then note = 'redundant' elseif external then note = string.format(				'overrides %s%s%s',				link and  or '"',				external,				link and  or '"'			) if link then local intTitle = mw.title.new(internal or generated) local intRedir = intTitle.redirectTarget intTitle = intRedir or intTitle local extTitle = mw.title.new(external) local extRedir = extTitle.redirectTarget extTitle = extRedir or extTitle if intTitle ~= extTitle then note = note .. ', a different article' end end end end local s = generated or internal or external if link then s = generated and '(' .. s .. ')' or  .. s ..  end if note then s = string.format('%s (%s) ', s, note) end return s end

local function organize(lects, parent, parentCode) local t, aliases = {}, {} for code, lect in pairs(lects) do		if lect.aliasOf then aliases[lect.aliasOf] = aliases[lect.aliasOf] or {} table.insert(aliases[lect.aliasOf], code) end end for code, lect in pairs(lects) do		if not lect.aliasOf then local name, link = getNameAndLink(				parent and parentCode .. '-' .. code or code			) table.insert(t, {				code = ' ',				name = formatNameOrLink(lect.name, name),				link = formatNameOrLink(lect.link, link, true, lect.name),				key = lect.key and  .. lect.key .. 					or '(' .. (parent and parent.key or data.defaultKey) .. ')'			}) if lect.dialects then t[#t].dialects = organize(lect.dialects, lect, code) end if aliases[code] then table.sort(aliases[code]) t[#t].code = string.format(					'%s (also ) ',					t[#t].code,					table.concat(aliases[code], ',  ')		for i, lect in ipairs(lects) do			if lect ~= lang then				local subRow = wt:tag('tr')				local prev = i == 1 and lang or lects[i - 1]				if prev.name == lect.name then					nameRowspan = nameRowspan + 1					nameCell:attr('rowspan', nameRowspan)				else					nameRowspan = 1					nameCell = subRow:tag('td'):wikitext( string.format('%s', lect.link, lect.name) )				end				subRow:tag('td'):wikitext(' ')			end		end	end	return tostring(wt) end

return p