Skip to main content

mdq/md_elem/
tree_ref.rs

1use crate::md_elem::elem::*;
2
3mod elem_ref {
4    use super::*;
5
6    impl Table {
7        pub fn alignments(&self) -> &[Option<ColumnAlignment>] {
8            &self.alignments
9        }
10
11        pub fn rows(&self) -> &Vec<TableRow> {
12            &self.rows
13        }
14
15        /// Normalizes this slice, so that every row has the same number of columns.
16        ///
17        /// If the table is jagged, all jagged rows will be filled in with [None] cells. Any missing
18        /// alignments will be filled in as `None`.
19        /// This is a departure from the Markdown standard, which specifies that the first row defines
20        /// the number of rows, and extras are discarded.
21        pub fn normalize(&mut self) {
22            let max_cols = self.rows.iter().map(Vec::len).max().unwrap_or(0);
23
24            for row in &mut self.rows {
25                let n_missing = max_cols - row.len();
26                for _ in 0..n_missing {
27                    row.push(Vec::new());
28                }
29            }
30            if self.alignments.len() > max_cols {
31                self.alignments.truncate(max_cols);
32            } else {
33                let nones = [None].iter().cycle().take(max_cols - self.alignments.len());
34                self.alignments.extend(nones);
35            }
36        }
37    }
38}