Module:Nomenclature: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
mNo edit summary
(fix file generation, show warning and category instead of error when term not found)
Line 1: Line 1:
local p = {}
local h = {}
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo
local expgame = require('Module:Exp Game')
local expgame = require('Module:Exp Game')
local tab2 = require('Module:Tab2')
local tab2 = require('Module:Tab2')
local term = require('Module:Term')
local Term = require('Module:Term')
local translation = require('Module:Translation')
local translation = require('Module:Translation')
local utilsCode = require('Module:UtilsCode')
local utilsError = require('Module:UtilsError')
local utilsGame = require('Module:UtilsGame')
local utilsGame = require('Module:UtilsGame')
local utilsLanguage = require('Module:UtilsLanguage')
local utilsLanguage = require('Module:UtilsLanguage')
local utilsMarkup = require("Module:UtilsMarkup")
local utilsString = require("Module:UtilsString")
local utilsTable = require('Module:UtilsTable')
local utilsTable = require('Module:UtilsTable')
local p = {}
local h = {}


-- For creating nomenclature tables
-- For creating nomenclature tables
function p.Main( frame )
function p.Main( frame )
local term = frame.args["term"]
local term = frame.args["term"]
if utilsCode.IsEmpty(term) then
if utilsString.isEmpty(term) then
term = mw.title.getCurrentTitle().subpageText
term = mw.title.getCurrentTitle().subpageText
end
end
Line 25: Line 27:
displayGames = true
displayGames = true
end
end
if not utilsCode.IsEmpty(row["meaning"]) then
if not utilsString.isEmpty(row["meaning"]) then
skipMeanings = false
skipMeanings = false
end
end
Line 160: Line 162:
local refs = h.RefsWithSameTranslation(row, cargoData)
local refs = h.RefsWithSameTranslation(row, cargoData)
for key, ref in ipairs(refs) do
for key, ref in ipairs(refs) do
if not utilsCode.IsEmpty(ref) then
if not utilsString.isEmpty(ref) then
result = result .. mw.getCurrentFrame():extensionTag("ref", ref)
result = result .. mw.getCurrentFrame():extensionTag("ref", ref)
end
end
Line 216: Line 218:
local ret = {}
local ret = {}
for k, v in pairs(meanings) do
for k, v in pairs(meanings) do
if utilsCode.IsEmpty(v) then
if utilsString.isEmpty(v) then
ret[#ret+1] = ' '
ret[#ret+1] = ' '
else
else
Line 233: Line 235:
while true do
while true do
index = index + 1
index = index + 1
if utilsCode.IsEmpty(args["tab" .. index]) then
if utilsString.isEmpty(args["tab" .. index]) then
break
break
else
else
Line 245: Line 247:
function p.CreateTranslationTables(game, filetype, header, tabs, subjects)
function p.CreateTranslationTables(game, filetype, header, tabs, subjects)
subjects = mw.text.split(subjects, '%s*,%s*')
subjects = mw.text.split(subjects, '%s*,%s*')
local translations = translation.fetchTranslationsByGame(game, subjects)
if filetype == "Screenshot" then
if filetype == "Screenshot" then
filetype = ""
filetype = ""
else
filetype = " " .. filetype
end
end
if utilsCode.IsEmpty(header) then
if utilsString.isEmpty(header) then
header = "Subject"
header = "Subject"
end
end
Line 278: Line 277:
--Creating rows
--Creating rows
local errs = {}
local translations = translation.fetchTranslationsByGame(game, subjects)
translations = utilsTable.keyBy("term")(translations)
for key2, subject in ipairs(subjects) do
for key2, subject in ipairs(subjects) do
local row = mw.html.create("tr"):node(mw.html.create("td"):addClass("centered"):wikitext("[[File:" .. game .. " " .. term.fetchTerm({game = game, term = subject}) .. filetype .. ".png|150x150px]]<br><b>" .. term.Main({game = game, term = subject, link = "link"}) .. "</b>"))
local term, err = Term.fetchTerm(subject, game)
for key3, language in ipairs(languages) do
if not term then
outputs = {}
utilsError.warn(string.format("Page %s does not store any terms.", utilsMarkup.code(subject)))
for key4, translation in ipairs(translations) do
errs = utilsTable.concat(errs, err)
if translation["term"] == subject then
else
if translation["language"] == language then
local img = utilsMarkup.gameFile(game, term, filetype, {
table.insert(outputs, translation["translation"])
size = "150x150px"
end
})
end
term = Term.printTerm({
end
page = subject,
game = game,
local cell = mw.html.create("td"):addClass("centered")
link = "link",
if outputs[1] == "N/A" then
})
cell:css("background-color", "var(--zw-dark-2)")
local row = h.printRow(img, term, languages, translations[subject] or {})
else
content:node(row)
cell:wikitext(table.concat(outputs, "<br>"))
end
row:node(cell):done()
end
end
content:node(row)
end
end
tab["tabContent"] = tostring(content) .. "<small>Return to [[#top|top]]</small>"
tab["tabContent"] = tostring(content) .. "<small>Return to [[#top|top]]</small>" .. utilsMarkup.categories(errs)
-- Formatting tab names
-- Formatting tab names
Line 318: Line 314:
return tab2.Main(tabs, 1, "top", #tabs, "", "", "", "left")
return tab2.Main(tabs, 1, "top", #tabs, "", "", "", "left")
end
end
end
 
function h.printRow(img, term, languages, translations)
local row = mw.html.create("tr")
:node(mw.html.create("td")
:addClass("centered")
:wikitext(img .. "<br>" .. utilsMarkup.bold(term)))
for _, language in ipairs(languages) do
outputs = {}
for _, translation in ipairs(translations) do
if translation["language"] == language then
table.insert(outputs, translation["translation"])
end
end
 
local cell = mw.html.create("td"):addClass("centered")
if outputs[1] == "N/A" then
cell:css("background-color", "var(--zw-dark-2)")
else
cell:wikitext(table.concat(outputs, "<br>"))
end
row:node(cell):done()
end
return row
end
end


return p
return p

Revision as of 15:41, 9 April 2020

This is the main module for Template:Nomenclature.

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


local p = {}
local h = {}

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 utilsError = require('Module:UtilsError')
local utilsGame = require('Module:UtilsGame')
local utilsLanguage = require('Module:UtilsLanguage')
local utilsMarkup = require("Module:UtilsMarkup")
local utilsString = require("Module:UtilsString")
local utilsTable = require('Module:UtilsTable')

-- For creating nomenclature tables
function p.Main( frame )
	local term = frame.args["term"]
	if utilsString.isEmpty(term) then
		term = mw.title.getCurrentTitle().subpageText
	end
	local cargoData = translation.fetchTranslations(term)
	local displayGames = false
	local skipMeanings = true
	for key, row in ipairs(cargoData) do
		if cargoData[1]["game"] ~= row["game"] or utilsGame.IsOrHasRemake(row["game"]) == true then
			displayGames = true
		end
		if not utilsString.isEmpty(row["meaning"]) then
			skipMeanings = false
		end
	end
	local resultTable = h.CreateTable(skipMeanings)
	resultTable = h.CreateRows(resultTable, cargoData, skipMeanings, displayGames)
	resultTable:node(mw.html.create("tr"):node(mw.html.create("th"):attr("colspan", "4"):wikitext("<small>This table was generated using [[Data:Translations|translation pages]].<br>To request an addition, please <span class='plainlinks'>[https://discord.gg/eJnnvYb contact]</span> a [[Zelda Wiki:Staff|staff member]] with a [[Guidelines:References|reference]].</small>")))
	return resultTable
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|link=]] Names in Other Regions [[File:TMC Jabber Nut Sprite.png|link=]]")
		: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, displayGames)
	h.SortTranslations(cargoData)
	for _, row in ipairs(cargoData) do
		if not row.skip then
			h.ProcessRow(output, cargoData, row, skipMeanings, displayGames)
		end
	end
	
	return output
end

function h.SortTranslations(translations)
	local lookupLang = utilsTable.hash(utilsLanguage.getCodes())
	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, displayGames)
	local meanings = h.GetMeanings(cargoData, row)
	local langText, flag = utilsLanguage.printLanguage(row.language)
	local tr = output:tag('tr')
		:tag("td")
			:addClass("nomenclature-flag")
			:wikitext(flag)
			:done()
		:tag("td")
			:wikitext(langText)
			:done()
	h.PrintNames(tr, cargoData, row, displayGames)
	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.PrintNames(tr, cargoData, row, displayGames)
	local td = tr:tag('td')
		:wikitext(table.concat(h.GetNamesAndTheirGames(cargoData, row, displayGames), '<br>'))
