else
cladeString = cladeString .. '\n|-'
cladeString = cladeString .. labelCellString
cladeString = cladeString .. leafCellString
cladeString = cladeString .. barLabelCellString
cladeString = cladeString .. '\n|-' -- add second row (only one cell needed for sublabel because of rowspan=2);
cladeString = cladeString .. sublabelCellString
end  
return cladeString
end  
-- adds text for label or sublabel to a cell  
function p.addLabel(childNumber,nodeLabel)  
--local nodeLabel = mw.getCurrentFrame:getParent.args['label'..tostring(childNumber)] or ""  
--local firstChars = string.sub(nodeLabel, 1,2) -- get first two characters; will be
-- a better method for template expansion size is to replace spaces with nonbreaking spaces
-- however, there is a problem if labels have a styling element (e.g. <span style= ..., <span title= ...)
local stylingElementDetected = false
if string.find(nodeLabel, "span ") ~= nil then stylingElementDetected = true end
if string.find(nodeLabel, " style") ~= nil then stylingElementDetected = true end
--TODO test following alternative
--if nodeLabel:find( "%b<>") then stylingElementDetected = true end  
if stylingElementDetected == true then
return '<div style=display:inline class=nowrap>' .. nodeLabel .. '</div>'
else
local nowrapString = string.gsub(nodeLabel, " ", "&nbsp;") -- replace spaces with non-breaking space
if not nowrapString:find("UNIQ.-QINU") and not nowrapString:find("%[%[.-%]%]") then -- unless a strip marker
nowrapString = string.gsub(nowrapString, "-", "&#8209;") -- replace hyphen with non-breaking hyphen (&#8209;)
end
return nowrapString
end
end
end  
--=================== Newick string handling function =============================  
function p.getNewickOuterterm(newickString)
return string.gsub(newickString, "%b", "") -- delete parenthetic term
end  
function p.newick(count,newickString)
--start table
--'{|%b", "") -- delete parenthetic term
if outerTerm == 'panthera' then outerTerm = "x" end -- how is this set in local variable for inner nodes?  
outerTerm = tostring(count)  
-- need to remove commas in bracket terms before split, so temporarily replace commas between brackets
local innerTerm2 = string.gsub(innerTerm, "%b", function (n)
return string.gsub(n, ",%s*", "XXX") -- also strip spaces after commas here
end)
--cladeString = cladeString .. '\n' .. p.addTaxon(1, innerTerm2, "")  
-- this needs a lastNode variable
local s = p.strsplit(innerTerm2, ",")
--oldLastNode=lastNode
local lastNode=table.getn(s) -- number of child branches
local i=1
while s[i] do
local restoredString = string.gsub(s[i],"XXX", ",") -- convert back to commas
--restoredString = s[i]
local outerTerm = string.gsub(restoredString, "%b", "")
if string.find(restoredString, '%(.*%)') then
--cladeString = cladeString .. '\n' .. p.addTaxon(i, restoredString, "x")
cladeString = cladeString .. '\n' .. p.addTaxon(i, p.newick(count,restoredString), outerTerm, lastNode)
-- p.addTaxon(2, p.newick(count,newickString2), "root")
else
cladeString = cladeString .. '\n' .. p.addTaxon(i, restoredString, "", lastNode) --count)
end
i=i+1
end
-- lastNode=oldLastNode  
-- close table
--cladeString = cladeString .. '\n' .. '| |
\n|}'
--cladeString = cladeString .. '\n|
\n|}' -- is this legacy for extra sublabel?
cladeString = cladeString .. '\n|}'
return cladeString
end
-- emulate a standard split string function
-- why not use mw.text.split(s, sep)?
function p.strsplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
local i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end  
-- =================== experimental Newick to clade parser function =============================  
--Function of convert Newick strings to clade format  
Usage:  
function p.newickConverter  
local newickString = .args['newickstring'] or .args['newick'] or pargs['newickstring'] or pargs['newick']  
--if newickString == '}' then return newickString end  
newickString = p.processNewickString(newickString,"") -- "childNumber")  
-- show the Newick string
local cladeString = ''
local levelNumber = 1 -- for depth of iteration
local childNumber = 1 -- number of sister elements on node (always one for root)  
-- converted the newick string to the clade structure
cladeString = cladeString .. ''  
local resultString = ''
local option = pargs['option'] or ''
if option == 'tree' then
--show the transcluded clade diagram
resultString = cladeString
else
-- show the Newick string
resultString = '<pre>'..newickString..'</pre>'
-- show the converted clade structure
resultString = resultString .. '<pre>'.. cladeString ..'</pre>'
end
--resultString = :expandTemplate{ title = 'clade', :preprocess(cladeString) }  
return resultString
end  
--labelN=labelname
2. the hand term in parenthesis has common delimited child nodes, each of which can be
i. a taxon name which just needs: |N=leafname
ii. a Newick string which needs further processing through reiteration  
function p.newickParseLevel(newickString,levelNumber,childNumber)  
local cladeString = ""
local indent = p.getIndent(levelNumber)
--levelNumber=levelNumber+1  
local j=0
local k=0
j,k = string.find(newickString, '%(.*%)') -- find location of outer parenthesised term
local innerTerm = string.sub(newickString, j+1, k-1) -- select content in parenthesis
local outerTerm = string.gsub(newickString, "%b", "") -- delete parenthetic term  
cladeString = cladeString .. indent .. '|label'..childNumber..'=' .. outerTerm
cladeString = cladeString .. indent .. '|' .. childNumber..'=' .. ''
return cladeString
end  
function p.getIndent(levelNumber)
local indent = "\r"
local extraIndent = pargs['indent'] or mw.getCurrentFrame.args['indent'] or 0  
while tonumber(extraIndent) > 0 do
indent = indent .. " " -- an extra indent to make aligining compound trees easier
extraIndent = extraIndent - 1
end  
while levelNumber > 1 do
indent = indent .. " "
levelNumber = levelNumber-1
end
return indent
end  
function p.newickstuff(newickString)  
end
function p.processNewickString(newickString,childNumber)  
local maxPatterns = 5
local i = 0
local pargs = pargs
local pattern = pargs['newick'..tostring(childNumber)..'-pattern'] -- unnumbered option for i=1
local replace = pargs['newick'..tostring(childNumber)..'-replace']  
while i < maxPatterns do
i=i+1
pattern = pattern or pargs['newick'..tostring(childNumber)..'-pattern'..tostring(i)]
replace = replace or pargs['newick'..tostring(childNumber)..'-replace'..tostring(i)] or ""  
if pattern then
newickString = string.gsub (newickString, pattern, replace)
end
pattern = nil; replace = nil
end
newickString = string.gsub (newickString, "_", " ") -- replace underscore with space
return newickString
end
---  
function p.test2(target)
local target ="User:Jts1882/sandbox/templates/Template:Passeroidea"
local result = mw.getCurrentFrame:expandTemplate{ title = target, args = {['style'] = '' } }
return result
end
---  
---  
--function getCladeTreeInfo
this preprocessing loop gets information about the whole structure (number of nodes, leaves etc)
it makes a redundant calls to the templates through transclusion, but doen't affect the template depths;
it provides the global lastNode that is used to limit the main while loop
---
function p.getCladeTreeInfo  
-- enable proprocessing loop
local childNumber = 0
local childCount =0
local maxChildren =20  
--info veriables (these are global for now)
nodeCount=0
cladeCount=0
leafCount=0  
while childNumber < maxChildren do -- preprocessing loop
childNumber = childNumber + 1 -- so we start with 1
local nodeLeaf,data = pargs[tostring(childNumber)] or "" -- get data from |N=
local newickString = pargs['newick'..tostring(childNumber)] or "" -- get data from |labelN=
local listString = pargs['list'..tostring(childNumber)] or "" -- get data from |labelN=
if newickString ~= "" or nodeLeaf ~= "" or listString ~= "" then
--if nodeLeaf ~= "" then
childCount = childCount + 1 -- this counts child elements in this clade
--[[]