Module:Table Animals: Difference between revisions

From Eco - English Wiki
[unchecked revision][unchecked revision]
m (Remove unused require, add documentation.)
(Use mw.loaddata to load data instead of require, add error checking.)
Line 2: Line 2:
local Utils = require("Module:Utils")
local Utils = require("Module:Utils")
local AnimalUtils = require("Module:UtilsAnimalLists")
local AnimalUtils = require("Module:UtilsAnimalLists")
local AnimalData = mw.loaddata("Module:AnimalData")


local function addFootnotes(t)
local function addFootnotes(t)
Line 19: Line 20:
-- @author User:Demian
-- @author User:Demian
function p.main()
function p.main()
-- Import the data.
assert(AnimalData.animals , "Failed to data from Module:AnimalData!")
local animals = require("Module:AnimalData").animals
-- Sort animals by their name so the generated table will be pre-sorted on page load.
-- Sort animals by their name so the generated table will be pre-sorted on page load.
local sortedNames = Utils.getSortedKeys(animals)
local sortedNames = Utils.getSortedKeys(AnimalData.animals)
-- Table to insert data into.
-- Table to insert data into.
local tbl = { "<table class=\"wikitable sortable\">" }
local tbl = { "<table class=\"wikitable sortable\">" }
Line 31: Line 32:
-- Create the table rows.
-- Create the table rows.
for _, animalName in ipairs(sortedNames) do
for _, animalName in ipairs(sortedNames) do
AnimalUtils.insertAnimalListDataRow(tbl, animalName, animals[animalName])
AnimalUtils.insertAnimalListDataRow(tbl, animalName, AnimalData.animals[animalName])
end
end



Revision as of 22:21, 24 February 2022

Template:Doc/start Template:Template backend It creates an HTML table with data from Module:AnimalData

This module uses the following modules:

Template:Doc/end


local p = {}
local Utils = require("Module:Utils")
local AnimalUtils = require("Module:UtilsAnimalLists")
local AnimalData = mw.loaddata("Module:AnimalData")

local function addFootnotes(t)
	-- Sadly I could not find a way to add HTML to the table header generated with the Lua code below.
	-- <a id=\"cite_ref-1\" class=\"reference\" href=\"#cite_note-1\">[1]</a>
	-- <a id=\"cite_ref-2\" class=\"reference\" href=\"#cite_note-2\">[2]</a>
	table.insert(t, "<strong>Footnotes</strong>")
	table.insert(t, "<div class=\"mw-references-wrap\">")
	table.insert(t, "<ol class=\"references\">")
	table.insert(t, "<li id=\"cite_note-1\"><span class=\"mw-cite-backlink\"><a href=\"#cite_ref-1\" aria-label=\"Jump up\" title=\"Jump up\">&uarr;</a></span> <span class=\"reference-text\">Calories consumed by predator animals who eat this animal. Not applicable to players, see the <strong>Harvest Item</strong> column for details on the player consumable food items.</span></li>")
	table.insert(t, "<li id=\"cite_note-2\"><span class=\"mw-cite-backlink\"><a href=\"#cite_ref-2\" aria-label=\"Jump up\" title=\"Jump up\">&uarr;</a></span> <span class=\"reference-text\">The minimum and maximum delay between attacks. Lower values equal faster attacks.</span></li>")
	table.insert(t, "</ol></div>")
end

--- Create a HTML table with details on fish type animals.
-- @return #string HTML table in a string.
-- @author User:Demian
function p.main()
	assert(AnimalData.animals , "Failed to data from Module:AnimalData!")
	
	-- Sort animals by their name so the generated table will be pre-sorted on page load.
	local sortedNames = Utils.getSortedKeys(AnimalData.animals)
	-- Table to insert data into.
	local tbl = { "<table class=\"wikitable sortable\">" }

	-- Create the table header.
	AnimalUtils.insertAnimalListHeader(tbl)

	-- Create the table rows.
	for _, animalName in ipairs(sortedNames) do
		AnimalUtils.insertAnimalListDataRow(tbl, animalName, AnimalData.animals[animalName])
	end

	table.insert(tbl, "</table>")

	return table.concat(tbl," \n")
end

return p