Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub fn split_cells(line: &str) -> Vec<String> {
if ch == '\\' {
if let Some(&next) = chars.peek() {
if next == '|' {
// `\|` escapes the pipe so it becomes part of the cell
chars.next();
cells.push(current.trim().to_string());
current.clear();
current.push('|');
continue;
}
}
Expand All @@ -55,6 +55,36 @@ pub fn split_cells(line: &str) -> Vec<String> {
cells
}

/// Formats the cells for a separator row based on column widths.
fn format_separator_cells(widths: &[usize], sep_cells: &[String]) -> Vec<String> {
if sep_cells.len() != widths.len() {
// A malformed separator row could cause a panic below when indexing
// `widths`. Return the cells unchanged so the caller can decide how to
// handle the mismatch gracefully.
return sep_cells.to_vec();
}

sep_cells
.iter()
.enumerate()
.map(|(i, cell)| {
let trimmed = cell.trim();
let left = trimmed.starts_with(':');
let right = trimmed.ends_with(':');
let mut dashes = "-".repeat(widths[i].max(3));
if left {
dashes.remove(0);
dashes.insert(0, ':');
}
if right {
dashes.pop();
dashes.push(':');
}
dashes
})
.collect()
}

/// Reflow a broken markdown table.
///
/// # Panics
Expand Down Expand Up @@ -131,15 +161,12 @@ pub fn reflow_table(lines: &[String]) -> Vec<String> {
cleaned.push(row);
}

if !split_within_line
&& cleaned
.iter()
.map(Vec::len)
.collect::<std::collections::HashSet<_>>()
.len()
> 1
{
return lines.to_vec();
if !split_within_line {
if let Some(first_len) = cleaned.first().map(Vec::len) {
if cleaned[1..].iter().any(|row| row.len() != first_len) {
return lines.to_vec();
}
}
}

let mut widths = vec![0; max_cols];
Expand All @@ -166,25 +193,7 @@ pub fn reflow_table(lines: &[String]) -> Vec<String> {
while sep_cells.len() < widths.len() {
sep_cells.push(String::new());
}
let sep_padded: Vec<String> = sep_cells
.iter()
.enumerate()
.map(|(i, cell)| {
let trimmed = cell.trim();
let left = trimmed.starts_with(':');
let right = trimmed.ends_with(':');
let mut dashes = "-".repeat(widths[i].max(3));
if left {
dashes.remove(0);
dashes.insert(0, ':');
}
if right {
dashes.pop();
dashes.push(':');
}
dashes
})
.collect();
let sep_padded = format_separator_cells(&widths, &sep_cells);
let sep_line_out = format!("{}| {} |", indent, sep_padded.join(" | "));
if let Some(first) = out.first().cloned() {
let mut with_sep = vec![first, sep_line_out];
Expand Down
6 changes: 5 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ fn test_reflow_preserves_header(header_table: Vec<String>) {

#[rstest]
fn test_reflow_handles_escaped_pipes(escaped_pipe_table: Vec<String>) {
Comment thread
leynos marked this conversation as resolved.
let expected = vec!["| X | Y |", "| a | b | 1 |", "| 2 | 3 |"];
// The fixture contains a header row followed by a row with an escaped
// pipe sequence (`a \| b`). After reflow the escaped pipe becomes a literal
// `|` inside the first data cell, so the table has three columns and the
// header row is padded to match.
let expected = vec!["| X | Y |", "| a | b | 1 |", "| 2 | 3 |"];
assert_eq!(reflow_table(&escaped_pipe_table), expected);
}

Expand Down
Loading