Module:Nomenclature

local cargo = mw.ext.cargo local translation = require('Module:Translation') local expgame = require('Module:Exp Game') local utilsCode = require('Module:UtilsCode') local utilsGame = require('Module:UtilsGame') local utilsLanguage = require('Module:UtilsLanguage') local utilsTable = require('Module:UtilsTable')

local p = {} local h = {}

function p.Main( frame ) local resultTable = h.CreateTable local resultTable = h.CreateRows(resultTable, frame.args["term"]) return resultTable end

--Create an empty table with headers function h.CreateTable --Table structure local resultTable = mw.html.create("table") :addClass("wikitable") :css{ ["margin"] = "1em", ["font-size"] = "95%", }:done --Global header local headerRow = mw.html.create("tr"):done local headerContent = mw.html.create("th") :wikitext(" Names in Other Regions ") :attr("colspan", "4") :css{ ["font-size"] = "110%", }:done headerRow:node(headerContent) resultTable:node(headerRow) --Individual headers --Language headerRow = mw.html.create("tr"):done headerContent = mw.html.create("th") :wikitext("Language") :attr("colspan", "2"):done headerRow:node(headerContent) --Name headerContent = mw.html.create("th") :wikitext("Name"):done headerRow:node(headerContent) --Meaning headerContent = mw.html.create("th") :wikitext("Meaning"):done headerRow:node(headerContent) resultTable:node(headerRow) return resultTable end

function h.CreateRows(output, term) local cargoData = translation.fetchTranslations(term) h.SortTranslations(cargoData) for _, row in ipairs(cargoData) do		if not row.skip then h.ProcessRow(output, cargoData, row) end end return output end

function h.SortTranslations(translations) local lookupLang = utilsTable.hash(utilsLanguage.GetCodeSortOrder) local lookupGame = utilsTable.hash(utilsGame.GetSortOrder("canon")) table.sort(translations, function (a,b)			if (lookupLang[a.language] or 0) == (lookupLang[b.language] or 0) then				return (lookupGame[a.game] or 0) < (lookupGame[b.game] or 0)			else				return (lookupLang[a.language] or 0) < (lookupLang[b.language] or 0)			end		end	) end

function h.ProcessRow(output, cargoData, row) local meanings = h.GetMeanings(cargoData, row) local tr = output:tag('tr') local td	h.PrintFlag(tr, row) h.PrintLanguage(tr, row) h.PrintNames(tr, cargoData, row) h.MarkRowsToSkip(cargoData, row) h.PrintMeanings(tr, meanings) end

function h.GetMeanings(cargoData, row) local ret = { row.meaning } for _, row2 in ipairs(cargoData) do		if h.SameLangDifTranslations(row, row2) then ret[#ret+1] = row2.meaning end end return ret end

function h.PrintFlag(tr, row) tr:tag('td') :wikitext(utilsLanguage.CodeToFlag(row.language)) end

function h.PrintLanguage(tr, row) tr:tag('td') :wikitext(utilsLanguage.CodeToLanguage(row.language)) end

function h.PrintNames(tr, cargoData, row) local td = tr:tag('td') :wikitext(table.concat(h.GetNamesAndTheirGames(cargoData, row), ' ')) end

function h.GetNamesAndTheirGames(cargoData, row) local ret = { h.GetOneNameAndGames(cargoData, row) } for _, row2 in ipairs(cargoData) do		if h.SameLangDifTranslations(row, row2) then games = h.GamesWithSameTranslation(row2, cargoData) ret[#ret+1] = h.GetOneNameAndGames(cargoData, row2) end end return ret end

function h.GetOneNameAndGames(cargoData, row) local games = h.GamesWithSameTranslation(row, cargoData) return ('%s %s'):format(row.translation, expgame.Display(games)) end

function h.GamesWithSameTranslation(row, cargoData) local ret = {} for _, row2 in ipairs(cargoData) do		if h.SameLangSameTranslation(row, row2) then ret[#ret+1] = row2.game end end return ret end

function h.SameLangSameTranslation(row1, row2) return row1.language == row2.language and row1.translation == row2.translation end

function h.SameLangDifTranslations(row1, row2) return row1.language == row2.language and row1.translation ~= row2.translation end

function h.SameLang(row1, row2) return row1.language == row2.language end

function h.PrintMeanings(tr, meanings) local meaningsDisplays = h.ProcessMeanings(meanings) td = tr:tag('td') :wikitext(table.concat(meaningsDisplays, ' ')) end

function h.MarkRowsToSkip(cargoData, row) for _, row2 in ipairs(cargoData) do		if h.SameLang(row, row2) then row2.skip = true end end end

function h.ProcessMeanings(meanings) local ret = {} for k, v in pairs(meanings) do		if utilsCode.IsEmpty(v) then ret[#ret+1] = ' ' else ret[#ret+1] = v		end end return ret end

return p