if newickString ~= "" or nodeLeaf ~= "" or listString ~= "" then
--if nodeLeaf ~= "" then
childCount = childCount + 1 -- this counts child elements in this clade
--[[]
for i in string.gmatch(nodeLeaf, "||rowspan") do -- count number of rows started (transclusion)
nodeCount = nodeCount + 1
end
for i in string.gmatch(nodeLeaf, '{|') do -- count number of tables started (transclusion)
cladeCount = cladeCount + 1
end
-- count occurences of clade structure using number of classes used and add to counters
local _, nClades = string.gsub(nodeLeaf, '', "")
local _, nNodes = string.gsub(nodeLeaf, '', "")
cladeCount = cladeCount + nClades
nodeCount = nodeCount + nNodes  
lastNode = childNumber -- this gets the last node with a valid entry, even when missing numbers
end
end
--]]
-- nodes can be either terminal leaves or a clade structure (table)
-- note: should change class clade-leaf to clade-node to reflect this
nodeCount = nodeCount -- number of nodes (class clade-leaf) passed down by transduction
+ childCount + 1 -- plus one for current clade and one for each of its child element
cladeCount = cladeCount + 1 -- number of clade structure tables passed down by transduction (plus one for current clade)
leafCount = nodeCount-cladeCount -- number of terminal leaves (equals height of cladogram)  
-- output for testing: number of clades / total nodes / terminal nodes (=leaves)
-- (internal nodes) (cladogram height)
infoOutput = '<small>[' .. cladeCount .. '/' .. nodeCount .. '/' .. leafCount .. ']</small>'  
return infoOutput  
end  
function p.showClade
--local code = .args.code or ""
local code = :getParent.args['code2'] or ""  
--return code
--return mw.text.unstrip(code)  
--local test = "<pre>Hello</pre>"
--return string.sub(test,6,-7)  
local o1 =:getParent:getArgument('code2')
return o1:expand  
--return string.sub(code,2,-1) -- strip marker \127'"`UNIQ--tagname-8 hex digits-QINU`"'\127
--return :preprocess(string.sub(code,3))
end  
function p.firstToUpper(str)
return (str:gsub("^%l", string.upper))
end
-- function to generate cladogram from a wikitext-like list
- uses @ instead of * because we don't want wikitext processed and nowiki elements are passed as stripmarkers (?)  
function p.list(count,listString)  
local cladeString = ""
--count = count+1
local i=1
local child=1
local lastNode=0--table.getn(list) -- number of child branches (potential)
local listChar = "@"  
if listString:find("UNIQ.-QINU") then -- if wrapped in nowiki
mw.addWarning("Stripping content in nowiki tags")
listString = mw.text.trim( mw.text.unstripNoWiki( listString ) )
end
if string.match( listString, "^*", 1 ) then
listChar = "*"
end  
local list = mw.text.split(listString, "\n")  
cladeString = cladeString .. '{| '  
while list[i] do
list[i]=list[i]:gsub("^"..listChar, "") -- strip the first @ or *
list[i]=mw.text.trim(list[i]) -- trim  
if not string.match( list[i], "^"..listChar, 1 ) then -- count children at this level (not beginning wiht @/*)
lastNode = lastNode+1
end
i=i+1
end  
i=1
while list[i] do  
-- pseudocode:
if next value begins with @ we have a subtree,
which must be recombined and past iteratively
else we have a simple leaf  
-- if the next value begins with @, we have a subtree which should be recombined
if list[i+1] and string.match( list[i+1], "^"..listChar, 1 ) then  
local label=list[i]
i=i+1
local recombined = list[i]
while list[i+1] and string.match( list[i+1], "^"..listChar, 1 ) do
recombined = recombined .. "\n" .. list[i+1]
i=i+1
end
--cladeString = cladeString .. '\n' .. p.addTaxon(child, recombined, label, lastNode)
cladeString = cladeString .. '\n' .. p.addTaxon(child, p.list(count,recombined), label, lastNode)
else
cladeString = cladeString .. '\n' .. p.addTaxon(child, list[i], "", lastNode)
end
i=i+1
child=child+1
end  
cladeString = cladeString .. '\n|}'  
mw.addWarning("WARNING. This is a test feature only.")
return cladeString
end
-- =================== experimental wikitext list to clade parser function =============================  
--Function of convert wikitext lists to clade format  
Usage:  
function p.cladeConverter
pargs = :getParent.args  
if .args['list'] or pargs['list'] then
return p.listConverter
elseif .args['newickstring'] or .args['newick'] or pargs['newickstring'] or pargs['newick'] then
return p.newickConverter
else
local message = "Conversion needs wikitext list or newick string in parameters *list* or *newick* respectively"
mw.addWarning(message)
return message
end
end
function p.listConverter  
local listString = .args['list'] or pargs['list']  
-- show the list string
local cladeString = ''
local levelNumber = 1 -- for depth of iteration
local childNumber = 1 -- number of sister elements on node (always one for root)
local indent = p.getIndent(levelNumber)
-- converted the newick string to the clade structure
cladeString = cladeString .. indent .. ''  
local resultString = ''
local option = pargs['option'] or ''
if option == 'tree' then
--show the transcluded clade diagram
resultString = cladeString
else
-- show the wikitext list string
resultString = '<pre>'..listString..'</pre>'
-- show the converted clade structure
resultString = resultString .. '<pre>'.. cladeString ..'</pre>'
end
--resultString = :expandTemplate{ title = 'clade', :preprocess(cladeString) }  
return resultString
end  
function p.listParseLevel(listString,levelNumber,childNumber)  
local cladeString = ""
local indent = p.getIndent(levelNumber)
levelNumber=levelNumber+1  
local nowiki = false
if listString:find("UNIQ.-QINU") then -- if wrapped in nowiki
mw.addWarning("Stripping content in nowiki tags")
listString = mw.text.trim( mw.text.unstripNoWiki( listString ) )
nowiki = true
end
local listChar = "@"
if string.match( listString, "^*", 1 ) then
listChar = "*"
end  
local list = mw.text.split(listString, "\n")
local i=1
local child=1
local lastNode=0  
while list[i] do
list[i]=list[i]:gsub("^"..listChar, "") -- strip the first @  
if not string.match( list[i], "^"..listChar, 1 ) then -- count children at this level (not beginning wiht @)
lastNode = lastNode+1
end
i=i+1
end
i=1  
while list[i] do  
-- pseudocode:
if next value begins with @ we have a subtree,
which must be recombined and past iteratively
else we have a simple leaf  
-- if the next value begins with @, we have a subtree which should be recombined
if list[i+1] and string.match( list[i+1], "^"..listChar, 1 ) then  
local label=list[i]
i=i+1
local recombined = list[i]
while list[i+1] and string.match( list[i+1], "^"..listChar, 1 ) do
recombined = recombined .. "\n" .. list[i+1]
i=i+1
end
cladeString = cladeString .. indent .. '|label' .. child ..'=' .. label
cladeString = cladeString .. indent .. '|' .. child ..'=' .. ''  
if nowiki then
return mw.getCurrentFrame:preprocess( '') --return wrapped in nowiki
else
return cladeString
end
end  
-- this must be at end
return p