Module:Table Fish: Difference between revisions

From Eco - English Wiki
[unchecked revision][unchecked revision]
m (Fix logic error.)
m (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")


--- Create a HTML table with details on fish type animals.
--- Create a HTML table with details on fish type animals.
Line 7: Line 8:
-- @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 21: Line 22:
-- Create the table rows.
-- Create the table rows.
for _, animalName in ipairs(sortedNames) do
for _, animalName in ipairs(sortedNames) do
data = animals[animalName]
data = AnimalData.animals[animalName]
 
if AnimalUtils.isFishAnimal(data) then
if AnimalUtils.isFishAnimal(data) then
AnimalUtils.insertAnimalListDataRow(tbl, animalName, data)
AnimalUtils.insertAnimalListDataRow(tbl, animalName, data)

Revision as of 22:27, 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")

--- 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\">" }
	-- Temporary variable to store animal data in.
	local data;

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

	-- Create the table rows.
	for _, animalName in ipairs(sortedNames) do
		data = AnimalData.animals[animalName]

		if AnimalUtils.isFishAnimal(data) then
			AnimalUtils.insertAnimalListDataRow(tbl, animalName, data)
		end
	end

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

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

return p