end

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

function h.GetOneNameAndGames(cargoData, row, displayGames)
	local games = h.GamesWithSameTranslation(row, cargoData)
	local result = row.translation
	if displayGames == true then
		result = result .. " " .. expgame.Display(games)
	end
	
	local refs = h.RefsWithSameTranslation(row, cargoData)
	for key, ref in ipairs(refs) do
		if not utilsString.isEmpty(ref) then
			result = result .. mw.getCurrentFrame():extensionTag("ref", ref)
		end
	end
	return result
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.RefsWithSameTranslation(row, cargoData)
	local ret = {}
	for _, row2 in ipairs(cargoData) do
		if h.SameLangSameTranslation(row, row2) then
			ret[#ret+1] = row2.reference
		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 utilsString.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 utilsString.isEmpty(args["tab" .. index]) then
			break
		else
			table.insert(tabs, {tabName = args["tab" .. index], tabContent = args["subjects"]})
		end
	end
	
	return p.CreateTranslationTables(args["game"], args["filetype"], args["header"], tabs, args["subjects"])
end

function p.CreateTranslationTables(game, filetype, header, tabs, subjects)
	subjects = mw.text.split(subjects, '%s*,%s*')
	
	if filetype == "Screenshot" then
		filetype = ""
	end
	
	if utilsString.isEmpty(header) then
		header = "Subject"
	end
	
	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(header)):done()
		for key2, language in ipairs(languages) do
			local langText, flag = utilsLanguage.printLanguage(language)
			headerRow:node(
				mw.html.create("th")
					:wikitext(flag .. "<br>" .. langText)
					:css("width", 100 / (#languages + 1) .. "%")
				)
				:done()
		end
		
		local content = mw.html.create("table")
			:addClass("wikitable")
			:css("width", "100%")
			:node(headerRow)
		
		--Creating rows
		local errs = {}
		local translations = translation.fetchTranslationsByGame(game, subjects)
		translations = utilsTable.keyBy("term")(translations)
		for key2, subject in ipairs(subjects) do
			local term, err = Term.fetchTerm(subject, game)
			if not term then
				utilsError.warn(string.format("Page %s does not store any terms.", utilsMarkup.code(subject)))
				errs = utilsTable.concat(errs, err)
			else
				local img = utilsMarkup.gameFile(game, term, filetype, {
					size = "150x150px"
				})
				term = Term.printTerm({
					page = subject,
					game = game,
					link = "link",
				})
				local row = h.printRow(img, term, languages, translations[subject] or {})
				content:node(row)
			end
		end
			
		tab["tabContent"] = tostring(content) .. "<small>Return to [[#top|top]]</small>" .. utilsMarkup.categories(errs)
		
		-- Formatting tab names
		for key2, language in ipairs(languages) do
			languages[key2] = utilsLanguage.printLanguage(language, true)
		end
		languages = utilsTable.unique(languages)
		tab["tabName"] = table.concat(languages, ", ")
	end
	
	if #tabs == 1 then
		return tabs[1]["tabContent"]
	else
		return tab2.Main(tabs, 1, "top", #tabs, "", "", "", "left")
	end
end

function h.printRow(img, term, languages, translations)
	local row = mw.html.create("tr")
		:node(mw.html.create("td")
			:addClass("centered")
			:wikitext(img .. "<br>" .. utilsMarkup.bold(term)))
		
	for _, language in ipairs(languages) do
		outputs = {}
		for _, translation in ipairs(translations) do
			if translation["language"] == language then
				table.insert(outputs, translation["translation"])
			end
		end

		local cell = mw.html.create("td"):addClass("centered")
		if outputs[1] == "N/A" then
			cell:css("background-color", "var(--zw-dark-2)")
		else
			cell:wikitext(table.concat(outputs, "<br>"))
		end
		row:node(cell):done()
	end
	return row
end

return p