require('strict')local TaxonItalics = require('Module:TaxonItalics')local ItalicTitle = require('Module:Italic title')local p = local l = -- separate out local functions
function l.show(items) local result = "" for i = 1,#items,1 do result = result .. "|" .. items[i] .. "" .. mw.ustring.len(items[i]) .. "" end return result .. "|"end
-- *****************************************************************************--
-- Split a string representing a taxon name into 'units'.function l.doSplitTaxonName(str) -- connecting terms that are not italicized local cTerms = local specialChrs = '[%(%)×%+]' -- pattern listing all specially treated characters local units = local states = local state = startState local j = 0 local currUnit = local chr -- startUnit starts a new unit to be taken from the string str local startUnit = function (nextState) currUnit = chr state = nextState end -- saveUnit saves the current unit taken from the string str and starts a -- new one local saveUnit = function (nextState) -- check for words that don't get italicized (?at this position) if state
startState then if chr
'(' or chr
'×' or chr
'&' then startUnit(entityState) else -- other kind of character startUnit(italState) end elseif state
' ' then -- ignore elseif mw.ustring.match(chr, specialChrs) then -- chr
')' or chr
'+' saveUnit(nonItalState) elseif chr
nonItalState then if chr
'(' or chr
'×' or chr
'&' then saveUnit(entityState) else -- other kind of character saveUnit(italState) end elseif state
';' then currUnit = currUnit .. chr chr = -- nonbreaking spaces are treated a whitespace if currUnit
' ' or currUnit
' ' then state = whiteSpState end saveUnit(startState) else currUnit = currUnit .. chr end else -- state
' ' then saveUnit(whiteSpState) elseif mw.ustring.match(chr, specialChrs) then -- chr
')' or chr
'+' saveUnit(nonItalState) elseif chr
function p.italicizeTaxonName(frame) local str = frame.args[1] or "" -- first clean up the name str = mw.ustring.gsub(str, "%s+", " ") str = mw.ustring.gsub(str, "", "") str = mw.ustring.gsub(str, "", "") str = mw.ustring.gsub(str, "?span.->", "") -- remove the last 'word' if it appears to be a disambiguating term local last = l.lastWord(str) local ln = mw.ustring.len(last) if mw.ustring.match(last, "%([a-z].*%)") then str = mw.ustring.sub(str, 1, -ln-1) else last = end -- now split up the name into 'units' units, states = l.doSplitTaxonName(str) -- finally, put everything back together, correctly italicized return l.joinUnits(units, states) .. lastend
function l.joinUnits(units, states) local res = '\n|' local j = 1 local n = #units while j <= n do res = res .. units[j] .. '' .. tostring(states[j]) .. '|' j = j + 1 end res = '\n' states[n+1] = endState states[n+2] = endState local openItalics = true j = 1 while j <= n do -- res = res .. '' .. tostring(states[j]) .. tostring(italState) .. '' if states[j]
whiteSpState and states[j+2]
function p.test(frame) local taxon = frame.args[1] or "" local item = frame.args[2] or "" local ok, info = pcall(frame.expandTemplate, frame,) if ok then return 'ok=true; >' .. info .. '<' else return 'ok=false; >' .. info .. '<' endend
function p.test1(frame) local str = frame.args[1] or local pattern = frame.args[2] or "%([A-Z].*%)" if mw.ustring.match(str, pattern) then return 'matched' else return 'not matched' endend
function p.test2(frame) local a1 = frame.args[1] or local t = t[1] = t[1].a = a1 t[1].e = 'Argument 1 = ' return t[1].e .. t[1].aend
function p.test3(frame) local a1 = frame.args[1] or local a2 = frame.args[2] or local t = mw.text.split(a1, a2, true) res = for i = 1, #t do if t[i]
then t[i] = 'EMPTY' end res = res .. t[i] .. '
' end local tbl = tbl[1] = tbl[1][9] = 'TEST' return res .. '
/' .. tbl[1][9].. '
/' .. '
/' .. table.concat(tbl[1],',',9,9)end
function p.test4(frame) local a1 = frame.args[1] or local firstCh = mw.ustring.sub(a1,1,1) if firstCh
function p.existsTest1(frame) local taxon = frame.args[1] or 'Life' local res if mw.title.new('Taxonomy/'..taxon, 'Template').exists then res = 'Taxonomy/'..taxon..'exists' else res = 'Taxonomy/'..taxon..'does not exist' end return resend
function p.existsTest2(frame) local taxon = frame.args[1] or 'Life' local template = 'Template:Taxonomy/' .. taxon local item = frame.args[2] or 'all' local ok, dummy = pcall(frame.expandTemplate, frame,) local res = template if ok then return res .. ' exists' else return res .. ' does not exist' endend
function p.getGenus(frame) local str = frame.args[1] or return l.genus(str, 1)end
function l.genus(str, init) local res = mw.ustring.match(str, '^[^%s]*', init) if res
function p.getLastWord(frame) local str = frame.args[1] or return l.lastWord(str)end
function l.lastWord(str) local res, n = mw.ustring.gsub(str, '.*%s', , 1) if n
function p.abbreviate(frame) local str = frame.args[1] or local res, n = mw.ustring.gsub(str, '([A-Z]).- (.*)', '%1. %2') return res .. ' (' .. tostring(n) .. ' matches)'end
function p.italicTaxonTitle(frame) local pageName = frame.args[1] or pageName = frame:expandTemplate return frame:callParserFunctionend
function p.linkCheck(frame) local linkTarget = frame.args[1] or local linkText = frame.args[2] or local res = true if linkTarget ~= and linkText ~= and linkTarget ~= linkText then local linkTargetTitle = mw.title.new(linkTarget) local linkTextTitle = mw.title.new(linkText) res = linkTextTitle.redirectTarget
function p.parseSpeciesName(frame) local speciesName = frame.args[1] or local genus, disambig, species = l.doParseSpeciesName(speciesName) return 'genus ='..genus..', disambig='..disambig..', species='..speciesend
function l.doParseSpeciesName(speciesName) local genus = local disambig = local species = local words = mw.text.split(speciesName, " ", true) local nWords = #words local currWord = 1 if currWord > nWords then return genus, disambig, species end genus = words[currWord] if genus
mw.ustring.char(40) then -- '(' disambig = species currWord = currWord + 1 if currWord > nWords then return genus, disambig, end species = words[currWord] end if species
then taxon = taxon .. ' ' .. infraspecies else taxon = taxon .. ' ' .. ct .. ' ' .. infraspecies end local italicizeP = italicTitle ~= 'no' and (basePageTitle
then name = basePageTitle if italicizeP then name = TaxonItalics.italicizeTaxonName(name, false, false) end end -- deal with page title if italicizeP then local pageTitle = mw.title.getCurrentTitle.text -- formatting the page title with DISPLAYTITLE needs the full page title pageTitle = TaxonItalics.italicizeTaxonName(pageTitle, false, false, true) -- format pageTitle, not italicizing any parenthesized term if italicTitle ~= 'test' then mw.getCurrentFrame:callParserFunction('DISPLAYTITLE', pageTitle) else name = name .. ' \\Italic title\\ ' .. pageTitle -- for testing and debugging end end return nameend--
function p.boldList(frame) local items = for i, v in ipairs(frame:getParent.args) do table.insert(items, v) end local conj = "" .. (#items > 2 and ", " or " ") .. (frame.args.conj or "or") .. " " return ""..mw.text.listToText(items, ", ", conj)..""end
return p