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:31, 31 May 2017 (debug). 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

local function fromTo(toFrom)
	if toFrom == 'to' then return 'from' else return 'to' end
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 template arguments
	local outArgs = {}
	outArgs['name'] = toFrom:sub(1,1):upper()..toFrom:sub(2)..' '..name..' of '..singleNoun
	outArgs[toFrom] = redirect..' of '..singleNoun..' (or group of '..(pluralNoun or 'organisms')..')'
	outArgs[fromTo(toFrom)] = target
	local mainCat = 'Redirects '..toFrom..getCat(redirect)
	if pluralNoun then mainCat = mainCat..' of '..pluralNoun end
	outArgs['main category'] = mainCat
	outArgs['printworthy'] = (args['printworthy'] or 'yes')
	
	--build output string
	local outStr = ''
	if frame.args['debug'] == 'true' then
		outStr = '{{Redirect template|'
		for k,v in pairs( outArgs ) do
			outStr = ' | '..k..' = '..v
		end
		outStr = '}}'
	else
		outStr = frame:expandTemplate{ title = 'Redirect template', args = outArgs }
	end
	
	if unknown == true then outStr = outStr..'[[Category:Redirects '..toFrom..' '..getCat(redirect)..' using unknown values for parameter 1]]' end
	
	return outStr
end

return p