Jump to content

Module:Science redirect/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 20:45, 31 May 2017 (update). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

function p.from_alternative_scientific_name(frame)
	local toFrom = 'from'
	local name = 'alternative scientific name'
	local redirect = 'an alternative scientific name'
	local target = 'the accepted scientific name'
	return p._main(frame, toFrom, name, redirect, target)
end

function p.to_scientific_name(frame)
	local toFrom = 'to'
	local name = 'the scientific name'
	local redirect = 'the [[biological nomenclature|scientific name]]'
	local target = 'a [[Common name|vernacular ("common") name]]'
	return p._main(frame, toFrom, name, redirect, target)
end

function p.from_scientific_name(frame)
	local toFrom = 'from'
	local name = 'a scientific name'
	local target = 'the [[biological nomenclature|scientific name]]'
	local redirect = 'a [[Common name|vernacular ("common") name]]'
	return p._main(frame, toFrom, name, redirect, target)
end

function p.sandbox(frame)
	return self[frame.args[1]](frame)
end

local conf = { -- list entries minus any trailing 's'
	plant      = {'a plant', 'plants'},
	fish       = {'a fish', 'fish'},
	fishe      = {'a fish', 'fish'},
	fungu      = {'a fungus', 'fungi'},
	fungi      = {'a fungus', 'fungi'},
	spider     = {'a spider', 'spiders'},
	crustacean = {'a crustacean', 'crustaceans'},
	reptile    = {'a reptile', 'reptiles'},
	insect     = {'an insect', 'insects'},
	none       = {'an organism'},
}

local function getCat(s)
	s = mw.ustring.match(s, '^an? (.*)$') or s
	s = mw.ustring.match(s, '^the (.*)$') or s
	return s..'s'
end


function p._main(frame, toFrom, name, redirect, target)
	local args = frame:getParent().args
	local singleNoun, pluralNoun = '', ''
	local cat = mw.ustring.match(mw.ustring.lower(args[1] or 'none'), '^(.-)s?$')
	local catText = nul
	local unknown = false;
	
	if conf[cat] then singleNoun, pluralNoun = conf[cat][1], conf[cat][2] else
		singleNoun, pluralNoun = 'an organism'
		unknown = true
	end
	
	--support alternative indications for printworthy
	if args[2] == 'unprintworthy' or args['unprintworthy'] == 'true' then args['printworthy'] = 'no' end
	
	--build output string
	local outStr = '{{Redirect template|name='..toFrom:sub(1,1):upper()..toFrom:sub(2)..' '..name..' of '..singleNoun
	outStr = outStr..'|'..toFrom..'='..redirect..' of '..singleNoun..' (or group of '..(pluralNoun or 'organisms')..')'
	if toFrom == 'to' then outStr = outStr..'|from='..target else outStr = outStr..'|to='..target end
	outStr = outStr..'|main category=Redirects '..toFrom..getCat(redirect)
	if pluralNoun then outStr = outStr..' of '..pluralNoun end
	outStr = outStr..'|printworthy'..(args['printworthy'] or 'yes')..'}}'
	if unknown == true then outStr = outStr..'[[Category:Redirects '..toFrom..' '..getCat(redirect)..' using unknown values for parameter 1]]' end
	return outStr
end

return p