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 21:08, 31 May 2017 (rework). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local conf = {
	from_alternative_scientific_name = {
		toFrom = 'from',
		name = 'alternative scientific name',
		redirect = 'an alternative scientific name',
		target = 'the accepted scientific name',
	}, to_scientific_name = {
		toFrom = 'to',
		name = 'the scientific name',
		redirect = 'the [[biological nomenclature|scientific name]]',
		target = 'a [[Common name|vernacular ("common") name]]',
	}, from_scientific_name = {
		toFrom = 'from',
		name = 'a scientific name',
		target = 'the [[biological nomenclature|scientific name]]',
		redirect = 'a [[Common name|vernacular ("common") name]]',
	},
}

function p.main(frame)
	local template = frame.args[1] or frame:getParent().args['template']
	if conf[template] then
		return p._main(frame, conf[template].toFrom, conf[template].name, conf[template].redirect, conf[template].target)
	elseif template then
		return '<span class="error">The template '..template..'is not valid.</span>\n'
	else
		return '<span class="error">No template specified</span>\n'
	end
end

local cats = { -- 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 cats[cat] then singleNoun, pluralNoun = cats[cat][1], cats[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