Group table rows into sections.

This commit is contained in:
Tom Alexander 2023-12-22 00:08:06 -05:00
parent 5dfd46852f
commit 7741e192f5
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 44 additions and 0 deletions

View File

@ -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<organic::types::TableRow<'parse>>,
) -> Vec<Vec<&'orig organic::types::TableRow<'parse>>> {
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
}