Module:Sandbox/trappist the monk/make cite iucn

From blackwiki
< Module:Sandbox
Revision as of 16:19, 27 December 2019 by blackwiki>Trappist the monk
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/trappist the monk/make cite iucn/doc

require ('Module:No globals');

--[[--------------------------< A U T H O R _ L I S T _ M A K E >----------------------------------------------

creates a list of individual |authorn= parameters from the list of names provided in the raw iucn citation.  names
must have the form: Surname, I. (more than one 'I.' pair allowed)

]]

local function author_names_get (raw_iucn_cite)
	local list = {};															-- table that holds name list parts
	local author_names = raw_iucn_cite:match ('^([^%d]-)%s+%d%d%d%d');			-- extract author name-list from raw iucn citation

	local names = author_names:gsub ('%.,%s+', '.|'):gsub ('%.%s+&%s+', '.|');	-- replace 'separators' (<dot><comma><space> and <space><ampersand><space>) with a pipe
	list = mw.text.split (names, '|');											-- split the string on the pipes into entries in list{}
	
	if 0 == #list then
		return table.concat ({'|author=', author_names})						-- no 'names' of the proper form; return the original as a single |author= parameter
	else
		for i, name in ipairs (list) do											-- spin through the list and 
			list[i] = table.concat ({'|author', i, '=', name});					-- add |authorn= parameter names
		end
		return table.concat (list, ' ');										-- make a big string and return that
	end
end


--[[--------------------------< T I T L E _ G E T >------------------------------------------------------------

extract and format citation title

''species name'' (amended or errate title)
''species name''
''species name'' ssp. ''subspecies''
Cephalophus ogilbyi ssp. crusalbum
TODO: add support for variants of simple species names

]]

local function title_get (raw_iucn_cite)
	local title = raw_iucn_cite:match ('%d%d%d%d%.%s+(.-)%. The IUCN Red List of Threatened Species');

	local patterns = {															-- tables of string.match patterns [1] and string.gsub patterns [2]
		{'(.-)%sssp%.(.-)%s(%b())$', "''%1'' ssp. ''%2'' %3"},					-- binomen ssp. subspecies (zoology) with errata or amended text
		{'(.-)%sssp%.(.+)', "''%1'' ssp. ''%2''"},								-- binomen ssp. subspecies (zoology)
		{'(.-)%ssubsp%.(.-)%s(%b())$', "''%1'' subsp. ''%2'' %3"},				-- binomen subsp. subspecies (botany) with errata or amended text
		{'(.-)%ssubsp%.(.+)', "''%1'' subsp. ''%2''"},							-- binomen subsp. subspecies (botany)
		{'(.-)%svar%.(.-)%s(%b())$', "''%1'' var. ''%2'' %3"},					-- binomen var. variety (botany) with errata or amended text
		{'(.-)%svar%.(.+)', "''%1'' var. ''%2''"},								-- binomen var. variety (botany)
		{'(.-)%ssubvar%.(.-)%s(%b())$', "''%1'' subvar. ''%2'' %3"},			-- binomen subvar. subvariety (botany) with errata or amended text
		{'(.-)%ssubvar%.(.+)', "''%1'' subvar. ''%2''"},						-- binomen subvar. subvariety (botany)
		{'(.-)%s(%b())$', "''%1'' %2"},											-- binomen with errata or amended text
		{'(.+)', "''%1''"},														-- binomen
		}
	
	for i, v in ipairs (patterns) do
		if title:match (v[1]) then
			title = title:gsub (v[1], v[2]);
			break;
		end
	end

	return table.concat ({' |title=', title});
end


--[[--------------------------< _ M A K E _ C I T E _ I U C N >------------------------------------------------

parses apart an iucn-format citation copied from their webpage and reformats that into a {{cite iucn}} template for substing

]]

local function make_cite_iucn (frame)
	local raw_iucn_cite = frame.args[1];
	
	local template = {'{{cite iucn '};											-- table that holds the {{cite iucn}} template as it is being assembled
	local year, volume, page, doi, accessdate;

	year = raw_iucn_cite:match ('^%D+(%d%d%d%d)');
	volume, page = raw_iucn_cite:match ('(%d%d%d%d):%s+(e%.T%d+A+%d+)%.%s');
	doi = raw_iucn_cite:match ('10%.2305/IUCN%.UK%.[%d%-]+%.RLTS%.T%d+A%d+%.en');
	accessdate = raw_iucn_cite:match ('Downloaded on ([^%.]+)%.');
	
	table.insert (template, author_names_get (raw_iucn_cite));					-- add string of author name parameters
	table.insert (template, table.concat ({' |year=', year}));					-- add formatted year
	table.insert (template, title_get (raw_iucn_cite));							-- add formatted title
	table.insert (template, table.concat ({' |volume=', volume}));				-- add formatted volume
	table.insert (template, table.concat ({' |page=', page}));					-- add formatted page
	table.insert (template, table.concat ({' |doi=', doi}));					-- add formatted doi
	table.insert (template, table.concat ({' |access-date=', accessdate}));		-- add formatted access-date
	table.insert (template, '}}');												-- close the template

	return table.concat (template);
end


--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------
]]

return {
	make_cite_iucn = make_cite_iucn,
	}