Next round of programming yields:
def panflute_to_bike_etree_nr(e, level=0) -> ET.Element:
# questions about how header levels are handled as we put them into the etree
etree = None
stack = [(e, level)]
# current ul_elem
ul_elem = None
heading_level = 0
while stack:
(e, level) = stack.pop()
print(" " * level, e.tag)
if is_inline_or_contentless(e):
li_elem = ET.Element("li")
# TO DO: handle rich text
p_elem = ET.SubElement(li_elem, "p")
p_elem.text = pf.stringify(e).strip()
# 2 things to figure out: parent_ul (where to attach li_elem) and what the current ul_elem is
if e.tag == "Header":
print ("header", e.level, e.identifier, e.classes, e.attributes, pf.stringify(e))
li_elem.attrib["data-type"] = "heading"
li_elem.attrib["data-level"] = str(e.level)
if e.level > heading_level:
# child
parent_ul = ul_elem
print ("e.level > heading_level", ul_elem)
elif e.level == heading_level:
# sibling
# parent_ul has to be ul parent of ul_elem
parent_ul = ul_elem.getparent().getparent()
else:
# e.level < heading_level
# uncle or higher
parent_ul = find_ul_ancestor(ul_elem, int(e.level)-1)
heading_level = e.level
ul_elem = ET.SubElement(li_elem, "ul")
parent_ul.append(li_elem)
else:
ul_elem.append(li_elem)
else:
if e.tag == "Doc":
etree = empty_bike_etree()
ul_elem = etree.xpath("//ul")[0]
else:
# BulletList
# OrderedList
# ListItem
print("block", e.tag)
try:
for c in reversed(e.content):
stack.append((c, level + 1))
except AttributeError:
pass
# clean up etree by adding ids
etree = add_ids_to_bike_etree(etree)
return etree