Module:Biota infobox/test

From Wikipedia, the free encyclopedia
require('strict')
local getArgs = require ('Module:Arguments').getArgs;

local p = {}  -- exposed variables

--local pargs = {}  
local template = {} 
local args = {}

-- parse the taxon list and get the taxon parameter and authority from the lowest rank (clear the parameters)
function p.get_taxon(frame, option)
	args['taxon'] = "taxon name"
	local ranks = require ('Module:Biota_infobox/data').taxonRanks;  -- get the list of rank parameters used by manual taxoboxes
	
	-- interate throught the rank parameters, setting taxon to each in turn. 
	-- The lowest rank parameter should hold the taxon name for the taxoox 
	local rank
	for k,v in pairs(ranks) do
		if args[v] then                                     -- if rank parameter
			rank = v
			args['taxon'] = args[rank]:gsub("[%[%]%']", "")    -- strip wiki formatting and set taxon parameter 
			--args['rank'] = rank
			args['authority'] = args[rank.."_authority"] or "" -- set authority
			args[rank] = nil
		end
	end
	args[rank.."_authority"] = nil -- delete the authority of taxon (e.g. familia_authority for family) but keep parent taxon authorities if provided
end

-- remove the taxon parameters apart from genus and species
function p.clear_taxon_parameters (frame)
	local ranks = require ('Module:Biota_infobox/data').taxonRanks;  -- get the list of rank parameters used by manual taxoboxes
    for k,v in pairs(ranks) do
    	if v ~= "genus" and v ~= "species" then
    		args[v] = nil
    	end
    end
end

-- add the parameter line with appropriate stripping of wikitext 
function p.addParam(param)

    if args[param] then
        local value =  args[param]
 		
 		if param == 'genus' then 
			value = value:gsub("%'",""):gsub("%[",""):gsub("%]","")
			--value='Hypsiboas'
		elseif param == 'species' then
			--value = value:gsub("[%'%[%]]","")
			value = value:gsub("[%'%[]*[A-Za-z%.]* ([A-Za-z]*)[%]%']*","%1")
			--value='albopunctatus'
		elseif param == 'binomial_authority' then
			param = 'authority'
			args['binomial_authority']=nil
			args['binomial']=nil             -- not needed
		end
		
		if param == 'name' and args['name'] == args['taxon'] then
			-- name parameter not need if same as taxon
			-- TODO not needed if page title either
		else
	    	table.insert (template, '| ' .. param .. ' = ' .. value );
		end	   	
        args[param]=nil -- remove handled parameters
    end
end 

function p.convert(frame)
	--args = frame:getParent().args
	args = getArgs (frame);
    local mode = "speciesbox"
	if args['genus'] and args['species'] then
		table.insert (template, '{{Speciesbox ' );	
		p.clear_taxon_parameters(frame)
	else
		table.insert (template, '{{Automatic taxobox ' );	
		mode = "auto"
		args['taxon'] = "test taxon"
		p.get_taxon(frame)
	end
	
    p.addParam('name')
    p.addParam('fossil range')
    p.addParam('status')
    p.addParam('status_system')
    p.addParam('status_ref')
    p.addParam('status2')
    p.addParam('status2_system')
    p.addParam('status2_ref')
    p.addParam('trend')
    p.addParam('image')
    p.addParam('image_upright')
    p.addParam('image_alt')
    p.addParam('image_caption')
    p.addParam('image2')
    p.addParam('image2_upright')
    p.addParam('image2_alt')
    p.addParam('image2_caption')
    p.addParam('classification_status')
    p.addParam('diversity')
    p.addParam('diversity_ref')
    p.addParam('diversity_link')
    p.addParam('extinct')

    p.addParam('taxon')
    p.addParam('authority')
    p.addParam('genus')
    p.addParam('species')
    p.addParam('binomial_authority')

    p.addParam('subdivision_ranks')
    p.addParam('subdivision')
    p.addParam('subdivision_ref')
    p.addParam('type_strain')
    p.addParam('type_strain_ref')
    p.addParam('range_map')
    p.addParam('range_map_upright')
    p.addParam('range_map_alt')
    p.addParam('range_map_caption')
    p.addParam('range_map2')
    p.addParam('range_map2_upright')
    p.addParam('range_map2_alt')
    p.addParam('range_map2_caption')
    p.addParam('range_map3')
    p.addParam('range_map3_upright')
    p.addParam('range_map3_alt')
    p.addParam('range_map3_caption')
    p.addParam('range_map4')
    p.addParam('range_map4_upright')
    p.addParam('range_map4_alt')
    p.addParam('range_map4_caption')
    p.addParam('synonyms_ref')
    p.addParam('synonyms')
    --p.addParam('')

	-- required parameters should have been set
	-- all others can be discarded
    local testing = true
	for k,v in pairs(args) do
		if testing == true then
		    if k ~="nosubst" then
		    	table.insert (template, table.concat ({' | '..k..'=', v }));	  -- show any unhandled parameters
		    end
		else
		    if k ~="nosubst" then
		    	args[k] = nil                                                 -- discard unwanted parameters
		    end
		end
	end

	table.insert (template, '}}');												-- close the template
	
	--return '<pre>' .. table.concat (template, '\n') .. '</pre>';
	 --return '\n<code><nowiki>{{Speciesbox'  ..  table.concat (template, '\n') .. '}}</nowiki></code>'
 	
 	local output =  table.concat (template, '\n')
 	if args['nosubst'] == "yes" or args['nosubst'] == "code" then
    	--return table.concat ({'<code>', frame:callParserFunction ('#tag:nowiki', output), '</code>'})
    	return table.concat ({'<pre>', output, '</pre>'})
 	elseif  args['nosubst'] == "preview" then
		return frame:preprocess(output);
    else
        return output;
    end
end

return p