มอดูล:Biota infobox/core

require('Module:No globals')
local p = {}
local templateArgs = {}
local info = {}
local paramData = require( 'Module:Biota infobox/data' ) -- contains the taxon ranks in order
local autotaxa = require("Module:Autotaxobox")
local parameters = require( 'Module:Biota infobox/param' ) 
-- ##################### CORE FUNCTIONS ###################################
this core function emulates Template:Taxobox/core 
it is followed by functions handling the different type of entry
p.main = function(frame)
	-- this function emulates Template:Taxobox/core when called from taxobox feeder templates
	--  i.e. Taxobox, Automatic taxobox, Speciesbox, etc
	parameters.getArgs(frame) 
	templateArgs'image_upright' = templateArgs'upright' or 1   
templateArgs'image2_upright' = templateArgs'upright2'  or 1
info.headerColor = templateArgs'colour'
	return p._core
--TODO check it works
end
-- this is the core function, called either from main() or auto()
p.core = function(frame, args, localinfo) 
	templateArgs = args
	info = localinfo
	return p._core(frame) 
end
p._core = function(frame) 
	                                    -- TODO use mw.title.getCurrentTitle().basePageTitle
	info.name = templateArgs'name' or tostring( mw.title.getCurrentTitle() ) --.rootPageTitle )
info.headerColor = p.getTaxoboxColor(frame) -- so only transverse taxobox heirarchy once
	-- create table (two versions)
	     1. use mwhtml library to build table in taxoboxTable
	     2. use wikitest to build table in wikitextTable

	local taxoboxTable = mw.html.create('table'):addClass('infobox'):addClass('biota')
								                :addClass('biota-infobox')
	local wikitextTable = '\n'
local output =	wikitextTable            -- output the wikitext table
--local output = tostring(taxoboxTable)    -- output the mw.html table
	
	return p.templateStyle( frame, src ) ..  output .. info.parameterCategory 
	            -- .. (info.parameterCategory or "")
	
end  -- End the main function.	
-- ================================================================================**
function to add conservation sections 
uses template  }}
--
function p.addStatusSection(frame, taxoboxTable, status)
-- must use table container to handle template output
-- don't use header or content cells; the 'taxobox/species' template handles it
-- just need to add background colour on the row
local wikiText = ""
if templateArgsstatus and templateArgsstatus ~= "" then

		local status = frame:expandTemplate{ title = 'taxobox/species', 
									          args = {templateArgsstatus..'_system' or ,
									    		      templateArgsstatus or ,
									    		      templateArgsstatus..'_ref' or ,
									    		      extinct=templateArgs'extinct' or   
									    		      }
	          	                        }
		local row = taxoboxTable:tag('tr')
		                                  --:cssText('background:' ..  p.getTaxoboxColor(frame) .. ';')
		local cell = row:tag('td'):attr('colspan', '2')
		           :wikitext('\n')

wikiText = '\n
.. '\n' ..tostring(status)
end
return wikiText 
end
-- ==========================================================================**
function to add sections for taxonomic information with authorities ('_authority' suffix)
e.g. binomial, trinomial, type_species, type_genus; 
diversity also handled here
--
function p.addTaxonSection(frame, taxoboxTable, target, headerText)
	
	local wikiText = ""
-- return if we don't have value
	if not templateArgstarget  or templateArgstarget ==  then return "" end
	
	local bold = "" -- variable for bolding binomial and trinomial (type genus/species etc are not bolded)
	local showHeader = true
	
	-- custom processing section
	--if target == 'binomial' or target == 'binomial2' or target == 'binomial3' or target == 'binomial4' then
	if target == 'binomial' then
		headerText =  .. headerText .. 
		bold =""
	--elseif target == 'trinomial' or target == 'trinomial2' or target == 'trinomial3' or target == 'trinomial4' then
	elseif target == 'trinomial'  then
		local trinomenLink = "Trinomen" -- for zoological or default
		if info.auto ==  "infraspeciesbox" then trinomenLink = "Infraspecific name (botany)" end -- regnum contains plant/fung/Archaeplastida"
		headerText =  .. headerText .. 
		bold =""
	elseif target == 'binomial2' or target == 'binomial3' or target == 'binomial4'
		   or target == 'trinomial2' or target == 'trinomial3' or target == 'trinomial4' then
		showHeader = false
		bold =""
	else
	   --TODO is any handling needed for diversity etc	
	   headerText =  .. headerText .. 
	end

	if templateArgstarget..'_ref' then
			headerText = headerText .. templateArgstarget..'_ref' .. '\n'
	 end
	
	local contentString =    -- content for the content cell
	if target == 'diversity' and templateArgs'diversity' ~= "" then
		if templateArgstarget..'_link' and templateArgstarget..'_link' ~= "" then 
		    contentString =  '\n' .. templateArgstarget..'_link' .. '|' .. templateArgstarget .. '\n' 
		else
			contentString =  '\n' .. templateArgstarget .. '\n'
		end
	 else
	 	-- taxon name and authority (binomial, type species, etc)
		local authorityString = 
		if templateArgstarget..'_authority' then
			authorityString =  .. templateArgstarget..'_authority' ..  -- \n' 
		end 
		contentString =  bold .. templateArgstarget .. bold .. authorityString .. '\n' 
		--contentString =  '\n' ..  bold .. templateArgstarget .. bold .. authorityString .. '\n'  -- extra  inserted
