Module:Cite taxon/utilities
Appearance
From Wikipedia, the free encyclopedia
(Redirected from Module:FishRef/utilities)
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
localp={} p.format_species_list=function(frame,taxa) locallist=frame.args[1]orframe:getParent().args[1]ortaxa localexpand=frame.args['expand']orframe:getParent().args['expand']-- want to subst template code localcompare=frame.args['compare']orframe:getParent().args['compare'] localoption=frame.args['option']orframe:getParent().args['option'] localmode=frame.args['mode']orframe:getParent().args['mode'] localnolink=frame.args['nolink']orframe:getParent().args['nolink'] list=mw.text.trim(list) localnames=mw.text.split(list,"\n")-- could use plain option --local genus, species, authority localoutput={}-- table of species names for output locali=1 whilenames[i]do localname,match,skip localsep=" "-- space between genus and species localinfrasep="" localitalics="''" localgenus,species,subspecies,authority --local subspecies = "" names[i]=mw.text.trim(names[i]) -- custom preprocess options ifnames[i]==""thenskip=trueend-- ignore blank lines ifcompareandnotnames[i]:find("^"..compare)then-- only consider lines beginning with compare text (e.g. =genus) skip=true end ifoptionandstring.lower(option)=="worms"then-- process WoRMS list ifmode=="genus"then names[i]=names[i]:gsub("^Genus ","")-- delete leading "Genus" else names[i]=names[i]:gsub("^Species ","")-- delete leading "Species" end ifnames[i]:find("accepted as")then-- ignore invalid species, i.e. those "accepted as" something else skip=true end end ifoptionandstring.lower(option)=="gbif"then-- process GBIF list (copies with Species on alternate lines) ifnames[i]=="Species"ornames[i]=="Unranked"then skip=true end end ifoptionandstring.lower(option)=="tpl"then-- process tpl list ifnames[i]:find("Accepted")then names[i]=names[i]:gsub("^(.+) Accepted.+","%1")-- only include up to Accepted names[i]=mw.text.trim(names[i])-- needed as separate line as gsub returns str, i else skip=true-- skip if not accepted end end ifoptionandstring.lower(option)=="algaebase"then-- process algaebase ifnames[i]:find("C$")then names[i]=names[i]:gsub("C$","")-- only include C names[i]=mw.text.trim(names[i])-- needed as separate line as gsub returns str, i else skip=true-- skip if not accepted end end -- make list -- TODO try "(%S+)([%s×ばつ]+)(%S+) (.*)" ifmode=="genus"ormode=="taxon"then-- assume form taxon authority forg,ainstring.gmatch(names[i],"(%S+) (.*)")do-- match: genus ×ばつ species authority genus=g species="" sep="" ifa~=""thenauthority=aend-- no authority after space match=true-- we have a match ifmode=="taxon"thenitalics=""end end else-- match species list (various forms w/wo authority, hybrid) ifnotmatchthen --for g, s, f, ss, a in string.gmatch(names[i], "(%S+) (%S+)( subsp%. )(%S+) (.*)" ) do -- match: genus species subsp. subspecies authority forg,s,f,ss,ainstring.gmatch(names[i],"(%S+) (%S+)( [svfb][ubsparomiv]*%. )(%S+) (.*)")do-- match: genus species subsp. subspecies authority genus=g species=s subspecies=ss infrasep=f--" subsp. " sep=" " ifa~=""thenauthority=aend-- no authority after space match=true-- we have a match end end ifnotmatchthen --for g, s, f, ss in string.gmatch(names[i], "(%S+) (%S+)( subsp%. )(%S+)" ) do -- match: genus species subsp. subspecies forg,s,f,ssinstring.gmatch(names[i],"(%S+) (%S+)( [svfb][ubsparomiv]*%. )(%S+)")do-- match: genus species subsp. subspecies genus=g species=s subspecies=ss infrasep=f-- " subsp. " sep=" " --if a ~= "" then authority = a end -- no authority after space match=true-- we have a match end end ifnotmatchthen forg,s,ainstring.gmatch(names[i],"(%S+) ×ばつ (%S+) (.*)")do-- match: genus ×ばつ species authority genus=g species=s sep=" ×ばつ " ifa~=""thenauthority=aend-- no authority after space match=true-- we have a match end end ifnotmatchthen forg,sinstring.gmatch(names[i],"(%S+) ×ばつ (%S+).*")do-- match: genus ×ばつ species genus=g species=s sep=" ×ばつ " ifa~=""thenauthority=aend match=true end end ifnotmatchthen forg,s,ainstring.gmatch(names[i],"(%S+) (%S+) (.*)")do-- match: genus species authority genus=g species=s ifa~=""thenauthority=aend-- no authority after space match=true-- we have a match for genus, species, authority end end ifnotmatchthen forg,sinstring.gmatch(names[i],"(%S+) (%S+).*")do-- match: genus species genus=g species=s match=true end end end ifmatchandnotskipthen localspecies_name=genus..sep..species ifsubspeciesthen name="''"..species_name.."''"..infrasep.."''"..subspecies.."''" ifnotnolinkthen name="[["..species_name..infrasep..subspecies.."|"..name.."]]"-- wikilinked name with redirect end else name=species_name ifnotnolinkthenname="[["..name.."]]"end-- add wikilink name=italics..name..italics-- add italics end ifauthorityandexpandthen name=name.." "..frame:expandTemplate{title='small',args={authority}}--expand template elseifauthoritythen name=name.." {{small|"..authority.."}}"-- don't expand template end end ifnamethen table.insert(output,name) elseifnotskipthen 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) locallist=frame.args[1]orframe:getParent().args[1] localexpand=frame.args['expand']orframe:getParent().args['expand']-- want to subst template code list=mw.text.trim(list) localnames=mw.text.split(list,"\n",plain) --local genus, species, authority localoutput={}-- table of species names for output localpatterns={"(%S+)( ×ばつ )(%S+) (.*)",-- genus ×ばつ species authority "(%S+)( ×ばつ )(%S+).*",-- genus ×ばつ species "(%S+)( )(%S+) (.*)",-- genus species authority "(%S+)( )(%S+).*",-- genus species -- taxon or genus authority } locali=1 whilenames[i]do localname,match --local sep = " " -- space between genus and species localgenus,separator,species,authority -- TODO try "(%S+)([%s×ばつ]+)(%S+) (.*)" fork,vinpairs(patterns)do forg,sep,s,ainstring.gmatch(names[i],v)do-- match: genus ×ばつ species authority genus=g species=s separator=sep ifaanda~=""thenauthority=aend-- no authority after space match=true-- we have a match end ifmatchthenbreakend end ifmatchthen name="''[["..genus..separator..species.."]]''" ifauthorityandexpandthen name=name..frame:expandTemplate{title='small',args={authority}}--expand template elseifauthoritythen name=name.."{{small|"..authority.."}}"-- don't expand template end end ifnamethen 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 returnp