Module:Sandbox/Peter coxhead/Refgen

-- The module has functions which generate references using various "cite taxonomic-database" templates, such as, using standardized parameters: The invocations of the functions should be substituted. In a cs1 citation, if the authority ends with a . then a second is added, because otherwise the citation generating code separates the . from the authority.
 * taxon – required
 * auth[ority] – optional
 * id – required
 * cs1 or cs2 – if omitted, cs1 is assumed

require('strict') local p = {} local l = {} -- separate out local functions

-- Trim leading and trailing spaces, always returning a string. function l.trim(str) str = mw.text.trim(str) if str == nil then str = "" end return str end

-- Ensure double .. is present if the mode is cs1 and the auth(ority) ends with .     Deals with "bug" in citation templates. function p.authchk(frame) local auth = frame.args[1] or "" local mode = frame.args[2] or "cs1" return l.authchk(auth, mode) end

function l.authchk(auth, mode) if mode == "cs1" then if mw.ustring.sub(auth, -2) == ".." then -- no action elseif mw.ustring.sub(auth, -1) == "." then auth = auth .. "."		end elseif mode == "cs2" and mw.ustring.sub(auth, -2) == ".." then auth = mw.ustring.sub(auth, 1, mw.ustring.len(auth)-1) end return auth end

-- Check arguments and generate a reference to the appropriate database given by "db". function l.refgen(db, frame) local taxon = frame.args[1] or "" if taxon == "" then return " ***ERROR: TAXON not supplied! " end taxon = mw.text.trim(taxon) local auth = frame.args[2] or "" auth = mw.text.trim(auth) local id = frame.args[3] or "" if id == "" then return " ***ERROR: ID not supplied! " end id = mw.text.trim(id) local mode = frame.args[4] or "cs1" mode = mw.text.trim(mode) auth = l.authchk(auth, mode) if db == "GCDsp" then return l.GCDsp(taxon, auth, id, mode) elseif db == "IPNI" then return l.IPNI(taxon, auth, id, mode) elseif db == "POWO" then return l.POWO(taxon, auth, id, mode) else return "***ERROR in l.refgen" end end

-- Generate reference to the Global Compositae Database for a species. function p.GCDsp(frame) return l.refgen("GCDsp", frame) end

function l.GCDsp(taxon, auth, id, mode) --Example output: local res = " " return res end

-- Generate reference to IPNI. function p.IPNI(frame) return l.refgen("IPNI", frame) end

function l.IPNI(taxon, auth, id, mode) --Example output: local res = " " return res end

-- Generate reference to PoWO. function p.POWO(frame) return l.refgen("POWO", frame) end

function l.POWO(taxon, auth, id, mode) --Example output: local res = " " return res end

return p