diff --git a/src/djot.rs b/src/djot.rs index 5676bcf..de63cc6 100644 --- a/src/djot.rs +++ b/src/djot.rs @@ -99,7 +99,21 @@ impl From for IrAlignment { } } +struct Context { + table_row_index: usize, +} + +impl Context { + fn new() -> Self { + Context { + table_row_index: 0, + } + } +} + pub fn djot_to_ir<'s>(mut djot: impl Iterator>) -> impl Iterator> { + let mut ctx = Context::new(); + // to be replaced by `gen`-blocks genawaiter::rc::Gen::new(|co| async move { while let Some(ev) = djot.next() { @@ -340,16 +354,12 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator>) -> impl Iterato } Event::Start(Container::Table, attributes) => { + ctx.table_row_index = 0; co.yield_(IrEvent::Start { container: IrContainer::Table, attributes: attributes.into(), }) .await; - co.yield_(IrEvent::Start { - container: IrContainer::TableBody, - attributes: IrAttributes::new(), - }) - .await; } Event::End(Container::Table) => { co.yield_(IrEvent::End { @@ -361,18 +371,47 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator>) -> impl Iterato }) .await; } - Event::Start(Container::TableRow { head: _ }, attributes) => { + Event::Start(Container::TableRow { head }, attributes) => { + if ctx.table_row_index == 0 { + if head { + co.yield_(IrEvent::Start { + container: IrContainer::TableHead, + attributes: IrAttributes::new(), + }) + .await; + } else { + co.yield_(IrEvent::Start { + container: IrContainer::TableBody, + attributes: IrAttributes::new(), + }) + .await; + } + } + co.yield_(IrEvent::Start { container: IrContainer::TableRow, attributes: attributes.into(), }) .await } - Event::End(Container::TableRow { head: _ }) => { + Event::End(Container::TableRow { head }) => { co.yield_(IrEvent::End { container: IrContainerEnd::TableRow, }) - .await + .await; + + if ctx.table_row_index == 0 && head { + co.yield_(IrEvent::End { + container: IrContainerEnd::TableHead, + }) + .await; + co.yield_(IrEvent::Start { + container: IrContainer::TableBody, + attributes: IrAttributes::new(), + }) + .await; + } + ctx.table_row_index += 1; } Event::Start(Container::TableCell { alignment, head }, attributes) => { co.yield_(IrEvent::Start { @@ -778,7 +817,7 @@ I. item 2 } #[test] - fn table() { + fn table_with_head() { test( r##" | head 1 | head 2 | @@ -786,11 +825,13 @@ I. item 2 | cell 1 | cell 2 | "##, r##" - + + + @@ -800,4 +841,49 @@ I. item 2 "##, ) } + + #[test] + fn table_without_head() { + test( + r##" +|--|--:| +| head 1 | head 2 | +| cell 1 | cell 2 | +"##, + r##"
head 1 head 2
cell 1 cell 2
+ + + + + + + + + + +
head 1head 2
cell 1cell 2
+"##, + ); + + test( + r##" +| head 1 | head 2 | +| cell 1 | cell 2 | +|--|--:| +"##, + r##" + + + + + + + + + + +
head 1head 2
cell 1cell 2
+"##, + ); + } }