Created
February 14, 2025 12:02
-
-
Save hongjr03/2403a46e65d95e072dfc19b0b695d8d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #let t(traverse: x => { x }, body) = { | |
| let nodes = ( | |
| ( | |
| heading: none, | |
| elems: (), | |
| children: (), | |
| ), | |
| ) | |
| let curr_depth = 0 | |
| for elem in body.children { | |
| if elem.func() == heading { | |
| let node = ( | |
| heading: elem, | |
| elems: (), | |
| children: (), | |
| ) | |
| let depth = elem.depth | |
| while nodes.len() > 1 and nodes.last().heading != none and nodes.last().heading.depth >= depth { | |
| let child = nodes.pop() | |
| let parent = nodes.pop() | |
| parent.children.push(child) | |
| nodes.push(parent) | |
| } | |
| nodes.push(node) | |
| } else { | |
| let last = nodes.pop() | |
| last.elems.push(elem) | |
| nodes.push(last) | |
| } | |
| } | |
| while nodes.len() > 1 { | |
| let child = nodes.pop() | |
| let parent = nodes.pop() | |
| parent.children.push(child) | |
| nodes.push(parent) | |
| } | |
| traverse(nodes.first()) | |
| } | |
| #let traverse(node) = { | |
| node.heading | |
| block( | |
| stroke: black, | |
| width: 100%, | |
| inset: 5pt, | |
| { | |
| for elem in node.elems { | |
| elem | |
| } | |
| for child in node.children { | |
| traverse(child) | |
| } | |
| }, | |
| ) | |
| } | |
| // #let traverse(node, depth: 0) = { | |
| // let indent = " " * depth | |
| // // 输出当前节点信息 | |
| // if node.heading == none { | |
| // [#indent#[Root\ ]] | |
| // } else { | |
| // [#indent#[Heading(#node.heading.body) depth:#node.heading.depth\ ]] | |
| // } | |
| // // 输出当前节点的elems | |
| // for elem in node.elems { | |
| // [#indent Content: #elem\ ] | |
| // } | |
| // // 递归遍历子节点 | |
| // for child in node.children { | |
| // traverse(child, depth: depth + 1) | |
| // } | |
| // } | |
| // | |
| // #let traverse(node) = node | |
| #show: t.with(traverse: traverse) | |
| = section1 | |
| sth | |
| == section1.1 | |
| boomsdad | |
| == section2 | |
| 12345 | |
| = section1 | |
| sth | |
| == section1.1 | |
| asdf | |
| boom | |
| == section2 | |
| 12345 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment