Module:Nomenclature

From Zelda Wiki, the Zelda encyclopedia
Revision as of 21:21, 12 August 2019 by MannedTooth (talk | contribs) (created basic tables)
Jump to navigation Jump to search

This is the main module for Template:Nomenclature.

Lua error in package.lua at line 80: module 'Module:Tab2' not found.


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

local p = {}
local h = {}

-- For creating nomenclature tables
function p.Main( frame )
	local cargoData = translation.fetchTranslations(frame.args["term"])
	local skipMeanings = true
	for _, row in ipairs(cargoData) do
		if not utilsCode.IsEmpty(row["meaning"]) then
			skipMeanings = false
			break
		end
	end
	local resultTable = h.CreateTable(skipMeanings)
	local resultTable = h.CreateRows(resultTable, cargoData, skipMeanings)
	return resultTable
end

-- For printing a single row in a translation page
function p.MainRow (frame)
	local resultRow = h.CreateNomenclatureRow(frame.args["game"], frame.args["term"], utilsText.split(frame.args["languages"]), frame.args["image"])
	return resultRow
end

function h.CreateNomenclatureRow(game, term, languages, image)
	local cargoData = translation.fetchTranslationsByGame(game, term)
	h.SortTranslations(cargoData)
	local tr = mw.html.create('tr')
	h.PrintTerm(tr, game, term, image)
	for _, lang in ipairs(languages) do
		h.PrintNomenclatureNames(tr, cargoData, lang)
	end
	
	return tr
end

function h.PrintTerm(tr, game, termToPrint, image)
	tr:tag('td')
		:css{
			["text-align"] = "center"
		}
		:wikitext('[[' .. image .. '|175x175px]]<br><b>' .. term.Main(game, termToPrint, "link", "", "") .. '</b>')
end

function h.PrintNomenclatureNames(tr, cargoData, language)
	tr:tag('td')
		:wikitext(table.concat(h.GetNamesFromLanguage(cargoData, language), '<br>'))
end

function h.GetNamesFromLanguage(cargoData, language)
	local names = {}
	local name = ""
	for _, row in ipairs(cargoData) do
		if row['language'] == language then
			name = row["translation"]
			if not utilsCode.IsEmpty(row["meaning"]) then
				name = "<span class='explain' rel='tooltip' title='" .. row["meaning"] .. "' style='color:Yellow;'>" .. name .. "</span>"
			end
			names[#names+1] = name
		end
	end
	return names
end

--Create an empty table with headers
function h.CreateTable(skipMeanings)
	--Table structure
	local resultTable = mw.html.create("table")
		:addClass("wikitable")
		:css{
			["margin"] = "1em 0",
			["font-size"] = "95%",
		}:done()
	
	--Global header
	local headerRow = mw.html.create("tr"):done()
	local headerContent = mw.html.create("th")
		:wikitext("[[File:TMC Forest Minish Artwork.png|20px]] Names in Other Regions [[File:TMC Jabber Nut Sprite.png]]")
		: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
	if not skipMeanings then
		headerContent = mw.html.create("th")
			:wikitext("Meaning"):done()
		headerRow:node(headerContent)
	end
	
	resultTable:node(headerRow)
	
	return resultTable
end

function h.CreateRows(output, cargoData, skipMeanings)
	h.SortTranslations(cargoData)
	for _, row in ipairs(cargoData) do
		if not row.skip then
			h.ProcessRow(output, cargoData, row, skipMeanings)
		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, skipMeanings)
	local meanings = h.GetMeanings(cargoData, row)
	local tr = output:tag('tr')
	h.PrintFlag(tr, row)
	h.PrintLanguage(tr, row)
	h.PrintNames(tr, cargoData, row)
	h.MarkRowsToSkip(cargoData, row)
	if not skipMeanings then
		h.PrintMeanings(tr, meanings)
	end
	
	
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), '<br>'))
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, '<br>'))
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] = '&nbsp;'
		else
			ret[#ret+1] = v
		end
	end
	return ret
end

-- TRANSLATION PAGES
function p._CreateTranslationTables(frame)
	local args = frame:getParent().args
	
	local tabs = {}
	local index = 0
	while true do
		index = index + 1
		if utilsCode.IsEmpty(args["tab" .. index]) then
			break
		else
			table.insert(tabs, {tabName = args["tab" .. index], tabContent = args["subjects"]})
		end
	end
	
	return p.CreateTranslationTables(args["game"], tabs, args["subjects"])
end

function p.CreateTranslationTables(game, tabs, subjects)
	-- Getting all translations for the game
	local tables = "Translations"
	local fields = "translation, meaning"
	local args = {
		where = "game = '" .. game .. "'"
	}
	local translations = cargo.query(tables, fields, args)
	
	for key, tab in ipairs(tabs) do
		local languages = mw.text.split(tab["tabName"], '%s*,%s*')
		--Creating tab contents
		local headerRow = mw.html.create("tr")
			:node(mw.html.create("th"):wikitext("Subject")):done()
		for key2, language in ipairs(languages) do
			headerRow:node(mw.html.create("th"):wikitext(utilsLanguage.CodeToLanguage(language))):done()
		end
		
		local content = mw.html.create("table"):addClass("wikitable")
			:node(headerRow)
		tab["tabContent"] = tostring(content)
		
		
		-- Formatting tab names
		for key2, language in ipairs(languages) do
			languages[key2] = utilsLanguage.CodeToLanguage(language)
		end
		tab["tabName"] = table.concat(languages, ", ")
	end
	
	return tab2.Main(tabs, 1, "top", #tabs, "", "", "", "left")
end

return p