tree: add Branch::branch
will be used to defer inline parsing of footnotes
This commit is contained in:
parent
1dde9e57e2
commit
412067a190
1 changed files with 91 additions and 33 deletions
124
src/tree.rs
124
src/tree.rs
|
@ -62,6 +62,22 @@ impl<C, A> Branch<C, A> {
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Split off the remaining part of the current branch. The returned [`Branch`] will continue on
|
||||||
|
/// the branch, this [`Branch`] will skip over the current branch.
|
||||||
|
pub fn take_branch(&mut self) -> Self {
|
||||||
|
let head = self.head.take();
|
||||||
|
self.head = self.branch.pop();
|
||||||
|
if let Some(h) = self.head {
|
||||||
|
let n = &self.nodes[h.index()];
|
||||||
|
self.head = n.next;
|
||||||
|
}
|
||||||
|
Self {
|
||||||
|
nodes: self.nodes,
|
||||||
|
branch: Vec::new(),
|
||||||
|
head,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Retrieve all inlines until the end of the current container. Panics if any upcoming node is
|
/// Retrieve all inlines until the end of the current container. Panics if any upcoming node is
|
||||||
/// not an inline node.
|
/// not an inline node.
|
||||||
pub fn take_inlines(&mut self) -> impl Iterator<Item = Span> + '_ {
|
pub fn take_inlines(&mut self) -> impl Iterator<Item = Span> + '_ {
|
||||||
|
@ -270,6 +286,9 @@ impl<C: std::fmt::Debug + Clone, A: std::fmt::Debug + Clone> std::fmt::Debug for
|
||||||
mod test {
|
mod test {
|
||||||
use crate::Span;
|
use crate::Span;
|
||||||
|
|
||||||
|
use super::Event;
|
||||||
|
use super::EventKind;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fmt() {
|
fn fmt() {
|
||||||
let mut tree = super::Builder::new();
|
let mut tree = super::Builder::new();
|
||||||
|
@ -310,40 +329,79 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fmt_container() {
|
fn branch_take_branch() {
|
||||||
let mut tree: super::Builder<u8, u16> = super::Builder::new();
|
let mut b = super::Builder::new();
|
||||||
tree.enter(1, Span::new(0, 1));
|
let sp = Span::new(0, 0);
|
||||||
tree.atom(11, Span::new(0, 1));
|
b.enter(1, sp);
|
||||||
tree.atom(12, Span::new(0, 1));
|
b.atom(11, sp);
|
||||||
tree.exit();
|
b.exit();
|
||||||
tree.enter(2, Span::new(1, 5));
|
b.enter(2, sp);
|
||||||
tree.enter(21, Span::new(2, 5));
|
b.enter(21, sp);
|
||||||
tree.enter(211, Span::new(3, 4));
|
b.atom(211, sp);
|
||||||
tree.atom(2111, Span::new(3, 4));
|
b.exit();
|
||||||
tree.exit();
|
b.exit();
|
||||||
tree.exit();
|
b.enter(3, sp);
|
||||||
tree.enter(22, Span::new(4, 5));
|
b.atom(31, sp);
|
||||||
tree.atom(221, Span::new(4, 5));
|
let tree = b.finish();
|
||||||
tree.exit();
|
|
||||||
tree.exit();
|
let mut root_branch = tree.root();
|
||||||
tree.enter(3, Span::new(5, 6));
|
|
||||||
tree.atom(31, Span::new(5, 6));
|
|
||||||
tree.exit();
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{:?}", tree),
|
(&mut root_branch).take(3).collect::<Vec<_>>(),
|
||||||
concat!(
|
&[
|
||||||
"1 (0:1)\n",
|
Event {
|
||||||
" 11 (0:1)\n",
|
kind: EventKind::Enter(1),
|
||||||
" 12 (0:1)\n",
|
span: sp
|
||||||
"2 (1:5)\n",
|
},
|
||||||
" 21 (2:5)\n",
|
Event {
|
||||||
" 211 (3:4)\n",
|
kind: EventKind::Atom(11),
|
||||||
" 2111 (3:4)\n",
|
span: sp
|
||||||
" 22 (4:5)\n",
|
},
|
||||||
" 221 (4:5)\n",
|
Event {
|
||||||
"3 (5:6)\n",
|
kind: EventKind::Exit(1),
|
||||||
" 31 (5:6)\n",
|
span: sp
|
||||||
)
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
root_branch.next(),
|
||||||
|
Some(Event {
|
||||||
|
kind: EventKind::Enter(2),
|
||||||
|
span: sp
|
||||||
|
})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
root_branch.take_branch().collect::<Vec<_>>(),
|
||||||
|
&[
|
||||||
|
Event {
|
||||||
|
kind: EventKind::Enter(21),
|
||||||
|
span: sp
|
||||||
|
},
|
||||||
|
Event {
|
||||||
|
kind: EventKind::Atom(211),
|
||||||
|
span: sp
|
||||||
|
},
|
||||||
|
Event {
|
||||||
|
kind: EventKind::Exit(21),
|
||||||
|
span: sp
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
root_branch.collect::<Vec<_>>(),
|
||||||
|
&[
|
||||||
|
Event {
|
||||||
|
kind: EventKind::Enter(3),
|
||||||
|
span: sp
|
||||||
|
},
|
||||||
|
Event {
|
||||||
|
kind: EventKind::Atom(31),
|
||||||
|
span: sp
|
||||||
|
},
|
||||||
|
Event {
|
||||||
|
kind: EventKind::Exit(3),
|
||||||
|
span: sp
|
||||||
|
},
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue