Module:Sandbox/Jts1882/Speciesbox

From WikiProjectMed
Jump to navigation Jump to search
require('strict')
-- All Lua modules on Wikipedia must begin by defining a variable that will hold their
-- externally accessible functions. They can have any name and may also hold data.
local p = {}  -- exposed variables
local g = {}  -- these are variables with global scope in this module
local info = {}

local parameters = require( 'Module:Sandbox/Jts1882/Biota infobox/param' ) 
local autotaxa = require("Module:Autotaxobox")

function p.main(frame)
    info.auto = "speciesbox" 
    g.args = {}
   
   --[[ get parameters, check for aliases, that they are valid for species box, etc
        uses Module:Biota infocore/param ]]
    parameters.getArgs(frame, g.args, info) 
    
    -- process the speciesbox parameters that must be changed before passing to taxobox/core
    p.speciesbox(frame)
   
    -- pass the processed arguments to the Template:Taxobox/core
    return frame:expandTemplate { title = 'Taxobox/core' , args = g.args }

end

function p.speciesbox(frame)

    g.args['display_taxa']  = g.args['display_parents'] or 1  -- note change of parameter name 
   
    local extinct = ""
 	if  g.args['extinct']  then 
		--extinct = "†"
		extinct = frame:expandTemplate{ title = 'extinct' }  -- use template to get tooltip
		-- speciesbox also checks the genus taxonomy template for extinct parameter
    end
    
        --[[ {{speciesbox}} gets genus and species from taxon, genus+species or page name
                1. uses 'taxon' paramter ( given as binomial) if available
                2. otherwise uses 'genus' and 'species' parameters
                3. uses page name
             the genus is used for the 'parent' taxon 
            	unless the parent is supplied (e.g. for subgenus)
            	else use genus (from taxon or genus parameter or page name)
            	
           {{Speciesbox}} now using {{Speciesbox/getGenus}} and  {Speciesbox/getSpecies}}
                code doing similar is commented out below
           
           TODO use {{{{Speciesbox/name}}
        --]]
       local genus, species = "", ""
       
       genus = frame:expandTemplate{ title = 'Speciesbox/getGenus' , args = {g.args['taxon'], g.args['genus']} }
       species = frame:expandTemplate{ title = 'Speciesbox/getSpecies' , args = {g.args['taxon'], g.args['genus']} }
       
		if g.args['taxon'] then
           
           -- following line disableas using getGenus/getSpecies templates	       
           -- genus, species = string.match(templateArgs['taxon'], "(%S+)%s(%S+)") -- %S: All characters not in %s
	       
	       g.args['genus'] = genus                 
	       g.args['species'] = species            
	   
	    elseif g.args['genus'] and g.args['species'] then
	    	
	    	--[[strip off (disambiguator) to handle multi-kingdom genus e.g.| genus = Acanthocarpus (plant)
	    	local genusParts =mw.text.split( templateArgs['genus'], " ", true )     -- string.match( s, '^%a*'', 1 )
	    	                                    
	    	if genusParts[1] ~= "" then 
	    		--templateArgs['parent']=templateArgs['genus']  -- set parent (NO, parent should override)
	    		genus = genusParts[1] 
	    	end
	    	now handled by getGenus/getSpecies templates --]]
	    	
	    	g.args['taxon'] = genus .. ' ' .. g.args['species']
	
	    else
	    	-- TODO no valid taxon yet; use page name
	    	-- use first word of pagename - handled by {{Speciesbox/getGenus}}
	    end
    
        if not g.args['parent'] or g.args['parent'] == "" then
        	g.args['parent'] = g.args['genus']       -- set parent to genus if not supplied
        end
        --[[if not templateArgs['name'] or templateArgs['name'] == "" then -- if page name not set
        	templateArgs['name'] = "''" .. templateArgs['taxon'] .. "''"
        end    ]]    	
        --TODO use {{Speciesbox/name}}
        g.args['name']  = frame:expandTemplate{ title = 'Speciesbox/name' , 
        	                           args = { g.args['name'], g.args['taxon'], 
        	                                    g.args['genus'], g.args['species'],
        	                                    mw.title.getCurrentTitle().baseText,
        	                                    g.args['italic_title' or 'yes']  
        	          	
        	          } }
            

        
        
		-- set binomial : the speciesbox template seems to use genus and species before taxon name
		-- "| binomial = ''{{Str letter/trim|{{{genus|{{{taxon|<includeonly>{{PAGENAME}}</includeonly><noinclude>Acacia</noinclude>}}}}}}}} {{{species|{{remove first word|{{{taxon|<includeonly>{{PAGENAMEBASE}}</includeonly><noinclude>Acacia aemula</noinclude>}}}}}}}}''"
		-- documentation suggest taxon, which is followed here
		g.args['binomial'] = "''" .. g.args['taxon'] .. "''"
		g.args['binomial_authority'] = g.args['authority'] or nil
				

    	-- set species_name e.g. Panthera leo -> P. leo
    	g.args['species_name'] = extinct .. "'''''" .. string.sub(g.args['genus'],1,1) .. '. ' .. g.args['species'] .. "'''''"
        g.args['species'] = g.args['species_name']
        
        g.args['display_taxa']   = g.args['display_taxa'] -1
        g.args['offset'] = 1
	    if g.args['subgenus'] and g.args['subgenus'] ~= ""  then
	    	g.args['offset'] =  g.args['offset'] + 1
			g.args['subgenus_authority']              = g.args['parent_authority'] or ""
    	end

        g.args['taxon'] = nil -- no longer needed
        g.args['genus'] = nil -- {{Taxobox/core}} uses parent
 
     g.args['colour'] = p.getTaxoboxColor(frame)

end

function p.getTaxoboxColor(frame)
	
	local colorAs = g.args['color_as'] or nil
	
	if  info.auto and not g.args['virus_group']  then 
	   --[[(1) if color_as|colour_as|color as|colour as set, use template {{Taxobox colour|color_as}}
	   	   (2) else use the auto taxonnomy tree to find colour: {{#invoke:Autotaxobox|taxoboxColour| }}
	    {{#invoke:Autotaxobox|taxoboxColour|{{{parent|{{{genus|{{first word|{{{taxon|{{PAGENAME}} }}
	--]]

		if colorAs then 
			
		    return frame:expandTemplate{ title = 'Taxobox colour', args = {colorAs} }
		    
		else
			-- us #invoke:Autotaxobox|taxoboxColour|{{{parent}}} [parent should be set]
			
			frame.args[1] = g.args['parent']
			
	        return autotaxa.taxoboxColour(frame)
		end

	end
end


return p