	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=MIC+04-3+Non-recursive+walk+of+a+panflute+document&amp;rft.aulast=Yee&amp;rft.aufirst=Raymond&amp;rft.subject=Uncategorized&amp;rft.source=Hypotyposis+on+a+Good+Day&amp;rft.date=2023-10-11&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=https://hypotyposis.net/blog/2023/10/11/mic-04-3-non-recursive-walk-of-a-panflute-document/&amp;rft.language=English"></span>
	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=MIC+04-3+Non-recursive+walk+of+a+panflute+document&amp;rft.aulast=Yee&amp;rft.aufirst=Raymond&amp;rft.subject=Uncategorized&amp;rft.source=Hypotyposis+on+a+Good+Day&amp;rft.date=2023-10-11&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=https://hypotyposis.net/blog/2023/10/11/mic-04-3-non-recursive-walk-of-a-panflute-document/&amp;rft.language=English"></span>
{"id":1575,"date":"2023-10-11T22:07:44","date_gmt":"2023-10-12T05:07:44","guid":{"rendered":"https:\/\/hypotyposis.net\/blog\/?p=1575"},"modified":"2023-10-11T22:07:44","modified_gmt":"2023-10-12T05:07:44","slug":"mic-04-3-non-recursive-walk-of-a-panflute-document","status":"publish","type":"post","link":"https:\/\/hypotyposis.net\/blog\/2023\/10\/11\/mic-04-3-non-recursive-walk-of-a-panflute-document\/","title":{"rendered":"MIC 04-3 Non-recursive walk of a panflute document"},"content":{"rendered":"<p>Next round of programming yields:<\/p>\n<pre><code class=\"language-Python\">def panflute_to_bike_etree_nr(e, level=0) -&gt; ET.Element:\n\n    # questions about how header levels are handled as we put them into the etree\n    etree = None\n    stack = [(e, level)]\n    # current ul_elem\n    ul_elem = None\n    heading_level = 0\n\n    while stack:\n        (e, level) = stack.pop()\n        print(&quot;  &quot; * level, e.tag)\n\n        if is_inline_or_contentless(e):\n\n            li_elem = ET.Element(&quot;li&quot;)\n\n            # TO DO: handle rich text\n            p_elem = ET.SubElement(li_elem, &quot;p&quot;)\n            p_elem.text = pf.stringify(e).strip()\n\n            # 2 things to figure out: parent_ul (where to attach li_elem) and what the current ul_elem is\n\n            if e.tag == &quot;Header&quot;:\n                print (&quot;header&quot;, e.level, e.identifier, e.classes, e.attributes, pf.stringify(e))\n                li_elem.attrib[&quot;data-type&quot;] = &quot;heading&quot;\n                li_elem.attrib[&quot;data-level&quot;] = str(e.level)\n\n                if e.level &gt; heading_level:\n                    # child\n                    parent_ul = ul_elem\n                    print (&quot;e.level &gt; heading_level&quot;, ul_elem)\n                elif e.level == heading_level:\n                    # sibling\n                    # parent_ul has to be ul parent of ul_elem                    \n                    parent_ul = ul_elem.getparent().getparent()\n                else:\n                    # e.level &lt; heading_level\n                    # uncle or higher\n                    parent_ul = find_ul_ancestor(ul_elem, int(e.level)-1)\n\n                heading_level = e.level\n                ul_elem = ET.SubElement(li_elem, &quot;ul&quot;)\n                parent_ul.append(li_elem)\n            else:\n                ul_elem.append(li_elem)\n\n        else:\n            if e.tag == &quot;Doc&quot;:\n                etree = empty_bike_etree()\n                ul_elem = etree.xpath(&quot;\/\/ul&quot;)[0]\n            else:\n                # BulletList\n                # OrderedList\n                # ListItem\n                print(&quot;block&quot;, e.tag)\n\n            try:\n                for c in reversed(e.content):\n                    stack.append((c, level + 1))\n            except AttributeError:\n                pass\n\n    # clean up etree by adding ids\n    etree = add_ids_to_bike_etree(etree)\n    return etree\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Next round of programming yields: def panflute_to_bike_etree_nr(e, level=0) -&gt; 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 = &hellip; <a href=\"https:\/\/hypotyposis.net\/blog\/2023\/10\/11\/mic-04-3-non-recursive-walk-of-a-panflute-document\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1575","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7I6qs-pp","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts\/1575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/comments?post=1575"}],"version-history":[{"count":1,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts\/1575\/revisions"}],"predecessor-version":[{"id":1576,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/posts\/1575\/revisions\/1576"}],"wp:attachment":[{"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/media?parent=1575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/categories?post=1575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hypotyposis.net\/blog\/wp-json\/wp\/v2\/tags?post=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}