Jump to content

Module:Sandbox/trappist the monk/taxonomy

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 19:27, 10 October 2021 (create;). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
require('Module:No globals');


--[[--------------------------< T A X O N _ G E T >------------------------------------------------------------

fetch a taxon table from the appropriate Module:Sandbox/trappist the monk/taxonomy <letter> date module where
<letter> is the uppercase first letter of <taxon> or 'symbol' when the first catacter is not a letter

]]

local function taxon_get (taxon)
local taxonomy = {};
	local letter = taxon:match ('^%a');
	letter = (letter and letter:upper()) or 'symbols';

	local ok, taxonomy = pcall (mw.loadData, 'Module:Sandbox/trappist the monk/taxonomy ' .. letter)

	if ok then
		return taxonomy[taxon];
	else
		error (taxon)
		return nil;
	end
end


--[[--------------------------< C R A W L _ T R E E >----------------------------------------------------------

experimental function to see if it is possible / makes sense to replace 87k+ taxonomy templates with lua data modules

]]

local function crawl_tree (frame)
	local node = frame.args[1];
	local tree = {};															-- collect the taxonomy tree here

	local taxon_t = taxon_get (node);											-- initialize
	
	while taxon_t and node do
		if taxon_t.same_as then													-- if this node redirects to another taxon via |same_as=; TODO: should this loop?  multiple |same_as= possible?
			taxon_t = taxon_get (taxon_t.same_as);								-- follow the redirect
		end
		
		if taxon_t.rank then													-- nil for Taxonomy/Life
			table.insert (tree, 1, taxon_t.rank .. ': ' .. node);
			if taxon_t.parent then
				node = taxon_t.parent;											-- get the next node
				taxon_t = taxon_get (node);										-- and get its taxon table
			else
				node = nil;														-- no next node
			end
		else
			node = nil;															-- no next node
		end
	end
																				-- render crude tree and taxonomy list from Module:Autotaxobox for comparison
	return '{|\n|-\n|' .. table.concat (tree, '<br />') .. ' || ' .. frame:callParserFunction ('#invoke', {'Autotaxobox', 'taxonomyList', frame.args[1]}) .. '\n|-\n|}';
end


--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------


]]

return {
	crawl_tree = crawl_tree,
	}