Module:Infobox Species

From AEGIS
Revision as of 19:57, 28 March 2024 by Pooka (Pookaw) (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Infobox Species/doc. Changes can be proposed in the talk page.
Function list
L 6 — systemLink
L 13 — speciesLink
L 19 — nationsLink
L 25 — Species.main

local Species = {}
local infobox = require( 'Module:Infobox' ):new()
local hatnote = require( 'Module:Hatnote' )._hatnote
local data = mw.loadJsonData( 'Module:System/data.json' )

function systemLink(systemid, systemname)
	local urlName = string.gsub(systemname, "%s+", "_")
	systemid = string.gsub(systemid, "%s+", "")
	local link = "<span class='plainlinks'>[" .. mw.site.server .. "/index.php?action=edit&redlink=1&preload=Template:Systems/Preload&preloadparams%5B%5D=" .. systemid .. "&title=" .. urlName .. " " .. systemname .. "]</span>"
	return link
end

function speciesLink(speciesname)
	local urlName = string.gsub(speciesname, "%s+", "_")
	local link = "<span class='plainlinks'>[" .. mw.site.server .. "/index.php?action=edit&redlink=1&preload=Template:Species/Preload&title=" .. urlName .. " " .. speciesname .. "]</span>"
	return link
end

function nationsLink(nationname)
	local urlName = string.gsub(nationname, "%s+", "_")
	local link = "<span class='plainlinks'>[" .. mw.site.server .. "/index.php?action=edit&redlink=1&preload=Template:Nations/Preload&title=" .. urlName .. " " .. nationname .. "]</span>"
	return link
end

function Species.main(frame)
	
	local parentFrame = frame:getParent()
	local parentArgs = parentFrame.args
	
    if parentArgs.name == nil then
		return hatnote( mw.ustring.format(
				'A required field (%s) is missing!',
				'name'
			),
			{ icon = 'WikimediaUI-Error.svg', extraclasses = 'error' }
		)
    end
	
	local basicsTable = {}
	local nationsTable = {}
	local speciesTable = {}

	-- Check if required fields are present (name) --

	if parentArgs.image == nil then
		infobox:renderImage( 'Aegis50.svg' )
	else 
		infobox:renderImage( parentArgs.image )
	end

	-- Check if aka exists, if so, prepend label --

	local aka = nil
	if parentArgs.aka ~= nil then
		aka = "<span class='aka'>aka:</span> " .. parentArgs.aka
	end
	
	-- Render Header Box --

	infobox:renderHeader( {
		title = parentArgs.name,
		subtitle = aka,
	} )

	-- Render Basics Table (type, origin) --

	local fieldType = 'Not specified'
	if parentArgs['type'] == nil or parentArgs['type'] == ' ' or parentArgs['type'] == '' then
		fieldType = 'Not specified'
	else 
		fieldType = parentArgs.type
	end

	local fieldOrigin = 'Not specified'
	if parentArgs['origin'] == nil or parentArgs['origin'] == ' ' or parentArgs['origin'] == '' then
		fieldOrigin = 'Not specified'
	else 
		local originId = tonumber( parentArgs.origin )

		if originId == nil then
			fieldOrigin = 'Invalid system (' .. parentArgs.origin .. ')'
		else 
			originId = originId + 1
			if data[originId] then
				fieldOrigin = systemLink(parentArgs.origin, data[originId].name)
			else 
				fieldOrigin = 'Invalid system (' .. parentArgs.origin .. ')'
			end
		end
	end

	basicsTable = {
		infobox:renderItem( {
			label = 'Species Type',
			data = fieldType
		} ),
		infobox:renderItem( {
			label = 'System of Origin',
			data = fieldOrigin
		} )
	}
	
	infobox:renderSection( {
		content = table.concat( basicsTable ),
		col = 2
	} )
	
	if parentArgs['nations'] ~= nil and parentArgs['nations'] ~= ' ' and parentArgs['nations'] ~= '' then
		local nationsList = mw.text.split(parentArgs.nations, ',')
		for _, v in pairs( nationsList ) do
			local nationsItem = infobox:renderItem( {
				data = nationsLink(v)
			})
			table.insert(nationsTable, nationsItem)
		end
		infobox:renderSection( {
			title = 'Nations commonly found in',
			content = table.concat( nationsTable ),
			col = 3,
			class = 'smalltitle'
		})
	end

	if parentArgs['related'] ~= nil and parentArgs['related'] ~= ' ' and parentArgs['related'] ~= '' then
		local speciesList = mw.text.split(parentArgs.related, ',')
		for _, v in pairs( speciesList ) do
			local speciesItem = infobox:renderItem( {
				data = speciesLink(v)
			})
			table.insert(speciesTable, speciesItem)
		end
		infobox:renderSection( {
			title = 'Related Species',
			content = table.concat( speciesTable ),
			col = 3,
			class = 'smalltitle'
		})
	end


	return infobox:renderInfobox( nil, parentArgs.name)
end

return Species