end 	 
	
	-- add table rows
	if templateArgstarget and templateArgstarget ~=  then
		if showHeader then
			local rowHeader = taxoboxTable:tag('tr')
			rowHeader  :tag('th')
			           :attr('colspan', '2')
			           :addClass('section-header')
			           :cssText('background:' ..  info.headerColor .. ';')
			           :wikitext( headerText )                   
	wikiText = wikiText .. '\n
		   .. '|' .. headerText
end
		
		local rowList = taxoboxTable:tag('tr')
		rowList    :tag('td')
		           :attr('colspan', '2')
		           --:addClass('section-content')
		           :addClass('taxon-section')
		           --:cssText('text-align:center;') --font-weight:bold;')
		           :wikitext( contentString )
wikiText = wikiText .. '\n

	end	
return wikiText
end
--s) groups 
- for general taxobox it has subdivision and possible subdivision (disabled here)
- any can be modified for other purposes (e.g. sisters) by changing the header taxt with a -text parameter

function p.addListSection(frame, taxoboxTable, target, headerText)

if not templateArgstarget or templateArgstarget == "" then return "" end -- redundant for now

local wikiText = ""
local refString = 
	if templateArgstarget..'_ref' then
		   --add '_ref' option for 'synonym_ref' (the rest get it as a bonus) TODO check example
			refString = templateArgstarget..'_ref' --.. '\n' 	
	 end
	local listHeader = (templateArgstarget..'_text' or headerText) .. refString
	-- add table rows
	if templateArgstarget and templateArgstarget ~= then
		local rowHeader = taxoboxTable:tag('tr')
		rowHeader  :tag('th')
		           :attr('colspan', '2')
		           :addClass('section-header')
		           :cssText('background:' .. info.headerColor .. ';')
		           :wikitext(listHeader)
wikiText = wikiText .. '\n
.. '|' .. listHeader

		local rowList = taxoboxTable:tag('tr')
		rowList    :tag('td')
		           :attr('colspan', '2')
		           :addClass('section-content')
		           :wikitext('\n' .. templateArgstarget .. '\n' )	
wikiText = wikiText .. '\n
.. '|' .. '\n' .. templateArgstarget .. '\n'
	 end	
	 return wikiText
end
-----------------------------------------------------------------------------------------------
-- ========================================================================**
function to add image sections
- used for illustrative images at top and for range maps
--
function p.addImageSection(frame, taxoboxTable, target)
	 
	 local wikiText = ""
	 if templateArgstarget and templateArgstarget ~=  then
	 	
local imageWidth = 
local imageCaptionString = 
if templateArgstarget..'_caption' then 
	--imageCaptionString =  .. templateArgstarget .. '_caption' ..  
	--imageCaptionString =  .. templateArgstarget .. '_caption' ..  
	imageCaptionString = templateArgstarget .. '_caption'
end
local imageAltString = 
if templateArgstarget..'_alt' then
	imageAltString = templateArgstarget..'_alt'
elseif templateArgstarget..'_caption' then
	imageAltString = templateArgstarget..'_caption' -- use caption for alt text if none provided
end 
local upright = templateArgs'image_upright' or 1

local InfoboxImage = require( 'Module:InfoboxImage' ) --, parent.argstarget  )
local params = { args = { 	image = templateArgstarget,
							        size = templateArgstarget..'_width',
							        sizedefault = 'frameless',
							        alt = imageAltString,
							        upright = templateArgstarget..'_upright' or upright 
							        }
					    }
					    
