diff --git a/src/intermediate/table.rs b/src/intermediate/table.rs index c52977e..bfaef7a 100644 --- a/src/intermediate/table.rs +++ b/src/intermediate/table.rs @@ -15,6 +15,12 @@ intermediate!( original, intermediate_context, { + // Separate groups by lines, multiple contiguous lines are the same as one. + // If there is only one group, it is a tbody. + // If there are more than one group, the first is thead and the rest are tbody. + + let sections = group_into_sections(&original.children); + let children = { let mut ret = Vec::new(); for obj in original.children.iter() { @@ -29,3 +35,41 @@ intermediate!( }) } ); + +enum GroupIntoSectionsState<'orig, 'parse> { + NonSection, + Section(Vec<&'orig organic::types::TableRow<'parse>>), +} + +fn group_into_sections<'orig, 'parse>( + rows: &'orig Vec>, +) -> Vec>> { + let mut sections = Vec::new(); + let mut rows = rows.into_iter(); + let mut state = GroupIntoSectionsState::NonSection; + loop { + state = match (state, rows.next()) { + (GroupIntoSectionsState::NonSection, None) => break, + (GroupIntoSectionsState::NonSection, Some(row)) if row.children.is_empty() => { + GroupIntoSectionsState::NonSection + } + (GroupIntoSectionsState::NonSection, Some(row)) => { + GroupIntoSectionsState::Section(vec![row]) + } + (GroupIntoSectionsState::Section(section), None) => { + sections.push(section); + break; + } + (GroupIntoSectionsState::Section(section), Some(row)) if row.children.is_empty() => { + sections.push(section); + GroupIntoSectionsState::NonSection + } + (GroupIntoSectionsState::Section(mut section), Some(row)) => { + section.push(row); + GroupIntoSectionsState::Section(section) + } + } + } + + sections +}