Jump to content

Module:Cite taxon/utilities

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jts1882 (talk | contribs) at 16:14, 20 September 2020 (skip lines if compare text missing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

p.format_species_list = function(frame)

  local list = frame.args[1] or frame:getParent().args[1] 
  local expand = frame.args['expand'] or frame:getParent().args['expand'] -- want to subst template code
  local compare = frame.args['compare'] or frame:getParent().args['compare'] 
  
  list = mw.text.trim( list )
  
	local names = mw.text.split( list, "\n", plain )
	--local genus, species, authority
	local output = { }            -- table of species names for output

	local i = 1
	while names[i] do
		
		local name, match, skip
		local sep = " "                                               -- space between genus and species
		local genus, species, authority
        
        if compare and not names[i]:find("^" .. compare) then skip = true end -- only consider lines beginning with compare text (=genus)                                                          
                                                                              -- TODO try "(%S+)([%s×]+)(%S+) (.*)" 
        
        if not match then 
			for g, s, a in string.gmatch(names[i], "(%S+) × (%S+) (.*)" ) do -- match: genus × species authority
				genus = g
				species = s 
				sep = " × "
				if a ~= "" then authority = a end                          -- no authority after space
				match = true                                               -- we have a match 
			end
		end

		if not match then 
			for g, s in string.gmatch(names[i], "(%S+) × (%S+).*" ) do   --  match:  genus × species
				genus = g
				species = s
				sep = " × "
				if a ~= "" then authority = a end
				match = true
			end
		end
        if not match then 
			for g, s, a in string.gmatch(names[i], "(%S+) (%S+) (.*)" ) do -- match: genus species authority
				genus = g
				species = s 
				if a ~= "" then authority = a end                          -- no authority after space
				match = true                                               -- we have a match for genus, species, authority
			end
		end
		if not match then 
			for g, s in string.gmatch(names[i], "(%S+) (%S+).*" ) do       -- match: genus species
				genus = g
				species = s 
				match = true
			end
		end


		if match and not skip then
			name = "''[[" .. genus .. sep .. species .. "]]'' " 
			if authority  and expand then
			   name = name .. frame:expandTemplate{ title = 'small', args = { authority } }  --expand template
			elseif authority then
			   name = name .. "{{small|" .. authority .."}}"                                 -- don't expand template
			end

		end
	   	if name  then 
	  		table.insert ( output , name )
	  	elseif not skip then 
	  		table.insert ( output ,'<span class="error">unsupported format: expects "genus species authority"</span>' )
	  	end
	   
  	 
		i=i+1
	end
  
	return "*" .. table.concat(output, "\n*")
	
end

p.format_taxon_list = function(frame)

  local list = frame.args[1] or frame:getParent().args[1] 
  local expand = frame.args['expand'] or frame:getParent().args['expand'] -- want to subst template code
  
  list = mw.text.trim( list )
  
	local names = mw.text.split( list, "\n", plain )
	--local genus, species, authority
	local output = { }            -- table of species names for output
    local patterns = { "(%S+)( × )(%S+) (.*)",           --     genus × species authority
    	              "(%S+)( × )(%S+).*",              --     genus × species
    	               "(%S+)( )(%S+) (.*)",            --     genus species authority
    	               "(%S+)( )(%S+).*",               --     genus species 
    }
	local i = 1
	while names[i] do
		local name, match
		--local sep = " "                                               -- space between genus and species
		local genus, separator, species, authority
                                                                     -- TODO try "(%S+)([%s×]+)(%S+) (.*)" 
       for k, v in pairs (patterns) do 
			for g, sep, s, a in string.gmatch(names[i], v ) do -- match: genus × species authority
				genus = g
				species = s 
				separator = sep
				if a and a ~= "" then authority = a end                          -- no authority after space
				match = true                                               -- we have a match 
			end
			if match then break end
		end

		if match then
			name = "''[[" .. genus .. separator .. species .. "]]'' " 
			if authority  and expand then
			   name = name .. frame:expandTemplate{ title = 'small', args = { authority } }  --expand template
			elseif authority then
			   name = name .. "{{small|" .. authority .."}}"                                 -- don't expand template
			end

		end
	   	if name  then 
	  		table.insert ( output , name )
	  	else
	  		table.insert ( output ,'<span class="error">unsupported format: expects "genus species authority"</span>' )
	  	end
	   
  	 
		i=i+1
	end
  
	return "*" .. table.concat(output, "\n*")
	
end
return p