local image = InfoboxImage.InfoboxImage(  params )

	    --local rowImage = taxoboxTable:tag('tr') 
	    local rowImage = mw.html.create('tr')
		rowImage:tag('td')
					 :attr('colspan', '2')
		             :addClass("image-section")
		             :wikitext(image)
		             --:wikitext(imageCaptionString)
		taxoboxTable:node(rowImage)
wikiText= wikiText .. '\n
	    
	    if imageCaptionString ~= "" then                  -- only insert row if caption string
		    local rowImageCaption = mw.html.create('tr')
			rowImageCaption:tag('td')
						 :attr('colspan', '2')
			             :addClass("image-section")
			             --:wikitext(image)
			             :wikitext(imageCaptionString)
	        taxoboxTable:node(rowImageCaption)
	        wikiText = wikiText ..  '\n
end 
end

-- TODO handle upright
	return wikiText
end 
-- ============================ TAXONOMY SECTION ===================================**
adds a table showing the taxonomy
- uses either manual parameters or the automatic taxonomy system
- currently adds a table inside a cell (like paraphyletic group) rather than just adding rows (core taxobox system)
--    

function p.addTaxonomySection(frame, taxoboxTable)

local wikiText = ""
	local taxonomyHeader = "การจำแนกชั้นทางวิทยาศาสตร์"
	if templateArgs'virus_group'  then 
		taxonomyHeader = "Virus classification"
	elseif templateArgs'ichnos'  then 
		taxonomyHeader = "Trace fossil classification"
	elseif templateArgs'veterovata'  then 
		taxonomyHeader = "Eggshell classification"
	else
		-- TODO add other options (DONE but not verified ichnos or veterovata)
		-- ! colspan=2 style="min-width:15em; text-align: center" | 
		--    
	--	    
		-- handle |classification_status=disputed (add ref)
	end
	
	-- add symbol and link to taxonomy editor
	local editLink = 
	if info.auto then
	    local tooltip = templateArgs'edit link' or "Edit this classification"
		editLink  = 
			.. 'File:Red Pencil Icon.png|link=Template:Taxonomy/' .. templateArgs'parent'
			.. '|' .. tooltip  .. 
	end
	local status = ""
	if templateArgs'classification_status' then 
		status = '(' .. templateArgs'classification_status' .. ')'
	    if templateArgs'classification_ref' then status = status .. templateArgs'classification_ref' end
		status = status .. 
	end
	
	local rowTaxonomyHeader = taxoboxTable:tag('tr')
	rowTaxonomyHeader:tag('th')
	           :attr('colspan', '2')
	           :addClass('section-header')
	           :cssText('background:' ..  info.headerColor .. ';')
	           :wikitext(taxonomyHeader)
	           :wikitext(editLink)
	           :wikitext(status)

wikiText = wikiText .. '\n
.. '|' .. taxonomyHeader .. editLink .. status

-- get taxonomy list, either using automatic taxobox system or manual system       
	if info.auto then 
		-- get automatic taxonomy hierarchy (three different variants)
		      1) direct call to taxoboxList() - preferred if issues with setting frame arguments can be resolved
		      2) experimental version of taxoboxList()
		      3) ET using Template:Ttaxobox/taxonomy
		
	    wikiText = wikiText ..'\n
	    --wikiText = wikiText .. p.addAutomaticTaxonomy2(frame, taxoboxTable)   -- use #invoke of modified module
	    --wikiText = wikiText .. p.addAutomaticTaxonomyET(frame, taxoboxTable)  -- use expandTemplate
	    
	    -- use manual taxobox for genus, subgenus,species, subspecies, variety
	    local taxonRanks = { 'subgenus', 'species', 'hybrid',  'subspecies', 'variety' } 
	    wikiText = wikiText .. p.addManualTaxonomy(frame, taxoboxTable, taxonRanks)
	else
		wikiText = wikiText .. p.addManualTaxonomy(frame, taxoboxTable, paramData.taxonRanks) -- just add rows
end
return wikiText
end
-------------------------------------------------AUTOMATIC TAXONOMY (using invoke of module function) -----------
function p.addAutomaticTaxonomy(frame, taxoboxTable) -- use invoke of module function
	--emulate template: 
	--which uses