Module:Sandbox/AlphaZeta/test4

Source: Wikipedia, the free encyclopedia.
--[[ 
 Infobox biography
 1. Sets default colors and styles
 2. Finds any input fields for death and birth dates 
 	and formats the date according to Swedish standard
 	and calculates and displays age or age at death

]]
local p = {};

-- Default style -------------
local default_bodystyle='border-spacing:3px;width:22em;border:1px solid #AAAAAA; border-top:0px;'
local default_titlestyle='border:1px solid #AAAAAA; border-bottom:0px;background:#ccd9e8;padding-top:5px;padding-bottom:5px;'
local default_labelstyle='' 
local default_headerstyle='background:#BFD2EB;'
local default_belowstyle='background:#BFD2EB;'
------------------------------

local Infobox = require('Module:Infobox')
local DateHelper = require('Module:Sandbox/AlphaZeta/test3')

function p.infobox(frame) 
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        inputargs = frame:getParent().args
    else
        inputargs = frame
    end
    
    -- Set default style
    if inputargs.bodystyle ~= nil then 
    	default_bodystyle = default_bodystyle..inputargs.bodystyle
    end
    inputargs.bodystyle=default_bodystyle
    
    if inputargs.titlestyle ~= nil then 
    	default_titlestyle = default_titlestyle..inputargs.titlestyle
    end
    inputargs.titlestyle=default_titlestyle
    
    if inputargs.labelstyle ~= nil then 
    	default_labelstyle = default_labelstyle..inputargs.labelstyle
    end
   	inputargs.labelstyle=default_labelstyle
   	
    if inputargs.headerstyle ~= nil then 
    	default_headerstyle = default_headerstyle..inputargs.headerstyle
    end
   	inputargs.headerstyle=default_headerstyle
   	
     if inputargs.belowstyle ~= nil then 
    	default_belowstyle = default_belowstyle..inputargs.belowstyle
    end  
	inputargs.belowstyle=default_belowstyle

	-- Check all data fields to find and format
	-- Birthdate (marked with @birth@)
	-- Death date (marked with @death@)
	-- Other dates (marked with @date@)
	local birthDateField
	local deathDateField
	local birth_date=''
	local death_date=''
	for name, value in pairs(inputargs) do
		if (name:sub(1,4)=='data') then
			if value:sub(1,7)=='@birth@' then
				birthDateField=name
				birth_date=value:sub(8)
				inputargs[name]=DateHelper.format(birth_date,'link',true)
			elseif value:sub(1,7)=='@death@' then
				deathDateField=name
				death_date=value:sub(8)	
				inputargs[name]=DateHelper.format(death_date,'link',true)	
			elseif value:sub(1,6)=='@date@' then
				inputargs[name]=DateHelper.format(value:sub(7),'',true)		
			elseif value:sub(1,10)=='@datelink@' then
				inputargs[name]=DateHelper.format(value:sub(11),'link',true)		
			end
		end
	end	
	

	-- calcuate age, either alive or dead
	local ageAlive=''
	local ageDeath=''
	if birth_date~=nil and birth_date~='' then
		if death_date==nil or death_date=='' then
			ageAlive=DateHelper.yearsBetween(birth_date)
			if ageAlive~='' then
				ageAlive=' ('..ageAlive..')'
			end
		else
			ageDeath=DateHelper.yearsBetween(birth_date,death_date)
			if ageDeath~='' then
				ageDeath=' ('..ageDeath..')'
			end
		end
	end
	
	-- add age to date fields
	if birthDateField~=nil then
		inputargs[birthDateField]=inputargs[birthDateField]..ageAlive
	end
	if deathDateField~=nil then
		inputargs[deathDateField]=inputargs[deathDateField]..ageDeath
	end
	return Infobox.infobox(inputargs)
end	
return p